City Facts Kata (OOP)

_config.yml

Picture attribution:

I was cruising code review, looking for an interesting problem to devour, and the gods delivered:

The code review problem

The problem here is that there is some code which the OP wanted refactored. I must congratulate the author for his initiative. It’s a very good attempt, the author wishes for feedback, and I find that simply amazing. It’s laudable. So I must heap praises on OP – sure – it’s not perfect, but it’s definitely commendable. Here it is:

And here is my refactored version:

The positives:

• It’s organised and very readable. This is key.

• I adopted a polymorphic approach. Notice how the conditionals and the actual behaviour are separated?

Take this example:

	if x == 1
		do_this1()
	end
	if x == 2
		do_this2()
	end
	if x == 3
   		 do_this3()
	end

Notice how the behaviour is tied to the conditional? You can separate that out using polymorphism, so that all behaviour associated with a conditional is located in the one single place. This makes it much easier to read and change when you have nuanced conditionals.

The negatives:

• Infinite loops. I hate them. If possible, avoid it. The only thing worse would be recursions. Ugh.

My friends, I hope you gained something from this post.

As always, keep well, and strive for excellence!

Written on April 5, 2017