Then and Now

 

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.

Tags:

3 Comments

  1. barryd says:

    Now, did it work with negative powers? 😉

  2. I should have been clearer. The inputs are all *positive* integers.And anyway, I refer you to my P5 (I think) teacher who told me that minus numbers don’t exist. She and I had a bit of an argument. She asked if you have 3 and you take away 5 what do you have, and I said minus 2. She wouldn’t accept that for some strange reason.

  3. Gary Short says:

    Negative numbers don’t exist eh? Wish she calculated my overdraft then LOL

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s