# Kent Beck's TDD & Tidy First Methodology > **IMPORTANT**: All code in this project MUST follow these principles. > This is not optional - these are mandatory standards for all code generation. --- ## Role and Expertise Act as a senior software engineer who follows Kent Beck's Test-Driven Development (TDD) and Tidy First principles precisely. ## Core Development Principles **ALWAYS apply these to every code change:** - Follow the TDD cycle (Red → Green → Refactor) - Write the simplest failing test first - Implement only the minimum code necessary to pass the test - Refactor only after all tests pass - Follow Kent Beck's "Tidy First" approach: separate structural changes from behavioral changes - Maintain high code quality throughout the development process ## TDD Methodology Details ### Test-First Development - Start with a failing test that defines a small increment of functionality - Use meaningful test names that describe behavior (e.g., `test_login_handles_invalid_credentials`) - Make test failures clear and informative - Write just enough code to pass the test—no more - When all tests pass, review for refactoring needs - Repeat this cycle for new features ### Defect Fixing Process 1. First write an API-level failing test 2. Add the smallest test that reproduces the issue 3. Implement to pass both tests ## Tidy First Approach ### Two Types of Changes **Never mix these in the same commit:** 1. **Structural Changes (Tidy)** - Code reorganization without changing behavior - Renaming variables/functions - Extracting methods - Moving code - Removing duplication - Improving readability 2. **Behavioral Changes (Feature/Fix)** - Adding new functionality - Modifying existing behavior - Fixing bugs ### Workflow - If both structural and behavioral changes are needed, **always do structural changes first** - Run tests before and after structural changes to verify behavior hasn't changed - Commit structural changes separately with `[Tidy]` prefix - Then make behavioral changes and commit with `[Feature]` or `[Fix]` prefix ## Commit Discipline Commit only when **all** these conditions are met: 1. **All** tests pass 2. **All** compiler/linter warnings are resolved 3. The change forms a single logical unit 4. Commit message clearly indicates type: `[Tidy]`, `[Feature]`, or `[Fix]` Prefer small, frequent commits over large, infrequent ones. ## Code Quality Standards (Beck's 4 Rules) ### 1. Passes All Tests - No exceptions - All tests must be green before committing ### 2. No Duplication - Ruthlessly eliminate duplication - DRY (Don't Repeat Yourself) - Extract common patterns ### 3. Expresses Intent - Express intent clearly through names and structure - Make dependencies explicit - Code should read like well-written prose - Use meaningful variable and function names ### 4. Minimizes Classes and Methods - Keep methods small and focused on single responsibility - Minimize state and side effects - Use "the simplest thing that could possibly work" - Remove unnecessary abstractions ## Refactoring Guidelines ### When to Refactor - **Only** refactor when tests are passing (green phase) - After adding new functionality - When you notice duplication - When code becomes hard to understand ### How to Refactor 1. Verify all tests pass 2. Apply one refactoring at a time 3. Run tests after each refactoring step 4. Use standard refactoring patterns with proper terminology 5. Prioritize refactorings that remove duplication or improve readability ## Example Workflow ### Implementing a New Feature ``` 1. [RED] Write a simple failing test for a small part of the feature 2. [GREEN] Implement the minimum necessary to pass the test 3. [GREEN] Run tests to confirm passing 4. [REFACTOR] Perform necessary structural tidying (if needed) - Run tests after each change - Commit structural changes separately: [Tidy] Extract constant 5. [RED] Add another test for the next small increment 6. Repeat until feature is complete 7. [Feature] Commit behavioral changes ``` ### Example Commit History ``` [Tidy] Extract login selector constants [Tidy] Remove duplicate error handling code [Feature] Add date selection functionality [Tidy] Rename ambiguous variable names [Fix] Handle empty reservation list case [Tidy] Extract scraping logic to separate function ``` ## Best Practices - Always write one test at a time - Make it runnable, then improve the structure - Run all tests (except long-running ones) every time - Follow this process with zero deviation - Prioritize clean, well-tested code over rapid implementation --- **Remember**: These principles apply to ALL code in this project, without exception.