Artikel van Bart Bons 2004-06-14

Terug naar de basis - Deel 1

In this series of articles I will take you back to the basic principles of programming. For some of you these principles might be childishly obvious. If so, great! Game on. The problem is that I still see a lot of programmers who don't know of or don't follow these basic principles.

Principle 1 - Clarity over speed

Clearly-written code has a higher priority than faster-performing code. First off, clearly-written code normally doesn't imply that your code is slower.

If you are in a situation where you need to change your nicely designed code into less-clearer code to gain a performance boost, think again. Especially in large software projects or public software where the software lasts longer than the underlying hardware, changing your code to faster code is of temporary value. If you need a faster performing software system, then upgrade the hardware. In most cases it is much cheaper to buy faster hardware than having a programmer maintaining fast, but o so difficult to understand, source code.

Principle 2 - Let the compiler find your typing errors

Make sure you write your code in such a way, that the compiler finds all (or most) typos in your source code. Let me give you an example. In the following code you see a comparison based on character values and a comparison based on more strongly typed variables.

Wrong

var 
   PrintType: char;

if PrintType = 'P' // for Printing
then <do_some_printing>
else 
if PrintType = 'R' // for pReview
then <do_some_previewing>

Right

type 
   TPrintType = (ptPrint, ptPreview);

var 
   PrintType: TPrintType;	

if PrintType = ptPrint 
then <do_some_printing>
else 
if PrintType = ptPreview 
then <do_some_previewing>

Why is the first example wrong? Because using a Char variable in this case, is asking for trouble. You simply have to much values that are valid (the whole alphabet for starters).

With the declaring of a new type (TPrintType) and a smaller set of valid values (ptPrint and ptPreview) you are creating a safenet against those little but hard to find typos.

For example, the compiler has no problems with a typo like if PrintType = 'O'. Your program will likely fail to work, but your compiler happily swallows and compiles the code. On the other hand, a typo like if PrintType = ptPront will cause the compiler to choke and stop promptly.

Although the 'Create new type' method requires a bit more code, it will save you time and money in the long run.

Sometimes you have to use a Char variable or Integer variable in a comparison. For example when you need to interact with 3rd-party modules or software and the function only accepts the weakly typed parameters. The only thing you can do is create a wrapper function in which the weak variables get translated (back and forth) to strong variables. In this way you keep the translation close to the problem function.

At the risk of stating the obvious, I found it necessary to write an article about the basic rules of programming. It is the first, but surely not the last in this series. Hope you enjoyed it...


Bart Bons houdt zich voornamelijk bezig met het ontwikkelen van business software, webservices en open standaarden.

recente artikelen
Stay Focused

– stay focused –

FeedCentral: RSS index + API webservice

2007-05-01 Onlangs heeft FocusFriends een negental nieuwe websites gelanceerd. Deze...   meer

Debster in Boomers

2005-07-06 6 juli kwam alweer de 4e editie van Boomers uit - het lijfblad van de...   meer

Debster in De Ster

2005-05-22 In De Ster , 'de grootste krant van Maastricht', verscheen onlangs een kort...   meer

Overzicht van alle artikelen