The Bowling Kata Anti-IF Challenge

"Have you ever noticed how many IFs have been used in Bob Martin's Bowling Kata solution? Is this an effective solution from an evolutionary design perspective?"

Damir, a participant of the "Software Production Coaching You & Me", asked Francesco Cirillo how he would have developed that same kata in an evolutionary way and without applying the "IF Strategy". In this Challenge, Francesco will show his solution.

Here you can find the next sessions of this course series:

The Bowling Kata: defusing the "IF Strategy"

About this Software Development Challenge

Francesco's Software Development Challenges are journeys in which Francesco, you and the other participants will develop one or more features of a real application. Francesco will lead the development of the feature to be created and show you how he makes software design decisions, how he applies principles and tools, why he chooses one pattern over another and why he prefers certain practices in specific contexts.

This Challenge was born out of a request from Damir, one of my best students, in the Software Production You and Me Coaching Programme.

One day Damir asks me, "I would like to see how you would develop the Bowling Kata."

  1. I admit that I have never been very interested in Katas. They are usually tiny exercises and I am interested in developing other muscles. Like introducing a complex feature into a software system that has already reached a certain "age." These are the muscles I like to train and to do this I need complex features and complex software systems.
  2. I admit that I did not know of the Bowling Kata. This Kata is about developing an application that can calculate the score of a bowling match. Robert Martin provided a solution in 2005 to demonstrate how he applies TDD.
    Robert Martin's solution is considered by many as a "procedural solution." The result of applying the "IF Strategy" several times to deal with new tests. I understand the reason for my client's request.
	public int score() {
		int score = 0;
		int frameIndex = 0;
		for (int frame = 0; frame < 10; frame++) {
			int frameIndex = 0;
IF=>		if (isStrike(frameIndex)) {
			score += 10 + strikeBonus(frameIndex);
			frameIndex++;
IF=>		} else if (isSpare(frameIndex)) {
      				score += 10 + spareBonus(frameIndex);
					frameIndex += 2;
IF=>		} else {
					score += sumOfBallsInFrame(frameIndex);
					frameIndex += 2;
      		}
      	}
      	return score;
      }
      

I do a bit of Googling to see alternative solutions:

  • Most of the solutions repeat Martin's steps and generate the procedural solution.
  • Several people are surprised by the absence of an abstraction that emerged through his TDD solution and propose the introduction of a Frame class.
  • Matteo Vaccari and Antonio Carpentieri in their "Open/Closed Principle Dojo" propose a number of new interesting user stories (ie 20 frames instead of 10, 3 rolls per frame instead of 2 and other interesting stories). They show the IF tangle that Martin's solution would become by implementing those stories in continuing Martin's way of doing TDD.
	public int score() {
		int score = 0;
        int currentRoll = 0;
        int numFrames = isMartian() ? 12 : (isVenusian() ? 11: 10);
		for (int frame = 0; frame < numFrames; frame++) {
IF=>		if (callistoVariant() && isLastFrame(frame) {
        		while(isStrike(currentRoll, frame)) {
                	score += 10 + sumOfTwoRolls(currentRolls+1);
                    currentRoll++;
                }
IF=>		} else {
IF=>            if(isStrike(currentRoll)) {
                	score += 10 + sumOfTwoRolls(currentRolls + 1);
                    currentRoll++;
                }
            }
IF=>		} if (isSpare(currentRoll)) {
      			score += 10 + rolls[currentRolls+1];
				currentRoll += 2;
IF=>		} else if (isMartian() {
				score += sumOfThreeRolls(currentRolls);
				currentRoll += 3;
IF=>		} else {
				score = sumOfTwoRolls(currentRolls);
				currentRoll += 2;
			}
		}
      	return score;
      }
      
	public boolean isStrike(int currentRoll, int frame) {
IF=>	if (isVenusian()) {
			return rolls[currentRoll] == frame;
		}
		return rolls[currentRoll] == 10;
	}

Note

In the comments of this Blog Post you can find the design solution that Matteo seems to suggest: an implementation of the Chain of Responsibility Design Pattern.

  1. I admit that I never liked bowling. And maybe I never understood its rules either. That's why my client has to repeat the rules for calculating the score of a game to me several times. I pretend to understand.

What are the objectives of this Challenge?

  1. To solve the Bowling Kata:
    1. Developing the initial requirements presented by Bob Martin.
    2. Developing the requests proposed by Matteo Vaccari.
    3. Developing some other requirements that we can't even imagine right now.
  2. To apply an evolutionary and adaptive design process:
    1. In particular, since the first time this Kata was done in a TDD way, we will use TDD.
  3. Obviously we will not apply the "IF Strategy" to deal with change, complexity and growth.

Ready to work together?

—Francesco Cirillo

Who is it for?

Ideal for Software Developers and Technical Coaches. Helpful for Product Developers and Business Coaches interested in understanding how to organise requirements and user stories and see how to turn them into effective working code.


Episodes


About the Trainer

Francesco Cirillo

Francesco Cirillo has worked at the forefront of the software industry for more than 30 years.

In a career spanning startups, corporations and freelance consulting, he has mentored thousands of professionals, developers, managers and software teams.

In the 1990s he worked as a Senior Consultant and Mentor for Sun Microsystem, expert in Software Architectures and Software Development Processes and Java and Object-Oriented Evangelist.

Francesco is one of the pioneers of Agile Methods. He started working on XP and TDD in 1999. He was chosen by Kent Beck to develop the XP community in Europe and as reviewer of the second edition of the book Extreme Programming Explained.

In 2000 ha created XPLabs, the first company in Europe to offer comprehensive services on XP: software development, training and consulting.

In 2007 he created the Anti-IF Campaign to raise awareness in the international community of software developers to apply good design principles to grow software in a sustainable way.

Since 2008 Francesco stopped actively working on Agile Methods and started to develop RRP, a new method of software development.

In 2013 he created Cirillo Consulting based in Berlin to offer professional training and consulting on productivity and software development.

Francesco invented the Pomodoro® Technique, a renowned time-management method used by millions of people all over the world, while being a university student looking for a way to get more done in less time.

Read the Francesco Cirillo's complete profile.


Upcoming Sessions

Here you can find the next sessions of this series:

Can't make it on any of our dates?

We can discuss other options with you


Testimonials


Useful Resources

The Accounting Adventure Journal - Episode 01: The Challenge

The following is the report of the first session of the Accounting Adventure. The time is 31 March 2021 at...

The Accounting Adventure Journal - Episode 02: Emergent Design

The following is the report of the first session of the Accounting Adventure. The time is 27 April 2021 at...


FAQ

Do you have a question not listed here?

Pomodoro® Web App

The Pomodoro® Timer Web App will be your time tutor and help you stay focused so you can get more...

Pomodoro® Sheets

To help you get started, check out the official templates of the Pomodoro Technique that you can download and print....

Pomodoro® Internal Process

Time is limited. We can't have as much as we want, neither stop it nor slow it down. When our...

Pomodoro® Core Process

Interruptions and procrastination are one of the most dangerous productivity killers. How to deal with the Predator while we're working...