Class Structure - alexdaube/My-Software-Engineering-Guide GitHub Wiki

Class Structure

Intro

  • Clean needs to be well organized inside classes
  • Classes must have proper package and namespace structure

Internal Organization(Java)

Attributes

  1. Constants (private final static)
  2. Static private attributes
  3. Private attributes(members)

Methods

  1. Public methods => Or logically linked group of methods
  2. Private methods that they use
  3. Other public methods or group...

Principles

  • We aim to read the class just like we would a newspaper
  • Sometimes getters/setters go to the end to avoid extra noise
  • No public attributes => rare exceptions

Encapsulation

  • Prefer private attributes
  • There are times where we need to compromise
  • Be weary of protected attributes

What it contains?

  1. Class should be small
  2. It should be even smaller
  • It is not measured in terms of LOC(lines of code), rather in terms of responsibilities. Should respect Single Responsibility Principle(SRP)

Class Packaging

Maximize in namespaces and packages

Default package

  • Never put classes in a default package

Respect naming conventions

  • Proper to the language
  • Considers business model, glossary, etc..

Less abbreviations as possible

  • They do not mean the same thing to everybody

In logical grouping

  • What looks alike sticks together
  • Think about dependencies
  • Think about code reuse
  • Logical arborescence

Separate sub-components

  • Should be separated in sub-packages