<!--
title: Organize code for readability
domain: devtools
topic: Code Style
language: Python
source: bazelbuild/bazel
updated: 2024-08-26
url: https://awesomereviewers.com/reviewers/bazel-organize-code-for-readability/
-->

Structure code to minimize complexity and improve readability through logical organization. This includes using early returns to reduce nesting levels and ordering operations in a logical sequence.

Use early returns to flatten code structure and avoid deep nesting:

```python
# Prefer this - early return pattern
if argv[1] != "get":
    eprint("Unknown command ...")
    return 1

# Handle main logic here
...

# Instead of nested if-else chains
if argv[1] == "get":
    # Handle main logic here
    ...
else:
    eprint("Unknown command ...")
    return 1
```

Additionally, organize method calls in logical order where setup operations precede execution operations:

```python
# Setup first
self.ScratchFile('BUILD', [...])
self.ScratchFile('main.cc', [...])

# Then execute
exit_code, _, stderr = self.RunBazel([...])
```

This approach reduces cognitive load by keeping indentation shallow and presenting operations in their natural sequence.
