Install Now
Back to Guides

Code Review Examples: Before-and-After Walkthroughs

Jan 16, 2026
Molisha Shah
Molisha Shah
Code Review Examples: Before-and-After Walkthroughs

A high-quality code review example turns vague, frustrating feedback into clear, actionable guidance, and the following before-and-after walkthroughs demonstrate how. By anchoring comments to specific code lines, explaining the rationale, and proposing concrete fixes, reviewers accelerate development, reduce defects, and lift team confidence. Google's Engineering Practices documentation advocates this structured approach as a way to improve code health over time, but it does not present empirical data showing that teams adopting it resolve feedback faster or ship healthier code.

Modern engineering teams repeatedly prove that well-structured code review examples shorten cycle time, shrink bug counts, and boost developer morale. In the following side-by-side walkthroughs, you'll see precisely how targeted, constructive feedback outperforms blanket statements such as "This is confusing."

TL;DR

Code reviews fail when feedback lacks specificity or actionable alternatives. General best practices discussed in Google Engineering Practices, language documentation, and various engineering blogs can inform ways to make code review comments more constructive, even though these sources do not explicitly define validated patterns for converting unhelpful comments into constructive guidance across Python, Java, and TypeScript codebases. Techniques include paired feedback transformations, inline commentary frameworks, and anti-pattern corrections.

Why Specific, Actionable Feedback Transforms Code Review Quality

Atlassian's analysis of 5,000 review comments shows that vague feedback, such as "This is confusing," leads to ineffective reviews, while specific suggestions with code snippets and clear explanations drive productive interactions. Some code review best-practice guides suggest that effective comments explain the "why," offer precise alternatives, and focus on code impact rather than personal critique, but this specific formulation is not stated in Google's official Engineering Practices.

Consider the difference between "This variable name is terrible" and "Could we rename data to userProfiles for clarity when revisiting this code?" The latter accelerates development and preserves team trust, whereas the former stalls progress. Graphite's code review best-practices guide echoes this: actionable feedback plus rationale equals faster resolution and higher code quality.

This guide provides concrete code review examples across Python, Java, and TypeScript, including paired feedback transformations, language-specific snippets, and common anti-patterns. Every sample is grounded in Google's Engineering Practices, official language docs, and authoritative blogs like Baeldung, Real Python, Microsoft, DoorDash Engineering, and GitHub Engineering.

When you layer comprehensive dependency analysis onto your workflow, reviewers can immediately see cross-file impacts and hidden side effects, catching issues that traditional diff-only tools miss. Teams implementing static code analysis alongside manual reviews catch 40% more defects before merge.

Explore how Context Engine identifies review-relevant code patterns →

What Separates a Good Code Review Example from a Poor One?

Good feedback answers three questions: what needs attention, why it matters, and how to fix it. Poor feedback misses at least one of these.

The Transformation Pattern

The following table illustrates how vague comments can be restructured into specific, actionable guidance that developers can immediately apply.

Poor FeedbackProblemImproved Feedback
"This is confusing"No specificity"The nested conditionals in lines 45-62 make the logic hard to follow. Consider extracting validation into isValidPaymentMethod()."
"Wrong formatting"No action path"Our style guide uses 2-space indentation. Run npm run format to auto-fix."
"This will never scale"No mechanism"The N+1 query pattern here could cause issues at 10,000+ items. Consider select_related() to reduce database queries."
"Needs comments"Vague directive"The shipping calculation algorithm isn't clear. Either add a comment explaining the logic or extract it into calculateShippingCost()."

Google's Engineering Practices are sometimes paraphrased as encouraging reviewers to explain not just what is wrong, but why it is wrong, what the implications are, and potentially how to fix it, although this wording does not appear in the official documentation.

Severity Labeling System

Labeling comment severity ("Nit," "Optional," "Critical," "FYI") helps authors prioritize. Google and the Conventional Comments spec recommend explicit prefixes so reviewers' intent is unambiguous:

  • Nit: Minor suggestion; not blocking
  • Optional: Nice-to-have; improves code but not required
  • Critical: Must be fixed before merge
  • FYI: Context only; no action required

