Code Review in Java: What to Check and How to Give Feedback

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Code Review in Java: What to Check and How to Give Feedback

\n

Code 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\n

1. What to Check in Java Code: From Syntax to Architecture

\n

Code 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\n

1.1. Correctness and Logic

\n

The 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 Exception as a universal solution).
  • \n
\n

Example of bad code:

\n
public int divide(int a, int b) {\n    return a / b; // Potential division by zero\n}
\n

What to suggest:

\n
public 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\n

1.2. Readability and Style

\n

Java 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
\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\n

1.3. Performance and Optimization

\n

For Java, the following are especially important:

\n
    \n
  • Avoid unnecessary object creation in loops.
  • \n
  • Use StringBuilder for string concatenation in loops.
  • \n
  • Check algorithm complexity (O(n), O(n log n)).
  • \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\n

1.4. Testability and Tests

\n

Check 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 (assert method with clear messages).
  • \n
\n\n

2. How to Give Feedback: Constructively and Without Toxicity

\n

Even the most correct code review can fail if the feedback is given aggressively or vaguely. Here are a few rules for beginners:

\n\n

2.1. The "Sandwich" Rule

\n

Start 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 Optional instead of returning null to avoid NullPointerException."
  • \n
  • Conclusion: "Otherwise the code is clean, thanks for your work!"
  • \n
\n\n

2.2. Use "I" Statements and Questions

\n

Instead 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.

Blogs

Book Recommendations