Ten Reasons Why I Hate C
Okay... so maybe I actually hate obfuscation in programming... but C really lets you create incomprehensible code better and faster than any other language. And I'm not really sure there are ten reasons here... it was so confusing that I lost count.
At my new job, I was asked to analyze some code and find out how it worked so we could make some changes (please note that the names and places have been changed to protect the guilty). The code in question manages the display of several user screens and responds to input from those screens. All of them have a similar layout, so it should be easy to figure out... right?
I knew I wanted to study and eventually modify the ShowSprocket display, and I found that there was a specific .c file for this display. In fact, there is a separate .c file with a group of functions for each distinct user display-- hey! That makes sense! I think I'm getting the hange of it!
Now all I need to do if find out where this displayShowSprocket() function is called, and I'll know how the whole system works. Let's see... just a simple grep... WHAT? NOBODY calls it? I must not have all the actual source code. No, I'm wrong... after several hours of searching and asking questions I discovered that I do have all the code.
After a few more days of analysis, I finally had a wafer-thin understanding of how this system managed the user displays.
- Each user display has a group of functions that are the same from display to display. They also define a struct which includes a state transition table, and void function pointers to its functions.
- In a .h file there is an enormous array of pointers to this struct type which is initialized called bigDisplayDescriptorArray[] (a much more useful name than the real one, of course!).
- Naturally, the order of the entries in this array is critical, and is tied to a great big enum where the ordinal position of each known user display is defined. Conspicuously absent is the big comment block which says "STOP! DO NOT CHANGE THE ORDER OF THIS STUFF WITHOUT CHANGING THE ORDER OF THE BIG ARRAY!!!"
- Actually, there are two arrays-- one that points to structs for when the user display is a large one, and one for when the user display is smaller in size. At run time, another pointer (displayDescriptorArrayPtr) to this array type is initialized to point to either the small display array or the large display array.
So... in the end, a master display management function uses a pointer to an array of pointers to descriptor structs which contain void function pointers to the functions it needs to call to present a particular display page to the user. Did you get all that?
One Way to get In Shape
I've always been the kind of person who couldn't really exercise regularly unless I had some sort of goal to work toward. Emily (my eldest daughter at 13) has been disappointed that her school (Arizona School for the Arts) has no sports programs, and she's been hoping to do something athletic on her own time. Well, we both got what we asked for!
On May 4th, Emily and I are competing (and that's a really loose usage of the word) in the Tempe Town Lake Triathlon. We also paid a non-trivial fee to be trained for the event by 1st Triathlon, a training program with certified trainers in all three disciplines (swimming, cycling, and running). We will attend training and lecture sessions at least three times a week, and are required to do additional training on our own.
Hopefully I'll be able to keep up with Em!
Main Page |
Lunches |
Current Mail |
E-mail Us! |
Discussions |
Books |
Referenced Sites
Copyright © 2003, Paul Wren. All rights reserved.
Last modified 02/06/2003.
Webmaster