Java Code Review Checklist: How to Review Java Code

Java Code Review Checklist

How to Review Java Code? Which things should be maintained, what are the common flaws we should look after? There are thousands of questions in our mind. Here I've tried feed all those curious minds. 

Preface

With time when you've become a senior programmer, you need to have a code review checklist to evaluate code quality that your fellow programmers write. On the other hand, programmers need to know quality matrix based on which their code will be measured. Here I've compiled a review checklist from various online sources that can help both engineers and tech leads to ensure better quality code in their projects.

Categories to Review

I've categorized the review checklist to improve java code by focusing on a particular area of the program or one aspect at a time.

I have categorized code review checklist into following 4 categories:

1) General
2) Documentation
3) Threading, and
4) Security

The Code Review Checklist

For each of the above categories, the following sections enlist the important review checklist items. Items in the list may be duplicated in more than one category because those are applicable in both. All the item enlisted may or may not necessary for all java project.

General

General-1The code works
General-2The code is easy to understand
General-3Follows coding conventions
General-4Names are simple and if possible short
General-5Names are spelt correctly
General-6Names contain units where applicable
General-7Classes and Methods should be small!
General-8Enums are used instead of int constants where applicable
General-9There are no usages of 'magic numbers'
General-10All variables are in the smallest scope possible
General-11All class, variable, and method modifiers are correct.
General-12There is no dead code (inaccessible at Runtime)
General-13No code can be replaced with library functions
General-14Required logs are present
General-15Frivolous logs are absent
General-16No System.out.println() or similar calls exist
General-17No stack traces are printed
General-18Variables are not accidentally used with null values
General-19Variables are immutable where possible
General-20Code is not repeated or duplicated
General-21There is an else block for every if clause even if it is empty
General-22No complex/long boolean expressions
General-23No negatively named boolean variables
General-24No empty blocks of code
General-25Ideal data structures are used
General-26Constructors do not accept null/none values
General-27Collections are initialized with a specific estimated capacity
General-28Arrays are checked for out of bound conditions
General-29Favor the use of standard exceptions
General-30Catch clauses are fine grained and catch specific exceptions
General-31APIs and other public contracts check input values and fail fast
General-32StringBuilder is used to concatenate strings
General-33Null/None are not returned from any method
General-34Floating point numbers are not compared for equality
General-35Loops have a set length and correct termination conditions
General-36Blocks of code inside loops are as small as possible
General-37Order/index of a collection is not modified when it is being looped over
General-38No methods with boolean parameters
General-39No object exists longer than necessary
General-40Design patterns if used are correctly applied
General-41No memory leaks
General-42Methods return early without compromising code readability
General-43Check parameters for validity
General-44Return empty arrays or collections, not nulls
General-45In public classes, use accessor methods, not public fields
General-46No use of Object class, use generics instead

Documentation

Doc-1All methods are commented in clear language.
Doc-2There is no commented out code
Doc-3Comments exist and describe rationale or reasons for decisions in code
Doc-4All public methods/interfaces/contracts are commented describing the usage
Doc-5All unusual behavior or use case handling is described in comments
Doc-6Data structures and units of measurement are explained
Doc-7Document security-related information
Doc-8Avoid excessive logs for unusual behavior
Doc-9Do not log highly sensitive information

Threading

Thread-1Objects accessed by multiple threads are accessed only through a lock, or synchronized methods.
Thread-2Race conditions have been handled
Thread-3Locks are acquired and released in the right order to prevent deadlocks, even in error handling code.
Thread-4StringBuffer is used to concatenate strings in multi-threaded code
Thread-5Synchronize access to shared mutable data
Thread-6Keep Synchronized Sections Small

Security

Sec-1All data inputs are checked (for the correct type, length/size, format, and range)
Sec-2Invalid parameter values handled such that exceptions are not thrown
Sec-3No sensitive information is logged or visible in a stacktrace
Sec-4Make class final if not being used for inheritance
Sec-5Minimize the accessibility of classes and members
Sec-6Input into a system should be checked for valid data size and range
Sec-7Release resources (Streams, Connections, etc) in all cases
Sec-8Purge sensitive information from exceptions (exposing file path, internals of the system, configuration)
Sec-9Consider purging highly sensitive from memory after use 
Sec-10Avoid dynamic SQL, use prepared statement
Sec-11Limit the accessibility of packages,classes, interfaces, methods and fields using access control mechanism
Sec-12Validate inputs (for valid data, size, range, boundary conditions, etc)
Sec-13Define wrappers around native methods (do not declare a native method public)
Sec-14Make public static fields final (to avoid caller changing the value)
Sec-15Avoid exposing constructors of sensitive classes
Sec-16Avoid serialization for security-sensitive classes
Sec-17Be careful caching results of potentially privileged operations

References

Followings are the web resources which I've used to write the review checklist:
  1. https://dzone.com/articles/java-code-review-checklist
  2. https://gist.github.com/kashifrazzaqui/44b868a59e99c2da7b14
  3. https://portal.uci.edu/uPortal/f/welcome/p/webproxy-cms-file-view.u20l1n201/max/render.uP?pP_cmsUri=public%2FAdCom%2FQA-QualityAssurance%2FcheckListJavaCodeReview.xml
  4. https://www.evoketechnologies.com/blog/code-review-checklist-perform-effective-code-reviews/

Comments