Code Review Checklist for Java Beginners

Checklists are always helpful! They provide a quick check to ensure consistency and completeness in carrying out a task efficiently and effectively. Here, I've consolidated a basic 20 points checklist for Java Beginners to review the code. It'll help them to ensure code quality and consistency. Without further ado. Let's go through it.

1. Null Checks

We know NullPointerException is the most common exception in Java and can cause big problems. So, as a general practice, always do a null check on a variable before any operation.

2. Exception Handling

The try-catch block should be used for exception handling with proper logging in the catch block. Also, make sure to close the resources properly in the finally block.

3. Code Indentation and Formatting

For a cleaner and readable code, use code indentation thoroughly (with Tab or Spaces anything). It can be done automatically with the built-in editor of the IDE. For instance, use Ctrl-Shift-F in Eclipse. Similarly, Ctrl-Alt-L in IntelliJ. Make use of Java formatting rules!

4. Optimize Imports

Always optimize imports in the Java class.

5. Static Code Review Tools

Use static code review tools like Sonar, PMD, and FindBugs to review the code.

6. Constants

7. Naming Conventions

8. Not All One-Liners

It's good to keep the code clean and readable. So, it's a better idea to not always go with one-liners. Especially, when we initialize and operate the variable in one line.

For example, write:

out.write(attrs.get("offset") + "-" + Math.min(attrs.get("count"), attrs.get("offset") + attrs.get("max")) + " " + title + " " + attrs.get("count")); 
Enter fullscreen mode

Exit fullscreen mode

int start = attrs.get("offset") int total = attrs.get("count")) int end = Math.min(total, start + attrs.get("max")) out.write(start + "-" + end + " " + title + " " + total) 
Enter fullscreen mode

Exit fullscreen mode

9. White-Spaces

Use white-spaces to separate combined statements to make code more readable.

For example, write:

Integer.valueOf(params.offset?params.offset:attrs.offset) 
Enter fullscreen mode

Exit fullscreen mode

Integer.valueOf(params.offset ? params.offset : attrs.offset) 
Enter fullscreen mode

Exit fullscreen mode

10. Spaces Before and After Brackets

In general, we don't use white spaces in the brackets.

For example, write:

if ( params ) if ( total > 0 ) if ( end  begin ) 
Enter fullscreen mode

Exit fullscreen mode

if (params) if (total > 0) if (end  begin) 
Enter fullscreen mode

Exit fullscreen mode

11. Curly Braces

Use curly braces for one-liners also.

For example, write:

if ( end  begin ) end = begin 
Enter fullscreen mode

Exit fullscreen mode

if (end  begin)  end = begin > 
Enter fullscreen mode

Exit fullscreen mode

12. Comments

Always put comments (if any) defining the purpose.

For example, Javadoc on a class:

/** * General convenience tags for layout - header, body and footer * @author – Name * @dateCreated - Date */ class LayoutTagLib  > 
Enter fullscreen mode

Exit fullscreen mode

Javadoc on a method:

/** * Gets the user for specified code and role. * @param code :- The code, either username or email address * @param role :- The role identification e.g. A, B or C. Default is A. * @return the user or null if not found */ User findUser(String code, String role = "A") 
Enter fullscreen mode

Exit fullscreen mode

Make sure the code is self-explanatory and comments are really useful in very specific cases.

13. Clean Up

14. Business Logic

Avoid redundant code by using reusable components like utilities and service methods.

15. StringBuilder or StringBuffer in Place of String

When performing a lot of operations on the String, use StringBuilder or StringBuffer

For example, Java creates a new String object for every concatenation operation. In this case, a better idea is to use a StringBuffer.

16. switch-case Over Multiple if-else

It's a good practice to use switch-case in place of multiple if-else conditions.

It optimizes the code execution and also makes code cleaner and more readable.

17. Objects Creation in a Loop

It is usually better to create the object inside the loop (If object is not required outside loop). Java optimizes memory usage for short-lived objects.

For example, write:

Person person; for (int i=0; inamesList.size(); i++)  person = new Person(); person.setName(namesList.get(i)); person.display(); > 
Enter fullscreen mode

Exit fullscreen mode

for (int i=0; inamesList.size(); i++)  Person person = new Person(); person.setName(namesList.get(i)); person.display(); > 
Enter fullscreen mode

Exit fullscreen mode

Also, create a new object only if required.

For example, write:

ArrayListPerson> personList = new ArrayListPerson>(); for (int i=0; inamesList.size(); i++)  Person person = new Person(); if (null != namesList.get(i))  person.setName(namesList.get(i)); personList.add(person); > > 
Enter fullscreen mode

Exit fullscreen mode

ArrayListPerson> personList = new ArrayListPerson>(); for (int i=0; inamesList.size(); i++)  if (null != namesList.get(i))  Person person = new Person(); person.setName(namesList.get(i)); personList.add(person); > > 
Enter fullscreen mode

Exit fullscreen mode

18. Code Commit

19. Use equals over ==

equals perform the actual comparison of two strings, whereas == compares object references.

20. Keep It Simple!

Maintain simplicity and readability of code.

Please let me know your thoughts on it.
Thanks for reading.