# Brute-force idioms: `== True`, an if/else that returns a boolean, index loops that only read `xs[i]`, `.filter(...).length > 0`, `indexOf(x) !== -1`, `x ? true : false`, `x ? x : y`, `.map().flat()`, `strings.Index(...) != -1`, `.len() == 0`

*Code longer than it needs to be*: one of the ways AI coding agents bloat software, from lessloc's [pattern catalog](https://lessloc.org/patterns/index.md). lessloc catches it as [`simpler`](https://lessloc.org/guide/reference.md#tier-2), built in for Python, JS/TS, Go, Rust, Java and C#. ruff (in every hook) and clippy (in `ci`) go deeper.

## What makes brute-force idioms a bad practice?

The standards that call it a bad practice: Sonar S1125, S1126, S1066, S1155; ESLint no-unneeded-ternary, no-lonely-if; typescript-eslint prefer-for-of, prefer-optional-chain; unicorn prefer-array-some, prefer-array-find, prefer-includes; [PEP 8](https://peps.python.org/pep-0008/); ruff E712, SIM103; staticcheck S1002, S1003, S1008; clippy needless_bool, len_zero.

## What does lessloc do about brute-force idioms?

lessloc catches it as [`simpler`](https://lessloc.org/guide/reference.md#tier-2), built in for Python, JS/TS, Go, Rust, Java and C#. ruff (in every hook) and clippy (in `ci`) go deeper.

- `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.

## Examples

Caught as `simpler`:

```ts [p.ts]
export const has = (xs: number[]) => xs.filter(x => x > 2).length > 0;
export const at = (xs: string[], x: string) => xs.indexOf(x) !== -1;
export const flag = (v: boolean) => (v ? true : false);
```

Written well:

```ts [p.ts]
export const has = (xs: number[]) => xs.some(x => x > 2);
export const at = (xs: string[], x: string) => xs.includes(x);
export const flag = (v: boolean) => v;
```

Caught as `simpler`:

```js [q.js]
export function sum(xs) {
  let s = 0;
  for (let i = 0; i < xs.length; i++) {
    s += xs[i];
  }
  return s;
}
```

Written well:

```js [q.js]
export function sum(xs) {
  let s = 0;
  for (const x of xs) {
    s += x;
  }
  return s;
}
```

Caught as `simpler`:

```js [r.js]
export function isAdmin(u) {
  if (u.role === "admin") {
    return true;
  }
  return false;
}
```

Written well:

```js [r.js]
export function isAdmin(u) {
  return u.role === "admin";
}
```

Caught as `simpler`:

```python [x.py]
def names(users):
    out = []
    for i in range(len(users)):
        out.append(users[i].name)
    return out
```

Written well:

```python [x.py]
def names(users):
    return [u.name for u in users]
```

Caught as `simpler`:

```go [y.go]
package y

import "strings"

func Has(s, x string) bool {
	if strings.Index(s, x) != -1 {
		return true
	}
	return false
}
```

Written well:

```go [y.go]
package y

import "strings"

func Has(s, x string) bool {
	return strings.Contains(s, x)
}
```

## Related patterns

The other patterns in [Code longer than it needs to be](https://lessloc.org/patterns/index.md#code-longer-than-it-needs-to-be):

- [Hand-built counters and flag loops](https://lessloc.org/patterns/counters-and-flags.md): lessloc catches it as `simpler`.
- [A catch that only rethrows](https://lessloc.org/patterns/catch-rethrow.md): lessloc catches it as `simpler`.
- [A promise wrapped in new Promise](https://lessloc.org/patterns/promise-wrap.md): lessloc catches it as `simpler`.
- [Annotating types the compiler infers](https://lessloc.org/patterns/inferred-types.md): lessloc catches it as `simpler`.
- [A React Effect that only sets state from other values](https://lessloc.org/patterns/derived-effect.md): lessloc catches it as `simpler`.
- [Functions with many parameters (8 or more)](https://lessloc.org/patterns/long-params.md): lessloc catches it as `long-params`.
- [Deep nesting](https://lessloc.org/patterns/deep-nesting.md): lessloc catches it as `deep-nesting`.
- [Long functions](https://lessloc.org/patterns/long-function.md): lessloc catches it as `long-function`.
- [If-chains on literal values](https://lessloc.org/patterns/if-chain.md): lessloc catches it as `if-chain`.
- [A file that keeps growing](https://lessloc.org/patterns/swollen-file.md): lessloc catches it as `swollen-file`.
- [Reformatting lines the task did not change](https://lessloc.org/patterns/churn.md): lessloc gives every session a rule against it and catches it as `churn`.
- [Net growth past a budget](https://lessloc.org/patterns/net-growth.md): lessloc catches it as `net-growth`.
- [Golfed code](https://lessloc.org/patterns/golfed-code.md): lessloc catches it as `clever` and `dense-lines`.
