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.
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.
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.
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!
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
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
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
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
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.
Avoid redundant code by using reusable components like utilities and service methods.
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.
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.
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
equals perform the actual comparison of two strings, whereas == compares object references.
Maintain simplicity and readability of code.
Please let me know your thoughts on it.
Thanks for reading.