Sunday, November 10, 2019

TDD: Transport Tycoon, Exercise 1


I decided that I wanted to take a quick swing at this as a TDD exercise...

Because what we are being presented is the behavior of some pure function, I used a facade as my test subject -- a single function accepting a string argument and returning an integer.  Behind that facade I can refactor my way towards a fully buzzword compliant domain model, but these behavior tests aren't coupled to the model.

That means, incidentally, that these tests aren't some beautiful enduring artifact that describes the model in exquisite detail; they are disposable scaffolding tests.

Because I wasn't sure where I was going, I simply implemented everything in a straight forward way within the facade.  I went three examples in with `if` statements before I started trying to tease out the implicit duplication of the model.  Eventually, all three branches turned into the same code, and then they were simplified to remove that duplication.

It became clear in working on some of the longer problems that I really wanted to have more confidence in the intermediate state.  That insight led me back to the idea that I wanted a "pure function" that could handle each piece of cargo one at time, so that I could track the evolution of the system at each step.  In theory, such a thing can be created via refactoring, but since I wanted confidence in the implementation, I decided to perform a separate TDD run on just that piece, and then verified that the tests against the original facade continued to pass when I applied the refactoring.

When that refactoring was complete, the implementation behind the facade was broken into two pieces -- a state machine to manage the bookkeeping of my "fleet", and pure function that computed transitions from one state to another.

I deliberately declined to maintain CQS discipline; separating the queries from the commands in my bookkeeping component appeared to be ceremony with no particular payoff in the exercise.


4 comments:

  1. I read the above article and got some knowledge from your article which is about Freight. It's actually great and useful data for us. Thanks for sharing it.
    freight services in miami

    ReplyDelete
  2. Your blog is very valuable which you have shared here about Transport. I appreciate your efforts which you have put into this article and also it is a gainful article for us. Thank you for sharing this article here.
    international freight forwarding

    ReplyDelete
  3. In spite of the fact that harm is strange, it can tragically happen. Thusly it is important that you archive any harms to your vehicle on the bill of filling, when your vehicle falls off the truck, and before you drive it away.In spite of the fact that harm is strange, it can tragically happen. Thusly it is important that you archive any harms to your vehicle on the bill of filling, when your vehicle falls off the truck, and before you drive it away. vail colorado airport

    ReplyDelete
  4. This is a role of agree with due to the fact he's going to invoice the meals he buys to the deliver. Shipping from china

    ReplyDelete