System Design Principles - Java
Basic
- Clean Code
- Meaningful names of variables and functions
- Intention revealing & Descriptive
- Function name should generally be verb
- Class name should generally be noun
- Meaningful comments
- Indentation, Structure, Readable
- Curly Braces
- Functions
- Generally number of lines should fit on your screen (20 LOC)
- Should have a single objective
- Should have fewer arguments => Less than 2
- Create helper functions
- Decompose into multiple functions
- Number of characters in a line should be low
- Modular
- Design
- High-level
- Low-level
- Implementation based
- Class & Interface
Principles
- DRY (Don't Repeat Yourself)
- Otherwise difficult to maintain
- Use functions & classes to achieve it
- KISS (Keep is Simple Stupid)
- Benefits => Bugs changes are less, Less time to code
- Abstraction
- Curly's Law => Each entity should perform single responsibility
- Boy-Scout Law => Make the code better than how you found it
- Ward's principle => Understanding the functionality of a function based on its name and then understanding it
- YAGNI (You ain't gonna need it)
- Always implement things which are actually needed, not the ones you just foresee
- Cohesion
- Making sure that a class is designed with a single, well-focused purpose
- Degree of how strongly related & focused are the various responsibilities of a module
- Command-Query separation (CQS)
- Command => Changes the state but does not returns any value
- Query => Returns the value without changing any state
- A method should either be a Query or Command
- Stack popping is a well known exception
SOLID
- Single Responsibility
- Open-Source Principle (OSP)
- Open for extension but closed for modification
- Liskov Substitution Principle (LSP)
- Subtypes must be substitutable for their base types
- Interface Segregation Principle (ISP)
- Dependency of one class to another should be on smallest possible interface
- Dependency Inversion Principle (DIP)
- Depend on abstraction (Interface), not on concrete classes