Dense Implementation
Compression tactics
Apply in order until the code is short and clear:
- Use the platform — stdlib, framework helpers, language syntax sugar.
- Collapse data — table-driven logic, lookup maps, config objects.
- Inline — one-use helpers that don't clarify intent.
- Generalize — one function with parameters vs near-duplicate copies.
Before / after patterns
Manual loop → declarative
# verbose
out = []
for x in items:
if x.active:
out.append(x.name.upper())
# dense
out = [x.name.upper() for x in items if x.active]
Branch chain → lookup
// verbose: long if/else if chain on enum
// dense
const LABELS: Record<Status, string> = { ok: "OK", err: "Error" };
return LABELS[status] ?? "Unknown";
Null checks → optional chaining / early return
// verbose: nested if (a && a.b && a.b.c)
// dense
return a?.b?.c ?? defaultVal;
Repeated setup → spread / destructuring / small pipeline
Prefer chaining over temp variables when each step is obvious.
When NOT to compress
Stop compressing if any of these become worse:
- Error messages or debugging
- Public API contracts
- Security checks (auth, validation, SQL)
- Non-obvious business rules
Extract a named function when the compressed one-liner needs a comment to understand.
Density test
For each proposed block ask:
Can I delete a line without changing behavior?
If yes, delete it. If the block is still >15 lines, look for a built-in or existing project helper first.
Pair with
smart-code-core for principles, reuse-before-create before adding helpers.