For example:

Critical: This endpoint accepts unsanitized user input that's directly interpolated into a SQL query, creating a SQL-injection vulnerability. Use parameterized queries instead. See guidelines: [link]

Nit: Consider renaming process() to validateUserInput() for clarity. Not blocking.

Python Code Review Examples

Python reviews often cover error handling, naming, performance, and security. The following examples demonstrate common issues and their idiomatic Python solutions. For teams looking to accelerate Python development, AI-powered code generation tools can help maintain consistency across reviews.

Error Handling: Embrace EAFP

The Python glossary defines EAFP (Easier to Ask for Forgiveness than Permission) as the preferred Python style over LBYL (Look Before You Leap).

Before (LBYL, fragile):

python
if os.path.exists(file_path):
os.remove(file_path)
else:
print(f"Error: file {file_path} does not exist!")

After (EAFP, robust):

python
try:
os.remove(file_path)
except OSError as error:
print(f"Error deleting file: {error}")

Naming: PEP 8 Compliance

PEP 8 specifies that function names should be lowercase with words separated by underscores.

Before:

python
def calcTotal(basePrice, taxRate=0.05):
return basePrice * (1 + taxRate)

After:

python
def calculate_price_with_taxes(
base_price: float, tax_rate: float = 0.05
) -> float:
return base_price * (1 + tax_rate)

Performance: O(n) Set Deduplication

Before (O(n²)):

python
def remove_duplicates(items):
unique_items = []
for item in items:
if item not in unique_items:
unique_items.append(item)
return unique_items

After (O(n)):

python
def remove_duplicates(items):
return list(set(items))

Security: SQL Injection Prevention

The OWASP SQL Injection Prevention Cheat Sheet recommends parameterized queries as the primary defense.

Before:

python
def login(username, password):
query = f"SELECT * FROM users WHERE username = '{username}'"
cursor.execute(query)

After:

python
def get_user(username):
query = "SELECT username, password FROM users WHERE username = ?"
cursor.execute(query, (username,))
return cursor.fetchone()

Java Code Review Examples

Java reviewers primarily focus on broader concerns like correctness, readability, maintainability, performance, security, and test coverage, with exception handling, SOLID principles, and null-related issues appearing only indirectly or occasionally. These examples highlight patterns that frequently surface during enterprise Java reviews.

Exception Handling: Try-With-Resources

Oracle's Java documentation recommends try-with-resources for automatic resource management.

Before (leak risk):

java
public int getPlayerScore(String playerFile) {
try {
Scanner contents = new Scanner(new File(playerFile));
return Integer.parseInt(contents.nextLine());
} catch (FileNotFoundException noFile) {
throw new IllegalArgumentException("File not found");
}
}

After (auto-close, robust):

java
public int getPlayerScore(String playerFile) {
try (Scanner contents = new Scanner(new File(playerFile))) {
return Integer.parseInt(contents.nextLine());
} catch (FileNotFoundException e) {
logger.warn("File not found, resetting score.");
return 0;
} catch (NumberFormatException e) {
logger.warn("Player file corrupted!", e);
return 0;
}
}

SOLID: Single Responsibility Principle

Baeldung's SOLID guide explains that a class should have only one reason to change.

Before (violates SRP):

java
public class Book {
/* … */
void printTextToConsole() { /* formatting and printing */ }
}

After (separated concerns):

java
public class Book { /* domain logic only */ }
public class BookPrinter {
void printTextToConsole(String text) { /* printing */ }
}

See how comprehensive codebase analysis improves Java review accuracy →

Null Safety: Optional Chaining

Oracle's Optional documentation provides methods for handling potentially null values without nested conditionals.

Before (nested checks):

java
public static boolean isNearLocation(User user) {
if (user != null) {
if (user.getAddress() != null) {
if (user.getAddress().getState() != null) {
String zip = user.getAddress().getState().getPostalCode().getZip();
if (zip != null) {
return storeZipCodes.contains(zip);
}
}
}
}
return false;
}

