Good students will often seek feedback – or validation that what they are doing is right. This is especially true when they are just starting to learn programming (in fact, it’s probably true of everybody on some level, but that is a whole different post).

As a result, often students will ask whether they are doing something the right way, or the best way, and in programming that’s a very difficult question to answer and depends on the problem that you are trying to solve.

The answer is usually "it depends"

as the problems that we give students are usually, to some extent, artificial and contrived in some cases the answer could be “it doesn’t matter”, or the same as that answer to the question “does it work?”, but that doesn’t really help the student, especially given that the question raises an interesting point. In my opinion a far better answer is “it depends – specifically on what the meaning of ‘best’ is”.

Often programming comes down to compromises. There are a number of qualities that we would like to be associated with our code, fast, efficient, reliable, readable, maintainable, finished on time and so on. Quite often though these concepts are in conflict. One example is that you could cache some results, which might make your program faster, but would use more memory. Making code faster often goes hand in hand with making it more readable.

To combat this whenever I start a significant project I always try to define what the meaning of best is. I pick some aspects that I care about, and discard things I don’t care about. This keeps the concept of what I’m trying to achieve in focus and should stop me getting distracted.

Another thing that I think is worth noting is that in my list of qualities, although I’m really interested in clever ways to make code fast the size and scope of the projects I’m usually involved in means that speed is never an issue. Usually the issues are reliable, readable and maintainable, which begs the question what have I still not learnt to comment my own code?

Advertisements