Draft-to-Refactored Module: Introduction

Transcript

Resistor Module: Introduction

In this module we will be working through our next exercise, but instead of starting with existing tests and code, we will start with a list of requirements and write the tests and code ourselves. Once we have our dumbed-out, simplest thing that can work version we will refactor it into something maintainable.

I want you to see how to put this process together into a TDD workflow that really works. I want you to see how I really write code and that yes, I actually begin with a dumbed-out version before refactoring to something maintainable.

Our project is inspired by the ResistorColorDuo exercise from the exercism.io Ruby track, but I’m expanding the requirements into a more full-fledged resistor color bar reader.

If you’ve ever worked with electronics like an Arduino or Raspberry Pi you have almost certainly used resistors.

Resistors come in a wide range of values from a few ohms to several mega ohms. And they are manufactured to varying tolerances.

But they are also tiny. Too tiny to reasonably print these values on in a way that a normal human could be expected to read.

So, instead of numbers, we use color bands to indicate these values.

The other thing you need to know is that there are 4-band resistors and 5-band resistors. It sounds complicated, but this chart clears things up.

There is a 4-band resistor at the top and a 5-band resistor at the bottom.

In both cases, the leftmost bands correspond to the digit component of the value. The only difference between a 4-band resistor and a 5-band resistor is that on a 4-band resistor, the 2 leftmost digits are the value digits and on a 5-band resistor, the 3 leftmost digits are the value digits.

The next color band is a multiplier applied to the value to arrive at the correct order of magnitude.

The final color band, which is usually removed all the way to the right, indicates the manufacturing tolerance of the resistor. It tells you how widely the actual value might vary from the printed value.

Our task is to create an application to translate a series of color bands given as an array of strings into an object that can tell us the value of the resister in milliohms as an integer, the raw value of the digits represented by the first 2 or 3 bands, the value as a human-readable string, the multiplier, and the tolerance. It will also be able to render itself as a human-readable string, “22kΩ ±0.5%”, where 22k is the product of the value ’22’ and the multiplier ‘1k’, and ±0.5% is the tolerance.

Our application needs to handle both types of resistor.

Our Resistor class will be initialized with an array of 4 or 5 color names.

Our Resistor class will provide the following instance methods:

  • #value - returns the value in milliohms: 22,000,000
  • #digits - returns the value digits as an integer: 22
  • #multiplier - returns the multiplier as an integer: 1,000
  • #tolerance - returns the tolerance as a string ‘±0.5%’
  • #human_multiplier - returns the multiplier in human-readable form ‘1k’
  • #human_value - returns the value in human-readable form ’22kΩ’
  • #to_s - returns a string representation of the resistor value with its tolerance ’22kΩ ±0.5%‘

There will also be the following class-level convenience methods:

  • .value - returns the value in milliohms: 22,000,000
  • .human_value - returns a human readable string representing the value ‘22kΩ’

Two examples:

green, blue, yellow, gold results in

  • .value => 560,000,000
  • .human_value => ‘560kΩ’
  • #value => 560,000,000
  • #digits => 56
  • #multiplier => 10,000
  • #tolerance => ‘±5%’
  • #human_multiplier => ’10kΩ’
  • #human_value => ‘560kΩ’
  • #to_s => ‘560kΩ ±5%’

red, orange, violet, black, brown results in

  • .value => 237,000
  • .human_value => ‘237Ω’
  • #value => 237,000,000
  • #digits => 237
  • #multiplier => 1
  • #tolerance => ‘±1%’
  • #human_multiplier => ’1Ω’
  • #human_value => ‘237Ω’
  • #to_s => ‘237Ω ±1%’

Exceptions

We also want to raise errors if there are fewer than 4 colors passed in or more than 5, or if an unknown color is passed. Note, the list of valid colors are different for digits, multiplier, and tolerance.

In the next lesson we’ll write our first test and start driving out the behavior of our application, starting with the tolerance instance method.

Complete and Continue  
Discussion

0 comments