After (streamlined):

java
public static boolean isNearPhysicalLocation(User user) {
return Optional.ofNullable(user)
.map(User::getAddress)
.map(Address::getState)
.map(State::getZip)
.map(storeZipCodes::contains)
.orElse(false);
}

TypeScript Code Review Examples

TypeScript documentation and common review guides focus on general type safety, strict compiler options, and avoiding any, rather than specifically emphasizing type narrowing, generic constraints, and async error handling as key review topics. The following walkthroughs address patterns that improve type safety and runtime reliability.

Type Narrowing: typeof Guards

The TypeScript Handbook on Narrowing explains how type guards refine types within conditional blocks.

Before (unsafe):

typescript
function padLeft(padding: number | string, input: string): string {
if (typeof padding === "number") {
return " ".repeat(padding) + input;
}
return padding + input;
}

(Compiler still warns in other branches.)

After (safe, same code but explained):

typescript
function padLeft(padding: number | string, input: string): string {
if (typeof padding === "number") {
return " ".repeat(padding) + input; // padding is number here
}
return padding + input; // padding is string here
}

Generic Constraints: keyof for Safety

The TypeScript Handbook on Generics demonstrates using keyof to constrain generic parameters.

Before:

typescript
function getProperty(obj: any, key: string) {
return obj[key];
}

After:

typescript
function getProperty<Type, Key extends keyof Type>(
obj: Type,
key: Key
): Type[Key] {
return obj[key];
}

Async/Await: Proper Error Handling

Before (unhandled):

typescript
async function fetchUserData(id: string) {
const response = await fetch(`/api/users/${id}`);
return response.json();
}

After (robust):

typescript
async function fetchUserData(id: string): Promise<User> {
try {
const response = await fetch(`/api/users/${id}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Error fetching user:', error);
throw error;
}
}

Inline Commentary Techniques

A standardized prefix system (praise, nit, suggestion, issue, question, thought, note, chore) removes ambiguity. The Conventional Comments specification defines each prefix and provides usage examples.

PrefixMeaningExample
praise:Highlight positives"praise: Great use of the builder pattern here."
nit:Minor, non-blocking"nit: Prefer processUserData over process."
suggestion:Actionable, improvement"suggestion: Extract this into a helper method."
issue:Must be fixed"issue: Potential null pointer when user is null."
question:Clarification"question: Why threads here instead of async tasks?"
thought:Future consideration"thought: We might cache this later."
note:Context"note: Chose this pattern for performance reasons."
chore:Maintenance"chore: Update CONTRIBUTING.md with this example."

Question-based comments engage developers:

✅ "Could you help me understand why we're using approach X here?"
❌ "You should do Y."

Common Anti-Patterns and Their Corrections

Recognizing unproductive feedback patterns helps reviewers avoid them. The following examples show how to transform common anti-patterns into constructive guidance. Teams focused on code quality metrics can use these patterns to improve review effectiveness.

  1. Vague Criticism
  • Before: "This code is confusing."
  • After: "This function handles auth, validation, and logging. Extracting validation into validateUserInput() could improve readability and testability."
  1. Demanding Perfection
  • Before: "This violates SRP. Refactor."
  • After: "Current implementation works. If we add more payment types later, a factory pattern will ease extension; worth considering, but non-blocking for this PR."
  1. Knowledge Gatekeeping
  • Before: "This violates the principle we discussed."
  • After: Detailed explanation, risks, and suggested alternative, inviting team discussion.

What to Do Next

Effective feedback demands more than spotting flaws. Google's Engineering Practices do not contain an explicit directive telling reviewers to "explain not just what is wrong, but why, what the implications are, and how to fix it." Before hitting "send," double-check that every comment offers rationale and, when possible, a clear path forward.

Start improving your code review workflow today →

FAQ

Written by

Molisha Shah

Molisha Shah

GTM and Customer Champion


Get Started

Give your codebase the agents it deserves

Install Augment to get started. Works with codebases of any size, from side projects to enterprise monorepos.