What are code katas?
A code kata is an exercise in programming which helps a programmer hone their skills through practice and repetition. The term was probably first coined by Dave Thomas, co-author of the book The Pragmatic Programmer, in a bow to the Japanese concept of kata in the martial arts. As of October 2011,
Dave Thomas has published 21 different katas.
For me Katas are a great way to practice not implementation of code but the process involved in designing and generating code, I can become more of a master and gain a deeper understanding of the tools PHPSpec and Behat in a controlled environment. Explore communication between objects using Mocks and gain more confidence with my process for outside in BDD.
Normally I practice one of the 21 published katas that are simple solutions such as string calculator etc, But more recently I have been exploring how using simple tasks within a framework can impact my working practices. I started to experiment with simple repetitive tasks within Magento creating a very basic module that requires not much implementation other that some basic controller block and configuration but really focused upon how I can use the workflow more to my advantage.
So over the past 4 weeks I have tried a few Katas using Magento that I will document over the coming weeks but to start with I decided to create a very simple module that just added some text to the footer of the site on the homepage. During the experiment I needed:
- Magento 1.9
- Magento Sample data
- BehatMage 3
As with the string calculator kata the task in hand is very simple, return a value from a string. With this kata I decided to do the following:
Depending on the current time of the day display “Good morning”, “Good evening” based on before or after mid day
If the user is logged in add there name to the output
With the recent updates to both BehatMage and MageSpec I wanted to see how code generation could fit into my workflow and how using suites within Behat would work for different actors.
The process I followed was always the same, Describe a failing feature in Behat:
After adding some context to my feature definitions I jumped straight into MageSpec describing the blocks and controller. The code generation took care of creating the module structure for me so I could focus only on how I wanted my objects to behave.
After trying this on average twice a week for four weeks I have found a better flow for me within Magento, Having code generation has helped focus me more on behaviour and less on configuration. When I still had time to work on part 2 with logged in users I found it interesting on different methods to proxy Magento logic away from my code. This resulted in using adapters, Patchwork ( thanks to Allan ).
some might say that introducing a framework into the kata goes against the point of a kata that is to practice a technique over and over. However I feel that this experiment was a success. I will continue to develop this process of practicing TDD/ BDD within a controlled Magento environment as it leads to interesting thoughts about design of code within a highly opinionated framework such as Magento. It has allowed me to explore more modern language implementations from PHP, Traits, interfaces and DI. So possibly I should not call these katas but more meditation and reflection sessions where I seek the true path of enlightenment.
You may also find these related posts interesting: Developer Book Club All hail Xdebug and lets let var dump die A new look and a cleanup of content plus good bye github pages. Docker and Docker Sync