Challenge: How would you start this problem? 7
On Thursday, we’re holding our first Coding Dojo at the recently opened OkC CoCo. This isn’t the first Coding Dojo to happen in Oklahoma City. Some time back, Dave Nicolette held a Randori with a Fishbowl at the OkC Java User’s Group, and that’s the format I’ll be using for this problem. It was a blast then and I’m hoping it’ll be the same this time around.
This first DoJo is with C#, though I hope we manage to use several languages over time. I’d like to sneak in Smalltalk as soon as we have enough of a critical mass so we don’t lose people. I also plan to slowly introduce BDD (maybe not so slowly, who knows – depends on the group).
The recent refactoring exercises (here and here), they come from this problem. We’ll be starting from scratch, so you can probably imagine that those refactoring examples are not going to come up right away (actually probably not at all given the time).
So your challenge this time is not one of refactoring but rather one of an initial value problem. Given the problem statement, how would you go about starting it? I’m assuming TDD, do you make that same assumption? If so, what’s your first test? Your first few tests? What features do you try to tackle first? What questions do you ask yourself about the problem?
I’ve used this problem several times and it’s a great problem to practice many things including:- Test Driven Development
- refactoring (lower case r deliberate – can you guess why?)
- Refactoring to Design Patterns
- Most of the SOLID principles
Anyway, how would you go about starting this problem? Or better yet, give it a try and post your first few tests (in the order you create them).
I’ll be interested in seeing how people start and I’ll compare it to what I’ve done (hint, I use this as a class-driven thing, so I’m pretty flexible on how to start it).
p.s. As a result of studying the manual for my HP 32SII, I have a much better understanding of just how its stack works. I have a EE and CS background, so the stack implementation makes sense, but I left some of its details out of the problem.

When creating a new class / method, i usually write the simplest test that is possible, so that “test fails” means “does not compile” and the test passes after some trivial coding. So my first test was something like this:
Next i tried to get some values into the registers. All the tests first used ints instead of BigDecimal and char, but i refactored later to these types…
After that i started with class OperandStack and tests like these:
These are the first of currently 27 tests beeing now at the point where the second exercise starts.
Sorry for being a little off topic, but isn’t it funny that HP were able to market RPN as a feature, while it’s a way to make the calculator’s design simpler? A typical calculator parses the formula the user enters to create a tree and then executes it on a stack. HP calculators skipped the parsing step and required the user to work directly on the stack. It’s like RPN is the assembly language of calculators. :-)
Martin Vilcans wrote:
Maybe. It IS easier to build (in circuits or software) for the reason you mention. However, RPN has been around since 1920, so I’m not sure if their reason was to make something that was easier to build (maybe) or something that is easier to use (well it is for me, but I love vi as well).
According to the hp museum site, they decided on RPN because of its ability to handle more complex expressions. Given the time-frame (1968), and the weigh (40 pounds), going with a simpler way to represent expressions was a good idea!
And personally, I have a much better time using RPN than infix calculators.
The core of the problem revolves around manipulating the operand stack. Looking at the worked examples in the problem definition, the simplest assertion you can make is that when popping an empty stack, the returned value should be 0, so that is where I started from. The test is at http://randomcode.net.nz/2009/06/22/yet-another-bdd-extension-for-xunit/ as I used it to drive out a BDD style extension for xUnit.
@Test public void ??? shouldContainZeroInEachRegisterAfterCreation() throws Exception { RpnCalculator calc = new RpnCalculator(); assertEquals ??? ?? (BigDecimal.ZERO, calc.getX()); assertEquals(BigDecimal.ZERO, calc.getY()); assertEquals(BigDecimal.ZERO, calc.getZ()); assertEquals(BigDecimal.ZERO, calc.getT());
Given the time-frame (1968), and the weigh (40 pounds), going with a simpler way to represent expressions was a good idea!
what is wrong with me