Back to all reviewers

Reduce code nesting

getsentry/sentry
Based on 2 comments
Python

Excessive indentation makes code harder to read and understand. Two common techniques can help flatten your code structure: 1. **Use early returns** for guard conditions instead of nesting the main logic in else blocks:

Code Style Python

Reviewer Prompt

Excessive indentation makes code harder to read and understand. Two common techniques can help flatten your code structure:

  1. Use early returns for guard conditions instead of nesting the main logic in else blocks:
# Instead of this:
def increment_group_tombstone_hit_counter(tombstone_id: int | None, event: Event) -> None:
    if tombstone_id:
        # deeply nested logic here
        # ...

# Prefer this:
def increment_group_tombstone_hit_counter(tombstone_id: int | None, event: Event) -> None:
    if not tombstone_id:
        return
    
    # main logic here at the top level
    # ...
  1. Use compound context managers to reduce indentation levels when multiple contexts are needed:
# Instead of this:
with mock.patch(...):
    with self.feature(...):
        with self.tasks():
            with pytest.raises(...):
                # deeply nested code

# Prefer this:
with (
    mock.patch(...),
    self.feature(...),
    self.tasks(),
    pytest.raises(...)
):
    # code at a more readable indentation level

These techniques produce flatter, more scannable code that’s easier to maintain and debug. This pattern is sometimes called “flattening arrow code” as it helps avoid the rightward drift that can make functions hard to follow.

2
Comments Analyzed
Python
Primary Language
Code Style
Category

Source Discussions