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
- Constants (private final static)
- Static private attributes
- Private attributes(members)
Methods
- Public methods => Or logically linked group of methods
- Private methods that they use
- 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?
- Class should be small
- 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