A post which originated from observing the class of mistakes folks make when they start out with programming and what it says about the pitfalls that awaits the unsuspecting ones ….

1. Choices Abound

So you know how to write to a file & you know each time how to get it done.

Take a look at the various parameters of functionality (by no means complete) for the basic file writing api’s found in C/C++ on win32 platform. How many of us  have really read through each of those API documentations and the accompanying readme’s when we started out with the first win32 program ?

Read that and catch all those errors and special situations before they happen, for the API you have chosen to do the task.

2. API Trivia aka broken abstractions

Jon Bentley, teaches you how to make back-of-the-envelope calculations in his excellent Programming Pearls text. He describes how he tries to estimate whether a set of n records will fit into his computers memory. He is tripped up however, by the implementation of malloc that he used. Contrary to his assumptions, malloc took up an estimated 48 bytes of overhead instead of a very sane 8 byte value he assumed per allocation.

If even experts get tripped up, what say you of mere mortals like you & me. Well, the key here is that Mr Bentley actually measured what he had created. The point is that you cannot make assumptions or take something for granted. Being thorough as best as time permits is the only way to create excellent code.

Such ‘trivial’ pitfalls abound in this industry & they rarely seem funny when you are the owner of the code which has suffered to ignore this piece of trivia.

3. API side effects (a specialization of leaky asbtractions)

FunkyCar.Inc has developed a new prototype car. It’s so advanced that it can make intelligent decisions for you. For instance if you pressed the brakes, it could

  • Stop your engines if you are breaking too hard
  • Change your engine’s performance parameters depending on the no of times you braked
  • Change your brake’s sensitivity dynamically based on your braking behavior in the past

These in computer parlance, would be the side effects of using the brake, in your funky new car. Imagine if such decisions were made, every time you used some functionality of your car. Driving a car would then be an incredibly more complex experience, depending on the version and the bug fixes applied to its “intelligent” software.

Programming API’s are similar. Only, at times the side effects cannot be even intelligently guessed at unless you know what the implementation looks like. Here are some examples –

An understanding of these side-effects require an understanding of the what and why’s of these side effects. This requires one to further the understanding of the models beneath.

In short, the further you explore, the more you understand and the more complexity you carry around.


Advertisements