1---2name: code-quality-foundations3description: Code quality pillars, goals, abstraction layers, and tradeoff thinking. Use when evaluating code quality, setting quality goals, choosing abstraction levels, making design tradeoffs, or auditing code against quality pillars. Covers readability, modularity, testability, reusability, and the principle of least astonishment. Use when this capability is needed.4---56# Code Quality Foundations78## The Four Goals of High-Quality Code910| Goal | Question to Ask | Failure Mode |11|------|----------------|--------------|12| **It works** | Does it solve the problem correctly? | Bugs, unhandled edge cases, unmet requirements |13| **It keeps working** | Will it survive changes around it? | Brittle dependencies, no tests, hidden assumptions |14| **It's adaptable** | Can requirements change without a rewrite? | Rigid coupling, over-engineering, hardcoded assumptions |15| **It doesn't reinvent the wheel** | Are we reusing proven solutions? | Custom code for solved problems, duplicated effort |1617## The Six Pillars of Code Quality1819| Pillar | What It Means | Key Technique |20|--------|--------------|---------------|21| **Readable** | Other engineers can understand it quickly | Descriptive names, clean layers, consistent style |22| **No surprises** | Behavior matches expectations (POLA) | Explicit contracts, no magic values, no hidden side effects |23| **Hard to misuse** | Difficult to use incorrectly | Type safety, immutability, validated construction |24| **Modular** | Components can be swapped independently | Interfaces, DI, separation of concerns, cohesion |25| **Reusable & generalizable** | Solves problems broadly, not just one case | Focused parameters, generics, avoiding assumptions |26| **Testable** | Can be verified in isolation | Modularity enables testability; design for it from the start |2728**POLA** = Principle of Least Astonishment. If a function does something a reasonable caller wouldn't expect, it's a bug in the design, even if it "works."2930### Modern Additions (2024-2026 Industry Consensus)3132| Concern | Why It's Now Explicit | Original Coverage |33|---------|----------------------|-------------------|34| **Secure** | Shift-left security; threat model at design time | Implicit in "works" |35| **Efficient** | Resource optimization is a design choice, not afterthought | Subsumed under "works" |36| **Reliable** | Explicit error recovery and graceful degradation | Subsumed under "keeps working" |3738## Decision Tables3940### "Should I Abstract This?"4142| Signal | Action |43|--------|--------|44| Same logic appears in 2+ places | Extract to shared function/class |45| Function can't be described in one sentence | Split into smaller functions |46| Class has method groups that use different fields | Consider splitting into separate classes |47| Changing one feature requires touching many files | Improve modularity and layer boundaries |48| Only one use case exists | Wait — don't abstract prematurely |4950### "Where Should I Invest Quality Effort?"5152| Situation | Focus On | Rationale |53|-----------|----------|-----------|54| Greenfield project | Modularity, clean interfaces | Foundation decisions compound; structure early |55| Legacy codebase | Testing, readability | Understand before changing safely |56| High-traffic service | Reliability, efficiency, testing | Production failures are expensive |57| Prototype / spike | Working code, speed | Validate the idea; plan to rewrite |58| Shared library / API | Hard to misuse, no surprises | You can't predict how consumers will use it |59| Security-sensitive code | Security, testability | Breaches are catastrophic and trust-destroying |6061## Checklists6263### Before Writing Code6465- [ ] Problem clearly understood (requirements, constraints, edge cases)66- [ ] Existing solutions checked (libraries, shared code, prior art)67- [ ] Testing strategy considered (how will this be verified?)68- [ ] Key tradeoffs identified (what are we choosing, and what are we giving up?)6970### Code Quality Self-Review7172- [ ] Functions translate to single sentences73- [ ] No surprises: behavior matches names and types74- [ ] Hard to misuse: invalid states are unrepresentable where possible75- [ ] Modular: changes are localized, not scattered76- [ ] Testable: can be unit tested without complex setup77- [ ] Clean abstractions: API doesn't leak implementation details7879---80> Converted and distributed by [TomeVault](https://tomevault.io/claim/smileynet) — claim your Tome and manage your conversions.81<!-- tomevault:4.0:skill_md:2026-04-14 -->