Java : Best Practices Checklist

Shreyas M N
Java Cafe
Published in
2 min readOct 19, 2020

--

photo courtesy: https://www.serenova.com/
photo courtesy: https://www.serenova.com

Hello coding community, I thought of writing about the best practices I follow while coding in Java 8 and I make sure I tick all the below checkboxes before I create a Pull Requests, day to day.

The best practices listed below are quite generic and varies from company to company, org to org, team to team and developer to developer.

I don’t go in detail about each of the best practice as It would make this article unnecessarily long.

Take it as a generic checklist and follow best practices whenever possible.

  1. Always format the code: ⌥⌘L (in IntelliJ)
  2. Write {@link --} type Javadoc for methods, classes and variables
  3. Declare an Inner/ Anonymous class instead of concatenating multiple strings/classes(Builder-Pattern)
  4. Prefer event-based-queues instead of synchronizing data-structures across multiple-threads
  5. Use lambda expressions and streams instead of anonymous classes and for loops respectively.
  6. Try employing synchronized data-structures instead of using synchronized keyword
  7. Use meaningful and informative names for classes, methods and variables
  8. Prefer smaller and single-responsibility methods
  9. Prefer Thread over ThreadPool for single-task execution (ex: consumer thread)
  10. Always execute callbacks using separate callback-executors
  11. Create marker-interface to identify set of similar-classes to poke around
  12. Add default-methods in Interfaces, avoiding code duplication in Classes
  13. Override equals() and hashCode() methods in DTOs referring to their PRIMARY-KEY, to avoid duplicates
  14. Make DTOs / Java-beans Immutable by providing ONLY getters and private constructors
  15. Use Enums for related, constant set of variables
  16. Use generics for more typeSafe code
  17. Make use of Try-with-resources and Multi-Catch exceptions features
  18. Use CompletableFuture for Asynchronous stuff
  19. Use Optionals instead of nulls
  20. Always override toString() in DTOs/Java beans
  21. Avoid passing or returning null values, by returning empty-array or list (ex: Collections.emptyList())
  22. Use PreparedStatements inside the DTO/ Java beans to avoid the spaghetti code in Implementation classes
  23. Write separate Util classes (ex: StringUtil, MathUtil....)
  24. Don’t duck the exception, Instead Create an exception, throw an exception and handle it
  25. Make variables effectively final wherever possible, as java optimizes final declarations internally
  26. Prefer switch statements over if-else as Java better optimizes switch statements
  27. Use MappedByteBuffer class for best gains with file operations whose content is a memory-mapped region of a file.
  28. Use of Lombok makes code look elegant, resulting in a lot cleaner code

Stay tuned and I will try to write in detail about each of the above best practice.

Happy Coding and Engineering.!!

Resources:

  1. https://docs.oracle.com
  2. https://stackoverflow.com

--

--

Shreyas M N
Java Cafe

Software Engineer, -Passionate about building Scalable backend systems. https://www.linkedin.com/in/shreyasmn