Let's start with some low hanging fruit. Take a look at the main loop of the initial program.

void loop() {
  if (CircuitPlayground.leftButton()) {
    //
    // YES
    //
    for (int p=0; p<10; p++) {
      CircuitPlayground.setPixelColor(p, 0xFF6600);
    }
    CircuitPlayground.playTone(700, 750);
    CircuitPlayground.clearPixels();
  }  
  if (CircuitPlayground.rightButton()) {
    //
    // NO
    //
    for (int p=0; p<10; p++) {
      CircuitPlayground.setPixelColor(p, 0xFF6600);
    }
    CircuitPlayground.playTone(700, 500);
    CircuitPlayground.clearPixels();    
    delay(250);
    for (int p=0; p<10; p++) {
      CircuitPlayground.setPixelColor(p, 0xFF6600);
    }
    CircuitPlayground.playTone(700, 500);
    CircuitPlayground.clearPixels();     
  }
}

It is comprised of two if blocks that check for either a left or a right button press. Then, there's a bunch of stuff that happens if a button is pressed. You should think of that 'bunch of stuff' as one chunk of code. There's a 'bunch of stuff' for what should happen when the left button is pressed, and another 'bunch of stuff' for when the right button is pressed. In each case, that 'bunch of stuff' is what creates the YES and NO indications.

So how about doing something like this?

void loop() {
  if (CircuitPlayground.leftButton()) {
    indicateYes();
  }  
  if (CircuitPlayground.rightButton()) {
    indicateNo();    
  }
}

That should be much more readable compared to the original code. When then just need to implement the two functions indicateYes() and indicateNo(). To do so, we just move (refactor) the code from the original version into the functions.

Here's the complete code with the new functions defined.

///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Yes No v1
//
// One beep (left button) = Yes
// Two beeps (right button) = No
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>

///////////////////////////////////////////////////////////////////////////////
void indicateYes() {
  for (int p=0; p<10; p++) {
    CircuitPlayground.setPixelColor(p, 0xFF6600);
  }
  CircuitPlayground.playTone(700, 750);
  CircuitPlayground.clearPixels();
}

///////////////////////////////////////////////////////////////////////////////
void indicateNo() {
  for (int p=0; p<10; p++) {
    CircuitPlayground.setPixelColor(p, 0xFF6600);
  }
  CircuitPlayground.playTone(700, 500);
  CircuitPlayground.clearPixels();    
  delay(250);
  for (int p=0; p<10; p++) {
    CircuitPlayground.setPixelColor(p, 0xFF6600);
  }
  CircuitPlayground.playTone(700, 500);
  CircuitPlayground.clearPixels();
}
     
///////////////////////////////////////////////////////////////////////////////
void setup() {
  CircuitPlayground.begin();
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  if (CircuitPlayground.leftButton()) {
    indicateYes();
  }  
  if (CircuitPlayground.rightButton()) {
    indicateNo();    
  }
}

And BOOM - that's refactoring. While this is a fairly simple example, hopefully you can see how it can make the code more readable. The loop()now looks very simple - and it should. This is a very simple program.

This guide was first published on Jan 23, 2017. It was last updated on Jan 23, 2017.

This page (Refactor 1) was last updated on Jan 13, 2017.

Text editor powered by tinymce.