Scary Refactorings

Franziska Sauerwein

@Singsalad

Scary pumpkin 2 CC-BY-SA André Koehne 

https://commons.wikimedia.org/wiki/File:Scary_pumpkin_2.svg

The nightmare

Confusing Traffic sign, Boston MA CC-BY-SA NNECAPA Photo Library

https://www.flickr.com/photos/nnecapa/2868248691

The horrifying start 😨

 public void roll(int roll) {
        System.out.println(players.get(currentPlayer) + " is the current player");
        System.out.println("They have rolled a " + roll);

        if (inPenaltyBox[currentPlayer]) {
            if (roll % 2 != 0) {
                isGettingOutOfPenaltyBox = true;

                System.out.println(players.get(currentPlayer) + " is getting out of the penalty box");
                places[currentPlayer] = places[currentPlayer] + roll;
                if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12;

                System.out.println(players.get(currentPlayer)
                        + "'s new location is "
                        + places[currentPlayer]);
                System.out.println("The category is " + currentCategory());
                askQuestion();
            } else {
                System.out.println(players.get(currentPlayer) + " is not getting out of the penalty box");
                isGettingOutOfPenaltyBox = false;
            }

        } else {

            places[currentPlayer] = places[currentPlayer] + roll;
            if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12;

            System.out.println(players.get(currentPlayer)
                    + "'s new location is "
                    + places[currentPlayer]);
            System.out.println("The category is " + currentCategory());
            askQuestion();
        }

    }

Taking some steps

Reveal intent

Manual exploratory testing

Golden Master

Encapsulate external dependencies

System tests

Refactoring conditionals

Where are we?

public void executeMove(int roll) {
   print(players.get(currentPlayer) + " is the current player");
   print("They have rolled a " + roll);

   if (inPenaltyBox[currentPlayer]) {
       boolean rollIsOdd = roll % 2 != 0;

       if (rollIsOdd) {
          playerGetsOutOfPenaltyBox();

          moveTheCurrentPlayer(roll);

          askTheNextQuestion();
       } else {
          playerDoesNotGetOutOfPenaltyBox();
       }
   } else {
       moveTheCurrentPlayer(roll);

       askTheNextQuestion();
   }
}

How much effort did we put in?

Let's go back

The Start and Finish Line of the "Inishowen 100" scenic Drive CC-BY-SA Andrew Hurley 

https://www.flickr.com/photos/andrewhurley/6254407253 

Why are we doing this?

How did we get here? 😕

 public void roll(int roll) {
        System.out.println(players.get(currentPlayer) + " is the current player");
        System.out.println("They have rolled a " + roll);

        if (inPenaltyBox[currentPlayer]) {
            if (roll % 2 != 0) {
                isGettingOutOfPenaltyBox = true;

                System.out.println(players.get(currentPlayer) + " is getting out of the penalty box");
                places[currentPlayer] = places[currentPlayer] + roll;
                if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12;

                System.out.println(players.get(currentPlayer)
                        + "'s new location is "
                        + places[currentPlayer]);
                System.out.println("The category is " + currentCategory());
                askQuestion();
            } else {
                System.out.println(players.get(currentPlayer) + " is not getting out of the penalty box");
                isGettingOutOfPenaltyBox = false;
            }

        } else {

            places[currentPlayer] = places[currentPlayer] + roll;
            if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12;

            System.out.println(players.get(currentPlayer)
                    + "'s new location is "
                    + places[currentPlayer]);
            System.out.println("The category is " + currentCategory());
            askQuestion();
        }

    }

Where do we actually want to end up?

SOLID

Let the types emerge

Roll

Penalty Box

Player

Position

Gold Coins

Board

Category

Move

Questions

But wait - do we actually need it?

@AdiBolboaca: "Legacy code is fear"

Uncertainty is fear 😧

Learn

Teach

Invest

Thank you!

slides.com/franziskasauerwein

Scary Refactorings

By Franziska Sauerwein

Scary Refactorings

  • 2,219