Friday, May 8, 2015

Practice makes a man perfect - CodeKata

A kata is an exercise in karate where you repeat one particular form, multiple
times, each time making little improvements. The intent behind code kata is
similar.

How did Sachin Tendulkar or Roger Federer become the greatest players in their
sport?

Through practice. They practiced everyday. They did, even after they became
no.1 in their sport. It is an accepted norm to practice in the sports industry.
However, this doesn't happen in the software industry. A software
engineer doesn't practice his profession daily. Everyone thinks  "I write software
daily, so why practice writing more software ??"

Sadly, this is not wise to say. One has to practice .. to get better... to learn ..
to stay relevant .. to stay current

And so begins my journey on Code Kata. Inspired by codekata.com, which has many
interesting problems to practice on. Today, I am attempting to solve problem 1.

Kata01: Supermarket Pricing

Using some of the concepts of DDD, I tried to come up with a ubiquitous language
that might fit the model needed here based on the information presented.

"A super market order contains many items, which depict the products, their
quantity and their billing amount based on a pricing scheme"

This leads to the following model:

    - An order has many items
    - An item has a product, its quantity
    - A product has the pricing scheme used to calculate the amount.
    - A pricing scheme has many types
        - Single e.g. this can of beans costs $0.65
        - Multiple e.g three for a dollar
        - NonCountable e.g $1.99/Pound
        - MultipleWithOffer e.g buy two, get one free

This is shown in the UML below :







































The clear advantage is that calculation of the total bill amount and individual
line item's amount easily fits in a strategy design pattern. Adding more
pricing schemes becomes easy, Software becomes more composable.

Let us now answer the questions posed in the code kata 1 :
   
    - In the case of Multiple pricing scheme, like "three for a dollar", what is
      the price if I buy 4, or 5 pieces?
            A: It is the responsibility of the algorithm in the "Multiple"
            pricing scheme to decide this based on the business rules. for
            e.g. 4th and 5th one will have price of what a single piece would
            have if only one piece was bought.
    - In the case of  NonCountable scheme, like $1.99/pound, what does 4
      ounces cost?
            A: Again calculated by the "NonCountable" algorithm, which would
            have conversion methods to different fractions of the measure and
            also to different units of the measurement. Adding a conversion
            algorithm for a newer unit later would just mean adding a method
            and/or decorator pattern.
    - For MultipleWithOffer, like "buy two, get one free", does the third item
      have a price?
            A: maybe, depends on the business rules.

This was my kata for today, trying to practice my DDD and modelling skills.
I don't know if this approach mentioned is the best way to model the
supermarket pricing. However, I know  practicing daily will surely make me
better at modelling software.

Happy Practicing !!!

No comments:

Post a Comment