# prefer-with-capacity

Prefer withCapacity over immediate reservation after construction

Constructing an empty collection and immediately reserving capacity splits one initialization across two operations.
Instead, you SHOULD construct the collection with `withCapacity`.

- Category: performance
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
function collect(capacity: usize): ^int32[] {
    let values: ^int32[] = Array.new();
    values.reserve(capacity);

    return values;
}
```

## Accepted

```ds title="main.ds"
function collect(capacity: usize): ^int32[] {
    let values: ^int32[] = Array.withCapacity(capacity);

    return values;
}
```

## Prior art

- [Clippy · reserve_after_initialization](https://rust-lang.github.io/rust-clippy/master/index.html#reserve_after_initialization)

[language/linter/src/rules/performance/prefer_with_capacity.rs:25](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/performance/prefer_with_capacity.rs#L25)
