Recently a post was put up on Code Project that was basically a direct copy of a homework assignment. I don’t answer questions for people’s homework. I have enough trouble trying to hire a good developer as it is without contributing to another generation of useless code monkeys.
Anyway, the question was in two parts. The first part, in summary, was to write some code that calculates powers. e.g. 23=8; 74=2401; etc. by just using simple multiplication. The second part was to solve the same problem but without using a multiplication operator.
This has got me thinking for most of the afternoon, even after watching an episode of Judge John Deed (I’m on holiday). What I was thinking about was the different ways in which I’d tackle the problem. They way I would have tackled the problem when I was a student, and the way I’d do it now.
Then (15 years ago as a student) I’d have created a console application, read in a couple of integers from the keyboard and performed the calculation all in one horrible Main function. Then I would have thrown a few random integers at the command line to satisfy myself that it would work. If it didn’t I’d hack away at it until it did. There wouldn’t be any consistency in the testing as I’d be throwing different integers at it.
Now, because I was curious what the answer was, I did the following: I created a test assembly and wrote some unit tests so that each time I ran the tests it would be the same tests. Then I created a method that performed the calculation. It took two integers, x and y, and calculates xy using the multiplication technique. Then I ran my tests to see if it worked. Once that was working, I re-wrote the method to use the addition only technique. Then I ran the tests again to ensure that I still got the same results.