Java Clean Code
Principals:
- KISS (Keep it simple stupid): We write code for humans to read. Avoid unnecessary complexity, make it self explanatory.
- DRY (Don't repeat yourself): Reusability is a bliss, saves efforts and easy to maintain.
- SOLID(Single responsibility, Open/Close, Liskov Substitution, Interface Segregation, Dependency Inversion): https://www.baeldung.com/solid-principles
Design rules:
- Keep configurable data at high levels.
- Prefer polymorphism to if/else or switch/case.
- Separate multi-threading code.
- Prevent over-configurability.
- Use dependency injection.
- Follow Law of Demeter. A class should know only its direct dependencies.
Understandability:
- Be consistent. If you do something a certain way, do all similar things in the same way.
- Use explanatory variables.
- Encapsulate boundary conditions. Boundary conditions are hard to keep track of. Put the processing for them in one place.
- Prefer dedicated value objects to primitive type.
- Avoid logical dependency. Don't write methods which works correctly depending on something else in the same class.
- Avoid negative conditionals.
Names:
- Choose descriptive and unambiguous names.
- Make meaningful distinction.
- Use pronounceable names.
- Use searchable names.
- Replace magic numbers with named constants.
Functions:
- Small.
- Do one thing.
- Use descriptive names.
- Prefer fewer arguments.
- Have no side effects e.g., don't change args.
- Don't use flag arguments. Split method into several independent methods that can be called from the client without the flag.
- Return Optional if method expected to return null.
Source code structure:
- Separate concepts vertically.
- Related code should appear vertically dense.
- Declare variables close to their usage.
- Dependent functions should be close.
- Similar functions should be close.
- Place functions in the downward direction.
Objects and data structures
- Hide internal structure.
- Prefer data structures.
- Avoid hybrids structures (half object and half data).
- Should be small.
- Do one thing.
- Small number of instance variables.
- Base class should know nothing about their derivatives.
- Better to have many functions than to pass some code into a function to select a behaviour.
- Prefer non-static methods to static methods.
Others:
- If possible, set the initial capacity of a collection appropriately.
- Program in terms of interfaces.
- Return zero length collections or arrays as opposed to returning null.
- Immutable objects should be used as keys for the HashMap.
- Avoid storing unrelated or different types of objects into same collection
Design Patterns: https://www.edureka.co/blog/java-design-patterns/
Software Architecture: well defined structure for building software that meets specific requirements
- Layered
- Client Server
- Master Slave
- Event Bus
- Pipe and Filter
- Broker
- Peer to Peer(Each node can act as client and server)
- MVC
- Interpreter (Use to create languages)
- Blackboard (Central repository of data call Blackboard, on which small s/w module call Knowledge Source perform operations. Used in AI, Data engineering)