<!--
title: Structure for readability
domain: devtools
topic: Code Style
language: Rust
source: astral-sh/uv
updated: 2025-07-02
url: https://awesomereviewers.com/reviewers/uv-structure-for-readability/
-->

Organize code to maximize readability and maintainability. When code becomes complex, break it down into smaller, more manageable pieces with clear responsibilities:

1. **Place initialization logic in constructors**: Ensure objects are always in a valid state by initializing them fully in constructors rather than requiring separate initialization methods.

```rust
// Instead of:
struct FilesystemLocks {
    root: PathBuf,
}

impl FilesystemLocks {
    fn from_path(root: impl Into<PathBuf>) -> Self {
        Self { root: root.into() }
    }
    
    fn init(self) -> Result<Self, std::io::Error> {
        // Initialize here...
    }
}

// Prefer:
struct FilesystemLocks {
    root: PathBuf,
}

impl FilesystemLocks {
    fn from_path(root: impl Into<PathBuf>) -> Result<Self, std::io::Error> {
        let this = Self { root: root.into() };
        // Initialize here...
        Ok(this)
    }
}
```

2. **Extract complex expressions**: Assign complex expressions to named variables outside of nested structures to clarify their purpose and make code flow easier to follow.

3. **Create dedicated types**: When functionality around a concept grows complex, consider creating a dedicated type to encapsulate that logic and provide a cleaner interface.

4. **Combine related implementations**: Keep related functionality together by avoiding multiple separate `impl` blocks for the same type when they're logically connected.

Following these principles leads to code that is easier to understand, maintain, and extend.
