Java (Base language)¶
To learn more about advanced Java topics such as the Java virtual machine, collections language, and many more topics, visit baeldung.com ⧉.
Refactoring.guru ⧉ has information about general code smells.
- Be aware of common Java gotchas ⧉.
- Avoid acronyms and abbreviations that may not be clear to others.
- Name methods and variables after what they mean. Before giving a name, consider the responsibility of that piece of code (Naming 101: A Programmer's Guide on How to Name Things ⧉).
- 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 (Rule of 30 – When is a Method, Class, or Subsystem Too Big? and Avoid basic style errors ⧉)
- Remember to test the parameters of public methods for illegal values. These methods shouldn't crash because of a NullPointer.
- Order class members by scope from private to public (Ordering Class Members by Scopes and Separate public and private members ⧉)
- 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: They include only static methods and are stateless. Don't create an instance of such a class.
- Helper class: They can be utility classes, or they can be stateful or require an instance. 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 of Helper).
More Tips¶
- If you use functional interfaces, use the standard Java ones. If you want to understand the functional style, consult the page: Understand the functional style ⧉.
- If you want to use newer Java features, look at Modernize old code ⧉.
- Have a look at common sources of complexity ⧉.
Stack Exchange: Software Engineering¶ ⧉
- When do you use float, and when do you use double ⧉
- What's wrong with circular references? ⧉
- Are null references really a bad thing? ⧉
- Exceptions: Why throw early? Why catch late? ⧉
- How do quick & dirty programmers know they got it right? ⧉
- Why is Global State so Evil? ⧉
- How would you know if you've written readable and easily maintainable code? ⧉
- How do you know you're writing good code? ⧉