Java code review checklist

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A code review guide and checklist when working with Java, Spring and related technologies

License

kdsingharneja/the-java-code-review-checklist

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Читайте также:  Npm normalize css gulp

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

The Java Code Review Checklist

A code review guide and checklist when working with Java and related technologies. The following should really help when writing new code in Java applications after upgrading to Java 8 or refactoring code that is < Java8

Runnable runner = new Runnable() < public void run()< System.out.println("I am running"); >>; 

Refactor interfaces with default methods

public class MyClass implements InterfaceA < /** * @param args the command line arguments */ public static void main(String[] args) < // TODO code application logic here >@Override public void saySomething() < System.out.println("Hello World"); >> interface InterfaceA

Use default methods. Make sure you do not do this as a a habit because this pattern pollutes interfaces.

public class MyClass implements InterfaceA < /** * @param args the command line arguments */ public static void main(String[] args) < // TODO code application logic here >@Override public void saySomething() < System.out.println("Hello World"); >> interface InterfaceA < public void saySomething(); default public void sayHi() < System.out.println("Hi"); >> 

Prefer Streams to reduce code.

private static void printNames(List persons, Predicate predicate) < persons.stream() .filter(predicate) .map(p ->p.getName()) .forEach(name -> System.out.println(name)); > > 
Arrays.parallelSort(myArray); 

Depend on parameter reflection

Person getEmployee(@PathParam("dept") Long dept, @QueryParam("id") Long id) 
Person getEmployee(@PathParam Long dept, @QueryParam Long id) 

Since params names as same as var names.

Prefer to use «filter / map / reduce» approach

List names = Arrays.asList("Smith", "Adams", "Crawford"); List people = peopleDAO.find("London"); // Using anyMatch and method reference List anyMatch = people.stream().filter(p -> (names.stream().anyMatch(p.name::contains))).collect(Collectors.toList()); // Using reduce List reduced = people.stream().filter(p -> names.stream().reduce(false (Boolean b, String keyword) -> b || p.name.contains(keyword), (l, r) -> l | r)).collect(Collectors.toList()); 
Clock clock = Clock.systemUTC(); //return the current time based on your system clock and set to UTC. Clock clock = Clock.systemDefaultZone(); //return time based on system clock zone long time = clock.millis(); //time in milliseconds from January 1st, 1970 

About

A code review guide and checklist when working with Java, Spring and related technologies

Источник

Java Code Review Checklist

Follow the checklist with the top 10 steps for Java code review in order to deliver quality code that enables a great customer experience.

Code reviews, or peer reviews, can sometimes feel like an unnecessary chore, especially when there is a backlog of features to work on, leaving very little time for these reviews. However, manual or automated reviews are essential to delivering quality code that provides a great customer experience.

This guide covers some of the most common items to check in a Java code review to ensure your code is reliable and easy to read, maintain and scale.

1. Ensure the code follows standard naming conventions

Meaningful naming conventions help ensure the readability and maintainability of the application.

As such, ensure variable, method, and class names convey the subject:

Use all lower cases for package names and use reversed Internet domain naming conventions:

Class names should start with Capitals:

Variable and method names should use CamelCase:

2. Make sure it handles constants efficiently

Constants help improve memory as they are cached by the JVM. For values that are reused across multiple places, create a constant file that holds static values.

Favor database-driven values over dynamic values. Also, use ENUMs to group constants.

3. Check for proper clean Up

It is common during development to use procedures that help with your coding and debugging (hard coded variables, for example). It is good practice to remove these items and others such as:

  • Console print statements
  • Unnecessary comments
  • Use @deprecated on method/variable names that aren’t meant for future use

4. Handle strings appropriately

If you need to perform a lot of operations on a String, use StringBuilder or StringBuffer.

Strings are immutable, whereas StringBuilder and StringBuffer are mutable and can be changed. Additionally, a new String object is created for every concatenation operation.

Rather than creating multiple items, using a mutable object is preferred.

5. Optimize to use switch-case over multiple If-Else statements

Rather than using multiple if-else conditions, use the cleaner and more readable switch-case.

Doing so makes the logic cleaner and optimizes the app’s performance.

6. Ensure the code follows appropriate error handling procedures

The NullPointerException is one of the most common and frustrating exceptions.

However, they can be avoided by performing a null check on a variable before operating on it.

The best practice is to use checked exceptions for recoverable operations and use runtime exceptions for programming errors.

Another area to evaluate during a Java code review is to ensure all exceptions are wrapped in custom exceptions.

In this way, the stack trace is preserved, making it easier to debug when things go wrong.

Also, it should declare specific checked exceptions that the method throws rather than generic ones. Doing so doesn’t give you the option to handle and debug the issue appropriately.

public void hello() throws Exception < //Incorrect way

Try this instead:

public void hello() throws SpecificException1, SpecificException2 < //Correct way

Use the try-catch block for exception handling with appropriate actions taken in the catch block.

Also, use a finally block to release resources, such as database connections, in the finally block. This allows you to close the resource gracefully.

7. Avoid unnecessary comments in code?

Comments should not be used to explain code. If the logic is not intuitive, it should be rewritten. Use comments to answer a question that the code can’t.

Another way to state it is that the comment should explain the “why” versus “what” it does.

8. Validate that the code follows separation of concerns

Ensure there is no duplication. Each class or method should be small and focus on one thing.

For example:

EmployeeDao.java — Data access logic

Employee.java — Domain Logic

EmployeerService.java — Business Logic

EmployeeValidator.java — Validating Input Fields

9. Does the code rely on frameworks rather than custom logic when possible?

When time is of the essence, reinventing the wheel is time wasted. There are plenty of proven frameworks and libraries available for the most common use cases you may need.

Examples include Apache Commons libraries, Spring libraries, and XML/JSON libraries.

10. Make sure variables don’t cause memory leaks

Creating a bunch of unnecessary variables can overwhelm the heap and lead to memory leaks and cause performance problems. This occurs when an object is present in the heap but is no longer used, and the garbage collection cannot remove it from memory.

Try This Instead

Performing regular Java code reviews can help identify issues before the application makes it to production.

The more thorough you are about the process, the less chance you’ll miss anything that could be added to your backlog.

How Adservio can help

We hope this blog post provides some valuable insight into what to check for in your review process. If your business needs assistance with defining a Java code review process, our team can help.

Reach out to our team of professionals and learn how we can help you accomplish your goals for quality application delivery.

Источник

Оцените статью