Skip to content

Golfed code: nested ternaries, comprehensions with several for clauses, lines over 160 characters ​

Code longer than it needs to be: one of the ways AI coding agents bloat software, from lessloc's pattern catalog. lessloc catches it as clever and dense-lines.

What makes golfed code a bad practice? ​

The standards that call it a bad practice: Google Python Style Guide: "Optimize for readability, not conciseness"; ESLint no-nested-ternary; Sonar S3358.

What does lessloc do about golfed code? ​

lessloc catches it as clever and dense-lines. The counterweight to simpler, so shorter never means cryptic.

  • clever (tier 2): Golfed code: nested ternaries, nested conditional expressions, comprehensions with several for clauses.
  • dense-lines (tier 3): Code lines over 160 characters, long strings and URLs aside.

Examples ​

Caught as clever:

ts
export const label = (n: number) => (n < 0 ? "neg" : n === 0 ? "zero" : "pos");

Written well:

ts
export function label(n: number) {
  if (n < 0) return "neg";
  return n === 0 ? "zero" : "pos";
}

Caught as clever:

python
def flat(grid):
    return [c for row in grid for c in row if c]

Written well:

python
from itertools import chain


def flat(grid):
    return [c for c in chain.from_iterable(grid) if c]

The other patterns in Code longer than it needs to be:

Released under the Apache-2.0 license. · llms.txt