Aids and Tools
Constraint List
Access a comprehensive constraint list for TDD coding challenges. Improve your test-driven development process with TDD Buddy's expert resources.
Pick 1 or 2 to add an extra challenge when doing a kata. The constraints have been gathered from across the web with a few of our own thrown in.
Constraints work because they take away a tool you reach for without thinking.
Once else or a loop is off the table, you have to find another way to express
the same behavior, and that search is where new design options show up. The
goal is not to write production code this way; it’s to widen the set of moves
you can make when you are back in production code.
The Constraints
- Only four lines per method - After refactoring
- Immutables only
- Only one level of indentation per method
- Do not use else
- No passing of primitives between methods as parameters or return types
- No Properties/Getters/Setters
- No loops
- No void returns - All methods must return something
- No conditional statements
- No chained method calls - 1 dot per statement
- No objects
- No mouse
- No debugging
- Only static methods
- Only static fields
- One level of abstraction per method
- Let the code be read as if it was a small story.
- Collapse methods and read only function names top down
- Communicate clearly, meaningful naming taken from the Domain (Kata)
- Only void returns - All methods must return nothing
- Strictly follow the rule of three
- Allow duplication to emerge at least 3 times before removing it
- 2 refactorings after each passing test
- Do not use any classes with more than two instance variables
- Zero constructor arguments
- No more than 2 parameters per method
How to use the constraints
- Start with one. Two constraints that interact (for example no loops and no conditional statements) can turn a short kata into a long one. Add a second only once the first feels manageable.
- Pick a kata you’ve already solved. A familiar problem lets you notice how the constraint changes your solution rather than fighting the problem itself.
- Apply it during refactor, not only green. Several constraints, such as only four lines per method, are meant to be enforced after refactoring. Get to green first, then reshape the code until it complies.
- Debrief. After the session, write down one thing the constraint forced you to do differently and whether you’d use that move without the constraint.
What the constraints train
| Focus | Constraints |
|---|---|
| Small, focused methods | 1, 3, 16, 23 |
| Replacing conditionals with design | 4, 9 |
| Encapsulation and object design | 5, 6, 10, 21, 22 |
| Functional style | 2, 7, 8 |
| Working without your usual tools | 12, 13 |
| Unfamiliar paradigms | 11, 14, 15, 18 |
| Naming and duplication | 17, 19, 20 |
Suggested pairings
- No else or no conditional statements with
Roman Numerals or
FizzBuzz Whiz, where the rules invite long
ifchains. - No primitives between methods with Bank Account or Shopping Cart, so money and quantities become types of their own.
- No loops with Prime Factors or String Calculator, which push you toward recursion or collection pipelines.
- One level of abstraction per method with Bowling Game or Tennis Refactoring, where the scoring rules read best as a short story.