▲ | nathane280 7 days ago | |
For the lazy: ### Reduce Cognitive Load By: *1. Simplify Conditionals* ``` // High cognitive load if val > someConstant && (condition2 || condition3) && (condition4 && !condition5) // Low cognitive load isValid = val > someConstant isAllowed = condition2 || condition3 isSecure = condition4 && !condition5 if isValid && isAllowed && isSecure ``` *2. Use Early Returns* ``` // Nested ifs if isValid { if isSecure { doStuff() } } // Early returns if !isValid { return } if !isSecure { return } doStuff() // Happy path is clear ``` *3. Prefer Deep Modules* - *Deep module*: Simple interface, complex implementation (e.g., UNIX I/O with 5 methods) - *Shallow module*: Complex interface for simple functionality - Few deep classes > Many shallow classes *4. Use Self-Describing Values* ``` // Numeric codes requiring mental mapping 401 // expired token? 403 // insufficient access? // Self-describing { "code": "jwt_has_expired" } ``` *5. Apply DRY Carefully* - Don't create abstractions too early - Avoid tight coupling between unrelated components ### Avoid These Anti-Patterns: *1. Inheritance Chains* ``` AdminController extends UserController extends GuestController extends BaseController Use composition instead ``` *2. Too Many Layers* - Unnecessary abstraction layers add indirection, not simplicity - Only add abstractions when you need actual need to *3. Framework Magic* - Keep business logic framework-agnostic - Use frameworks as libraries, not containers for your logic - New developers shouldn't need months to learn framework "magic" |