Ultimate Clean Code Guide for Java, Spring based Applications
Coding Best Practices and OOP Design Principles to Improve Life of a Programmer
“Master programmers think of systems as stories to be told rather than programs to be written” ~ Uncle Bob
1. Naming Convention: All variables, methods and classes should use CamelCase style. The only difference: a first letter of class should be capital. Most importantly use intention-revealing, pronounceable names.
2. Single Responsibility Principle: One method should be responsible only for one action, it should do it well and do it only. Reduce coupling, if our method does two or three different things at a time then we should consider splitting the functionality.
3. Small Methods: There is not a standard pattern for method length among the developers. Someone can say 5, in some cases even 20 lines of code is okay. Just try to make methods as small as possible.
4. Duplication: Avoid code duplication. If we cannot reuse a method in other place then probably this method is bad and we should consider a better way to write this method. Use Abstraction to abstract common things in one place.
5. Variables/Methods Declaration:
- Encapsulate the code you feel might changed in future.
- Make variable and methods private by default and increasing access step by step like from a private to protected and not public.
- Classes, methods or functions should be Open for extension and Closed for modification(Open Closed Design Principle).
- Program for the interface and not for implementation, you should use interface type on variables, return types of a method or argument type of methods. Just like using SuperClass type to store object rather using SubClass.
- The use of interface is to facilitate polymorphism, client should not implement an interface method if its not needed. Although Java 8 has introduced default methods which allow the interface to have methods with implementation.
6. Structure your code correctly:
- Default packages not…