Monday, January 23, 2012

Homework 1 Solution

Here is my solution to HW1. Please look at it and compare it with your solution.

We will not be taking off points for "style" issues in this class as our goal is just to learn to write programs that work. However, for those of you that want to get better at programming, notice that if you had the line .replace("e","3") more than once in your program then you were violating the DRY principle (Don't Repeat Yourself). Namely, every piece of knowledge should appear only once in your program.

In this case, the 'knowledge' that e turns into a 3 must appear only once in your program. There are two main reasons for this:
  1. If the knowledge changes in the future you will have to change the code in multiple places. For example, if we wanted e to turn into a 3 or # then we would have to make this change in multiple places. This change might have to be made by a different person from the one who initially wrote the code, so it might not be obvious, and he might just change it in one place, thus introducing a nasty bug.
  2. Because it is harder to change the program, we are now less likely to do so, even if it would be profitable. We humans are lazy, illogically so sometimes. If you can make your program easy to change then you will change it lot more, new features will be added, more bugs will be fixed, more costumers will be pleased, mo' money in your pocket. If your program does not change, it dies.

No comments: