It’s Monday, you could use some motivation


Three days ago I decided to give you some motivation to start doing a code kata (see the Friday Dopamine Dump). Well, if you forgotten, haven’t got any time, had to do something more important and didn’t even try to try, then stop. No excuses! If you really want to do this – just start. Schedule one hour this week, cancel all meeting that time and say your wife that you need man’s time (she will understand, mine did).

I did my homework. You can find the code in piotrpasich/FridayDopamineDump github repository.

Solution and problems

I decided to use two design patterns – the builder to create a concrete bundle of necessary objects to calculate the final price, and the decorator pattern for different price calculations and behaviors as regular price, vat calculations, discounts or the rest mentioned in exercise description.

The heart of my solution is the PriceDirector where I get the price builder from. I thought about the strategy pattern and if it is better to operate on different kinds of prices rules, isn’t it? But after all I decided to stick to the Builder and it might help after quick refactoring.

The concrete builder returns a prices collection with objects extending Price class. After hour, I realized that much better name there would be not Price, but CalculationRule and maybe it’s a good idea to create a collection class to store the rules (which I removed at the beginning, because I thought this is unnecessary). Now, the builder pattern starts to make sense – I would be able to operate on the calculation rules much faster and easier than with strategy. Unfortunately, here’s my time’s up.

First step

This exercise was purposed to learn as much as you could about design patterns – not only about the theory, but also practise. What is more important, if you did the code kata then you’ve done next step to be a better developer, to solve problems faster, to create a better code. Congratulations!

In our small circle of friends we had been doing code kata from time to time, but after one coding session we had at least one or two month of break. But, if you want to win, finish every project, and survive every single code review – you need to train regularly. I do not know what is harder – the first step or to keep discipline and not to miss any training session.

This reminds me a story about Grzegorz Kubicki – a 76 years old man who starts on international running contests, from the shortest distances to marathon, and wins. Why? Because he runs every day.

His first step as a runner was made on retirement. The motivation was to keep himself in good health – as much he run, the lower blood pressure he had. After a few years, he was able to gradually discontinue the pressure treatment. Oh, believe me – if you exercise a lot, then you blood pressure will go down when you at work.

Read more

If you’re craving for knowledge then I can recommend you a presentation of @everzet, an author of Behat, about the third version of this BDD platform. He shows, how to use just few design patterns in the code and make it more extensible, decoupled, maintainable and stable.