Code Review in Java: What to Check and How to Give Feedback
\nCode review is not just a formality, but one of the most effective ways to improve code quality and raise the professional level of the team. For beginner Java developers, participating in a review can seem daunting: "What if I miss a bug?", "How do I tell a colleague their code isn't perfect?" In this article, we'll break down what to look for when reviewing Java code and how to give feedback that is helpful, not hurtful.
\n\n1. What to Check in Java Code: From Syntax to Architecture
\nCode review is not just about finding typos. A good reviewer looks at the code systematically. Here are the main categories for checking Java projects:
\n\n1.1. Correctness and Logic
\nThe first and main question: "Does the code solve the given problem?" Check:
\n- \n
- Edge cases (null, empty collections, negative values). \n
- The logic of conditional statements and loops. \n
- Correct exception handling (don't use
Exceptionas a universal solution). \n
Example of bad code:
\npublic int divide(int a, int b) {\n return a / b; // Potential division by zero\n}\nWhat to suggest:
\npublic int divide(int a, int b) {\n if (b == 0) {\n throw new IllegalArgumentException("Divisor cannot be zero");\n }\n return a / b;\n}\n\n1.2. Readability and Style
\nJava is a language with established conventions (Java Code Conventions, Google Java Style). Pay attention to:
\n- \n
- Names of variables, methods, classes (should be meaningful). \n
- Formatting (indentation, spaces, line length). \n
- The presence of magic numbers (extract them into constants). \n
Example:
\n// Bad\ndouble x = 3.14159 * r * r;\n\n// Good\nprivate static final double PI = 3.14159;\ndouble area = PI * radius * radius;\n\n1.3. Performance and Optimization
\nFor Java, the following are especially important:
\n- \n
- Avoid unnecessary object creation in loops. \n
- Use
StringBuilderfor string concatenation in loops. \n - Check algorithm complexity (O(n), O(n log n)). \n
Example:
\n// Bad (creates many String objects)\nString result = "";\nfor (int i = 0; i < 1000; i++) {\n result += i;\n}\n\n// Good\nStringBuilder sb = new StringBuilder();\nfor (int i = 0; i < 1000; i++) {\n sb.append(i);\n}\nString result = sb.toString();\n\n1.4. Testability and Tests
\nCheck if the new code is covered by unit tests. Pay attention to:
\n- \n
- The presence of tests for edge cases. \n
- No dependencies on external systems (DB, API) in unit tests. \n
- The readability of the tests themselves (
assertmethod with clear messages). \n
2. How to Give Feedback: Constructively and Without Toxicity
\nEven the most correct code review can fail if the feedback is given aggressively or vaguely. Here are a few rules for beginners:
\n\n2.1. The "Sandwich" Rule
\nStart with something positive, then point out the shortcomings, and end with encouragement. For example:
\n- \n
- Positive: "Great idea to extract the validation into a separate method!" \n
- Critique: "But here it's better to use
Optionalinstead of returning null to avoidNullPointerException." \n - Conclusion: "Otherwise the code is clean, thanks for your work!" \n
2.2. Use "I" Statements and Questions
\nInstead of "You wrote this wrong," say: "I think there might be a bug here with an empty list. What do you think?" This reduces defensiveness and steers the conversation in a constructive direction.