Information about general code smells can be found here.
- Be aware of common Java gotchas.
- Avoid acronyms and abbreviations that may not be clear to others.
- Methods and variables should be named after what they mean. Before giving a name, consider what is the responsibility of that piece of code.1
- Return empty collections instead of returning null elements (reference). It saves the efforts needed for testing on null elements.
- Efficiency: Use StringBuilder or StringBuffer for String concatenation
- Refactor your code regularly, especially when your classes or methods become too big.2,3.
- Remember to check parameters of public methods for illegal values. These methods shouldn't crash because of a NullPointer.
- Order class members by scope from private to public4,5 (the reserve order is also fine )
- Minimize the accessibility of class members. It enforces information hiding or encapsulation.
- Avoid hardcoded values.
- Use lazy initialization if performance is critical or use caching if necessary.
Utility and Helper classes¶
- Utility class = only static methods and stateless. Don't create an instance of such a class.
- Helper class = can be a utility class, or it can be stateful or require an instance be created. It can be any class whose design is to aid another class.
- Try to make the name of the utility or helper class more specific (e.g.
AdministrationHelper
,LoginHelper
instead ofHelper
).
Additional tips¶
- If you use functional interfaces, use the standard java ones. If you want to understand the functional style, consult this page.
- If you want to use newer Java features, have a look at this page.
- Have a look at common sources of complexity.
Last update:
November 11, 2021