6, 7, and 8 are 5 + 1, 2, or 3

I want to change the code so that 6, 7, and 8 are 5 + 1, 2, or 3.

I have a wee discussion with myself before making any changes and decide to comment out the 6, 7 and 8 elseif’s, change the if i = 5 to if i>=5, and subtract 5 from the i2r, and see how it works. I make these changes and break a lot of the tests. Hmmm. What to do?

  • I turn on the debugger. And the moment I step through the very simple code I see a blindingly obvious keying error that I could have found without the debugger if only I’d looked at the code a little harder.

  • I fix my mistake. I turn off the debugger.

  • I switch to Excel and recalc.

  • All of the tests pass.

  • I remove the code that I’d commented-out.

Public Function i2r(i As Integer) As String

Application.Volatile

i2r = ""

If i = 4 Then

i2r = "IV"

ElseIf i >= 5 Then

i2r = "V"

i = i - 5

End If

While i <= 3 And i > 0

i2r = i2r + "I"

i = i - 1

Wend

End Function

I look at the code and I’m happy with it, but not thrilled, so I look to see if I could refactor it a bit more. But, you know what? I don't feel confident enough to do that so, instead, I decide to add a few test cases to understand the problem better.

In other words, I want to do a bit more analysis and I am going to do it with the code and tests.