[DRAFT] The 'No Code' Constraint
April 17, 2015
When you’ve done a code kata many times, it helps to find new ways to increase the level of difficulty of the kata. We often introduce constraints, like no method greater than 3 lines of code, no primitive function arguments, no touching the mouse, etc.
I recently realized a new constraint I introduce to teams who have trouble introducing abstractions to their code.
I’m calling it the “No Code”, or Interfaces Only constraint.
The purpose is to help people think through problems on a conceptual level, rather than with data. To help them think through procedures more abstractly.
The constraint has two variants, depending on if you’re working in a dynamic or a statically typed language. For languages like C++/C#/Java/PHP the Interfaces Only approach looks like this:
- You may ONLY define interfaces, traits, and abstract classes.
- If you want to AT THE END, you can add concrete classes that conform to your interfaces and see if your solution can run.
For dynamic languages like Ruby/Python/Perl:
- You may ONLY define modules/namespaces, classes and methods
- You may fill in method bodies ONLY with calls to other methods
- You may NOT put code in those methods that manipulates data
- If you want to at the end, to affirm your design, you may do this, but not during the initial design process
- With no code in the methods, there is no pressure to declare object properties or variables
Notice how PHP is a dynamic language, but will enforce Interface semantics, so it fits this kata to be treated as a statically typed language.
To see how this might apply to a kata like FizzBuzz, consider the following PHP code:
class MyClass { function doSomething() { echo “Hello!\n”; } }