# prefer-first-last

Prefer first and last accessors over equivalent indexing

`array.at(0)` and `array.at(-1)` duplicate the optional `first` and `last` endpoint lookups.
`array.at(array.length - 1)` also duplicates `last`.
Instead, you SHOULD use the corresponding endpoint accessor.

Trapping subscript access has different empty-array behavior and remains unchanged.

- Category: style
- Level: warning
- Fix: automatic
- Scope: module

## Reported

```ds title="main.ds"
function first(values: int32[]): int32 | undefined {
    return values.at(0);
}
```

## Accepted

```ds title="main.ds"
function first(values: int32[]): int32 | undefined {
    return values.first();
}
```

## Prior art

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

[language/linter/src/rules/style/prefer_first_last.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/prefer_first_last.rs#L8)
