Here is the bad news: the new developer you hired has written some terrible, atrocious code. No one can understand what it does.
The good news: at least there are unit tests to prove the code is working.
You job is to refactor the code and make it readable, while keeping the code in working order (pass all tests).
- Clone this repository
- Install the required packages with
docker-compose run composer install
- Run the tests with
docker-compose run phpunit
- Start refactoring!
$ mv docker-compose.yml docker-compose.bind.yml
$ mv docker-compose.mutagen.yml docker-compose.yml
$ docker-compose build app
$ docker-compose up -d app
$ mutagen sync create --name=kata-app --default-directory-mode=0755 ./ docker://kata-app/app
$ docker-compose run composer install
$ docker-compose run phpunit
$ mutagen sync terminate kata-app
$ mv docker-compose.yml docker-compose.mutagen.yml
$ mv docker-compose.bind.yml docker-compose.yml
The primary goal is to refactor the code in src/Algorithm/Finder.php
- as it stands the code is incomprehensible.
- Start with simple rename refactors so you can better understand the abstractions you are working with. Rename any class or any variable.
- Move on to extract methods and making the code more modular.
- See if you can also eliminate switch statements and multiple exit points from methods.
Anything is fair game, create new classes, new methods, and rename tests. The only restriction is that the existing tests have to keep working. Lean on the tests and run them after every small change to make sure you are on the right path.
You can stop when you feel the code is good enough, something you can come back to in 6 months and understand.
I've decided to port the kata directly to PHP 7 instead of 5.6. This will allow you to practice also with the new PHP features. By the way, if you're not used to it yet, here you have some useful resources:
- Refactoring.guru Code Smells catalog
- Refactoring.guru Refactorings catalog
- SourceMaking Refactorings catalog
- Martin Fowler Refactorings catalog
- CodelyTV Refactoring videos (Spanish)
This kata is a PHP port of the original Incomprehensible Finder Refactoring Kata created by K. Scott Allen.
You can also find the kata in different programming languages in isolated repositories just ready to clone and enjoy:
This port has been developed by CodelyTV in order to have it available for the Software Craftsmanship Barcelona Coding Dojo session. Come with us and have some fun if you're near Barcelona the next Monday, August 22nd!