Originally posted by "JukeboxJim" ...
Deep within the bowls of a large financial institution lurks a monstrous system that was originally written by a single, Highly Paid Consultant. Today, there is a dedicated army of Oompa Loompas needed to support this system. Innocent souls must be routinely sacrificed into the meat grinder to keep the beast operating.
A few unfortunate individuals may know something of this beast. The title is an allusion to the system name. The Objective-C GUI is a work of art, and the ANSI C server code follows the same pattern. Most of the code is normal-bad, cut & pasted, or just plain messed up.
However, I've accumulated some of the finest gems from the beast over the years. These are the gems that make you sit up and ask yourself the same question that has driven scores of maintenance programmers mad over the years: "W.T.F.?!"
While the code is obviously anonymized, the function names were pretty cryptic though. My personal favorite was an 1,800-line function called "doit".
Say which... what?
if (what == 0) { // Code Block A } else if (what == 2) { // Code Block B } else if ((what == 1) || (what == 3) || (what == 5)) { if (what == 1) which = 0; if (what == 3) which = 1; if (what == 5) which = 1; } // Code Block C omitted for sanity/brevity if (what == 5) FooBar(position, 3); else doWibble(position, what); }On second thought, the ommitted Code Block C is good a few laughs, too...
if (manualrequests[position].systemid == 1) { if (strcmp(clients[i].type, "AAA") == 0) { found = 1; temp_fd = clients[i].fd; send_data(1, temp_fd, retstr); } } else if (manualrequests[position].systemid == 2) { if (strcmp(clients[i].type, "BBB") == 0) { found = 1; temp_fd = clients[i].fd; send_data(1, temp_fd, retstr); } } ..... snip ..... else if (manualrequests[position].systemid == 10) { if (strcmp(clients[i].type, "XXX") == 0) { found = 1; temp_fd = clients[i].fd; send_data(1, temp_fd, retstr); } }Well, except for case 3 which actually looks like this:else if (manualrequests[position].systemid == 3) { if (clients[i].g1 != 999) continue; temp_fd = clients[i].fd; send_data(1, temp_fd, retstr); }
If at first you cannot bind; bind, bind then bind again...
When he wrote the network code, the original HPC didn't understand really how TCP works, particularly closing sockets and that whole damn TIMEDWAIT crap. Inevitably, the system became a massive distributed system built around TCP/IP sockets -- which would certainly be what I'd write if I didn't understand sockets -- and offers a lot of interesting network code like this:for(;;) { myport++; if ((myport - LISTENPORT) >= 5) { myport = LISTENPORT; WARNING(func, "bind() failedon all ports from " << LISTENPORT); SLEEP(func, 5, "No idea"); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(myport); if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { std::strstream msg; msg << "bind() to " << myport << " failed with "; if (errno == EADDRNOTAVAIL) msg << "EADDRNOTAVAIL"; else if (errno == EADDRINUSE) msg << "EADDRINUSE"; else if (errno == EBADF) msg << "EBADF"; else if (errno == EINVAL) msg << "EINVAL"; else if (errno == ENOTSOCK) msg << "ENOTSOCK"; else msg << errno; ERROR(func, msg.str()); // close(listenfd); continue; } if ((ret = listen(listenfd, LISTENQ)) != 0) { LOG_WARNING(func, "listen() failed - myport = " << myport); // close(listenfd); continue; } break; }This code block is used in around 20 different processes, in various cut&pasted modified variants. Ironically, one of the reasons why the system was made distributed was for increased reliability and fault tolerance. That is, if you could ever get restarted processes to connect again...
Other gems
There are too many code snippets to fully list here are a few good ones:if ((onoff != 99) && (onoff != 98)) { clients[found].foobarregister[group] = onoff; if (onoff == 0) checkFoobarRegistered(group); } ... static char months[13][4] = {
" ", "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; static char monthslow[13][4] = {
" ", "jan", "feb", "mar", "apr", "may",
"jun", "jul", "aug", "sep", "oct", "nov", "dec"}; ... if ((int_year > 80) && (int_year < 100)) int_year +=1900; if ((int_year >= 0) && (int_year < 20)) int_year +=2000;
The Database
But of course, in a full-scale disaster movie like this, the damage isn't limited to only the code. Invariably, there's a database involved as well. One day, the HPC was thinking deeply...Hmm... how can we store stuff that can be on or off?
Aha - simple:
SELECT * FROM ONOFFTABLE keycode text ------- ----- 0 OFF 1 ONIt gives us just the flexibility we need to add FileNotFound at a future date! Too bad the "text" column is only a VARCHAR2(5).