Pattern of the Month: Red Green Refactor
Pattern of the Month: Red Green Refactor
Test-Driven Development, and the Red-Green-Refactor permutation of TDD, is a powerful way to drive efficiency among your Agile team.
Join the DZone community and get the full member experience.Join For Free
Red Green Refactor is the Agile engineering pattern which underpins Test Driven Development. Characterized by a “test-first” approach to design and implementation, it utilizes very short development cycles to minimize leap-of-faith assumptions. This is essentially how it's done:
Step 1: Design a test. Before any implementation work is done, the conditions to be met are specified. What is the expected output or result? This may be asserted by example, e.g. a test for a proposed function sqrt(x) may be designed as sqrt(4) => 2. Edge cases may also be exercised, e.g. sqrt(0) => 0, as may valid variations on terms of interface contract, e.g. sqrt(2.25) => 1.5. Invalid cases may also be tested, such as sqrt(-1) => Error.
Step 2: Run the test. Since no work has yet been done to satisfy the test, it may reasonably be expected that the test would fail were it to be run. A failing test is commonly shown as a red bar in an Integrated Development Environment. However, it must not be assumed that such a test will in fact fail. For example, it is possible that the test may have been incorrectly designed, such that it can “pass” (green) even without the implementation of a solution. Also, the test may pass (green) if the conditions are already satisfied, and additional work does not, in fact, need to be done. Running the test first, in order to verify the hypothesis that there will be a red bar or similar indication of test failure, is the “Red” step in Red-Green-Refactor.
Step 3: Write the code and test again. The missing feature should be implemented so that the test will now pass (green). Note that the “law of parsimony” should apply. Just enough code should be written to satisfy the test and no more. It’s also important to execute all other tests to make sure that the implementation has not caused existing functionality to fail. The code should be revised until all tests execute successfully. This is the “Green” step in Red-Green-Refactor.
Step 4: Refactor the code. Now that the functionality is in place, it can be optimized for improved readability and maintainability. Any expedient design decisions should be revised so that technical debt is not incurred. Better design patterns may be leveraged, and the quality of the code improved as per the team’s coding standards. After the code has been refactored in this way, the tests should be re-run. They should still come up green, thereby indicating that refactoring has not compromised the functional correctness of the implementation.
Thousands of tests may be written and executed during a test-first development initiative. A good test harness will, therefore, exhibit a clean architecture in its own right, and the application of good design principles. These include keeping tests small and focused and executing them regularly, which helps to minimize the leap-of-faith taken when implementing the functionality. Brittle dependencies between tests ought to be avoided, and each test should be predicated on a known state. The team should take the design of the test harness as seriously as the rest of the product architecture, and revise it with the same sense of ownership.
Red Green Refactor is the Pattern of the Month at agilepatterns.org
Red Green Refactor
Intent: First prove that an unmet need has been met without breaking anything else, then optimize the solution
- If it ain’t broke don’t fix it.
- Make it work, then make it fast.
Also Known As:
- Test Driven Development
- Test First
- Fail First
Motivation: Assert that each code change is necessary and fit for its intended purpose.
Structure: A team member creates a test for a change that has not yet been made. The test is run. If it passes (green) then this indicates that the change would be unnecessary, as the desired conditions are already satisfied. If it fails (red), then this proves that the conditions are not yet met. The change can then be made and the test run again. If the change has been implemented successfully, then the test will pass (green). The code is refactored to improve design, performance, or other qualities that may have been impacted by the change. The test is re-run and if this refactoring has not compromised the desired behavior, then the test will still pass. Previously written tests should also be run before any change is committed, as other tested code may have been compromised by the modification.
Applicability: Test Driven Development is applicable to all forms of development. It is also applicable for maintenance and repair. For example, if a flaw is discovered a test can be written that expresses the desired behavior. The test will fail, and a fix then introduced, at which point the test will pass.
Consequences: Test Driven Development is a significant culture change for many teams and throughput may initially drop as the practice beds in. Introducing TDD may be controversial in organizations with a separate QA testing function. However, productivity generally increases in line with improvements in code quality, and reduced rework can be expected. The practice also encourages a better understanding of requirements and their acceptance criteria. An automated test environment is recommended to gain the full benefits of TDD since all tests should be run before any change is committed.
Implementation: TDD is an integral part of the Extreme Programming method. It is also found in some Scrum, SAFe, and DSDM implementations. It is widely aspired to as a best practice in Agile development. Most implementations of TDD are done in a unit testing context. Behavior Driven Development is a variation that uses business-focused acceptance criteria as the tests.
Published at DZone with permission of Ian Mitchell , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.