Skip to content

@tank/clean-code

3.1.0

Reusable, modular, performant, readable code via KISS, SOLID, and pragmatic design. Covers code smells, refactoring, KISS/YAGNI, modularity (coupling, cohesion), performance (N+1, data structures, caching), readability (cognitive load). Synthesizes Martin, Ousterhout, Fowler, Metz, Hickey, Beck. Triggers: clean code, refactor, code smell, code review, KISS, SOLID, DRY, YAGNI, over-engineered, simplify, coupling, cohesion, reusable, performance, readability, cognitive load, technical debt, N+1.


name: "@tank/clean-code" description: | Reusable, modular, performant, readable code — guided by KISS, SOLID, and pragmatic design. Covers code smell detection, refactoring recipes, function design, KISS/YAGNI decision frameworks, modularity (coupling, cohesion, boundaries), performance-aware patterns, and readability through cognitive load management. Synthesizes Martin (Clean Code), Ousterhout (A Philosophy of Software Design), Fowler (Refactoring), Metz (Practical OOD), Hickey (Simple Made Easy), Beck (Implementation Patterns), grugbrain.dev.

Trigger phrases: "clean code", "refactor", "code smell", "code review", "code quality", "KISS", "keep it simple", "SOLID", "single responsibility", "DRY", "YAGNI", "over-engineered", "too complex", "simplify", "modularity", "coupling", "cohesion", "dependency", "reusable", "modular", "performance", "readability", "cognitive load", "naming", "function design", "extract method", "technical debt", "maintainability", "is this too abstract", "should I abstract", "N+1", "data structure"

Clean Code

Write reusable, modular code with performance and readability in mind.

Core Philosophy

  1. Code is read 10x more than written. Optimize for the reader.
  2. Complexity is the apex predator. Fight it at every level — function, module, system.
  3. Make the change easy, then make the easy change. Small steps beat big rewrites.
  4. Don't build what you don't need. Defer abstraction until the 3rd occurrence.
  5. Profile before optimizing, but design without obvious perf disasters.

Working Mode

  1. Identify the smallest unit of change.
  2. Confirm tests exist or add a characterization test.
  3. Apply one refactoring at a time.
  4. Re-run tests and reassess complexity.

Quick Smell -> Fix Mapping

SmellFix
Long method (>20 lines)Extract Method
Magic numberExtract Constant
Feature envyMove Method
Primitive obsessionIntroduce Value Object
Data clumpsIntroduce Parameter Object
Switch on type codesReplace Conditional with Polymorphism
God class (>200 lines)Extract Class
Pass-through methodsCollapse layers
Over-abstractionInline, delete unused generics

KISS Decision Framework

Before adding abstraction, answer these in order:

  1. Have I seen this pattern 3+ times? NO -> Stop. Wait.
  2. Is the interface simpler than the implementation? NO -> Shallow module, don't abstract.
  3. Can I explain it to a junior in 5 minutes? NO -> Too complex.
  4. Will it survive 3+ requirement changes? NO -> Premature.
  5. All YES -> Abstract it.

Shallow Module Score: (params + exceptions + concepts) / lines of logic

  • Score > 0.5 -> likely over-engineered.

YAGNI Checklist — before building ANY feature:

  • Is this a CURRENT, validated requirement?
  • Has a user explicitly requested it?
  • Will it ship in the next release?
  • Is cost of delay > cost of building?
  • Any NO -> Defer.

See references/kiss-and-simplicity.md for full decision trees.

SOLID Quick Reference

PrincipleOne-linerSignal it's violated
Single ResponsibilityOne reason to changeClass description needs "and"
Open/ClosedExtend without modifyingEvery new type requires editing existing code
Liskov SubstitutionSubtypes replace base typesSubclass throws "not supported"
Interface SegregationSmall, focused interfacesImplementors stub methods with throw
Dependency InversionDepend on abstractionsBusiness logic imports DB/HTTP libraries

Modularity Checklist

CheckTarget
CouplingData coupling (lowest). Eliminate content/common coupling.
CohesionFunctional (highest). Refactor coincidental/logical.
Depth ratiofunctionality / public API > 10
Dependency directionInward toward business rules
Composition vs inheritanceDefault to composition. Inherit only for true "is-a" with >80% behavior reuse.

See references/modularity-and-design.md for coupling/cohesion hierarchies.

Performance Awareness

Do NOT optimize prematurely. DO avoid obvious disasters:

Anti-PatternSignalFix
N+1 fetchesLoop contains query/API callBatch load or eager load
Nested loops on same dataO(n^2) with large nConvert inner to hash set
Allocation in hot loopObject creation per iterationHoist, reuse, or pool
String concat in loopImmutable rebuildsStringBuilder or join()
Computing unused resultsExpensive call before guardMove guards first
No TTL on cacheStale data foreverAlways set TTL

See references/performance-awareness.md for data structure selection and caching decisions.

Readability Rules

RuleTarget
Function length≤ 20 lines
Parameters≤ 3 (use parameter object above)
Nesting depth≤ 3 levels (use guard clauses)
Cyclomatic complexity≤ 10
Abstraction consistencyAll statements at same zoom level

The 3am Test: Would you understand this at 3am during an outage?

See references/readability-patterns.md for cognitive load scoring and review checklist.

Naming Rules

ElementRuleExample
VariableNounuserCount
FunctionVerbcalculateTotal()
Booleanis/has/can prefixisActive
ClassNounInvoicePrinter
ConstantUPPER_SNAKEMAX_RETRIES

Function Design Rules

RuleTarget
Max parameters3 (introduce parameter object when >3)
Max lines20-30 (exceptions: table lookups, state machines)
Max nesting depth2
CQSQueries return data; commands change state. Never both.

See references/function-design.md for parameter design, CQS, and complexity details.

Anti-Patterns

Anti-patternRemedy
Long parameter listsIntroduce Parameter Object
Flag argumentsSplit into two functions
Shotgun surgeryConsolidate responsibility
Speculative generalityInline or delete unused abstractions
Comments as deodorantRefactor, then comment why
Premature genericsConcrete until 3rd type
Configurable everythingBuild for current need, add config when requested

Reference Index

FileContents
references/code-smells.md10 code smells with detection heuristics, before/after examples, refactoring recipes
references/refactoring-recipes.mdStep-by-step refactoring patterns: Extract Method, Introduce Parameter Object, Replace Conditional, etc.
references/function-design.mdNaming, parameters, CQS, function length, cyclomatic/cognitive complexity, depth vs width
references/kiss-and-simplicity.mdKISS principle, over-engineering detection, YAGNI, complexity budgets, simplicity vs DRY
references/modularity-and-design.mdCoupling types, cohesion spectrum, module boundaries, dependency direction, composition vs inheritance
references/performance-awareness.mdData structure selection, N+1, unnecessary computation, memory, caching, algorithmic awareness
references/readability-patterns.mdCognitive load, visual structure, self-documenting code, narrative flow, review checklist

Command Palette

Search skills, docs, and navigate Tank