# Rebuilds what the standard library or framework has: counters, flag loops, CSV, date math, argument parsing

*Code that already exists*: one of the ways AI coding agents bloat software, from lessloc's [pattern catalog](https://lessloc.org/patterns/index.md). lessloc gives every session a rule against it and catches it as [`simpler`](https://lessloc.org/guide/reference.md#tier-2) and [`reinvented`](https://lessloc.org/guide/reference.md#tier-2).

## What makes rebuilding what the standard library or framework has a bad practice?

The standard that calls it a bad practice: Bloch, *Effective Java* Item 59: Know and use the libraries.

## What does lessloc do about rebuilding what the standard library or framework has?

lessloc gives every session a rule against it and catches it as [`simpler`](https://lessloc.org/guide/reference.md#tier-2) and [`reinvented`](https://lessloc.org/guide/reference.md#tier-2). The working standard puts the standard library and the platform before new code. [`simpler`](https://lessloc.org/guide/reference.md#tier-2) catches hand-built counters, flag loops and `range(len())`. [`reinvented`](https://lessloc.org/guide/reference.md#tier-2) catches deep clone, UUID, debounce, slugify, CSV and flag parsing written by hand, and retry loops where the project already retries.

- `simpler` (tier 2): Brute-force code with a shorter idiom: `== True`, an if/else that returns the condition, an index loop that only reads `xs[i]`, `.filter(...).length > 0`, a catch that only rethrows, a flag loop that is `any()`, a hand-built counter, an `__init__` that `@dataclass` writes, a React Effect that only sets state. ruff and clippy add more.
- `reinvented` (tier 2): A hand-written version of what the standard library or an installed package does (deep clone, UUID, debounce, slugify, CSV, flag parsing), or a retry loop where the project already retries.

## Examples

Caught as `reinvented`:

```js [ca.js]
export function deepClone(o) {
  return JSON.parse(JSON.stringify(o));
}
```

Written well:

```js [ca.js]
export function snapshot(o) {
  return structuredClone(o);
}
```

Caught as `reinvented`:

```python [cb.py]
def deep_copy(obj):
    return {k: v for k, v in obj.items()}
```

Written well:

```python [cb.py]
import copy


def snapshot(obj):
    return copy.deepcopy(obj)
```

## Related patterns

The other patterns in [Code that already exists](https://lessloc.org/patterns/index.md#code-that-already-exists):

- [Reimplementing a helper the project already has](https://lessloc.org/patterns/reuse-helper.md): lessloc gives every session the rule: take what is already in the codebase before new code.
- [Copying a rule another function owns](https://lessloc.org/patterns/copied-rule.md): lessloc catches it as `repeated-literal`.
- [Keeping old and new versions side by side](https://lessloc.org/patterns/side-by-side.md): lessloc gives every session the rule: one implementation per rule.
- [Copy-pasted test cases instead of one parametrized test](https://lessloc.org/patterns/copied-tests.md): lessloc catches it as `copied-tests`.
- [A second library for a job an installed one already does](https://lessloc.org/patterns/second-library.md): `new-dependency` names the library that is already installed.
- [The same data shape in several layers (DTO, model, entity)](https://lessloc.org/patterns/duplicate-shapes.md): `copy` catches identical type bodies.
- [The same CSS declarations under another selector](https://lessloc.org/patterns/css-copy.md): lessloc catches it as `style-copy`.
