Code Review in Java: What to Check and How to Give Feedback
Code review is not just a formality before merging a pull request. It is a powerful tool for improving code quality, sharing knowledge, and preventing bugs. For beginner Java developers, participating in a review often causes fear: "What if they criticize me?", "What if I miss a critical bug?". In this article, we will break down what to look for when reviewing Java code and how to give feedback that helps rather than demotivates.
1. What to Check in Java Code: A Checklist for Beginners
The first and most important step is knowing what to look for. Don't try to find everything at once. Focus on key areas. Here is a basic checklist for a Java code review:
- Logic and Correctness: Does the code solve the given task? Are there obvious logical errors? Are edge cases handled correctly?
- Naming: Do variable, method, and class names follow Java conventions (camelCase, PascalCase)? Are they descriptive?
int ais bad,int numberOfUsersis good. - Error Handling: Are there try-catch blocks? Are specific exception types used instead of a generic
Exception? Are exceptions silently swallowed (empty catch)? - Performance: Are there obvious issues, such as creating objects inside loops, using string concatenation with
+in a loop (better to useStringBuilder), or unnecessary method calls? - Testing: Is the code covered by unit tests? Do they test only positive scenarios or also negative and edge cases?
Example of bad code that should be caught during review:
public void processData(List data) { for (Object item : data) { String s = ""; for (int i = 0; i < 1000; i++) { s += item.toString() + i; // Potential performance issue } System.out.println(s); }}What's wrong here? Using + in a loop creates many intermediate String objects. It's better to use StringBuilder. Also, there is no null check for data.
2. How to Give Constructive Feedback
The most challenging aspect of code review is not the technical check, but communication. Your goal is not to show who is smarter, but to help the team grow. Here are a few rules:
- Focus on the code, not the author. Instead of "You wrote terrible code," say "This method could be simplified by using the Stream API."
- Use a questioning format. Instead of statements, ask questions. Example: "Why was ArrayList chosen here instead of LinkedList? Given the frequent insertions at the beginning of the list, LinkedList might be more efficient."
- Explain the "why". A simple "Fix this" doesn't work. Write: "Let's replace
==with.equals()for comparing strings, because==compares references, not content." - Praise good code. If you see an elegant solution, point it out. "Excellent use of the Builder pattern here — the code has become much more readable."
Example of a bad comment: "This is crap code, rewrite it." Example of a good comment: "I think this block could be extracted into a separate private method to improve the readability of the main method. What do you think?"
3. Common Mistakes by Beginners in Java and How to Highlight Them
Knowing common mistakes will help you find them faster and give more relevant advice. Here are the top 3 mistakes often seen in reviews by beginner Java developers: