• Quick note - the problem with Youtube videos not embedding on the forum appears to have been fixed, thanks to ZiprHead. If you do still see problems let me know.

Of Doughnuts and Databases

Blue Mountain

Resident Skeptical Hobbit
Joined
Jul 2, 2005
Messages
8,618
Location
Waging war on woo-woo in Winnipeg
A fable.

A small company once hired an intern, and informed him one of his duties was to purchase a dozen doughnuts for the weekly company meeting. On the day of the meeting, the intern went to a local doughnut shop, fortunately located in the same building as was the business, and purchased one doughnut. This he brought back to the company and placed it on a plate in the meeting room. He then went to the doughnut shop a second time, purchased a second doughnut, and again brought it back to the company's meeting room a put it on to the place. A third time he went to doughnut shop ...

The next week he repeated this procedure, making twelve trips to the doughnut shop to purchase the dozen doughnuts for the company meeting.

On the third week, one of the management team asked the intern, "Do you not know that you can purchase all twelve doughnuts at once, and the person at the doughnut shop will put them into a box for you, which you can then bring back to the meeting room? That way, you will save yourself eleven trips to the shop every week. And that approach works for 24, 36, and even 100 doughnuts."

And thus the intern was enlightened.


Strange as it may seem, a scenario similar to this was played out not once but twice these past two weeks. In my job I do the maintenance work on the company's legacy application. Instead of getting doughnuts, the program I'm working on does database calls. In two separate functions, written by separate people, the programmer made hundreds (and in once case, thousands) of separate calls, all SELECT statements that needed to be read, parsed, executed, and returned by the database engine to fetch individual pieces of information. With a bit of foresight, additional programming, and writing better SQL queries, probably well over 95% of these calls could have been eliminated.

I refactored the function that took hundreds of calls and got it down to three. That's right: only three SQL calls to do the work that initially took over 500. In addition, it doesn't matter how much input there is; those three calls do the work if there's only one piece of source data to report on or a hundred. The original code took six calls for every piece of source data. The original programmer has a master's degree in computer science and is now the lead programmer on the new version of our company's core product.

The other piece of code was worse: a simple report required nearly 6,000 separate SELECT statements (again, all of which had to be read, parsed, executed, and returned by the database driver) for reporting on a single year's worth of results. I thought briefly about refactoring it, but to do that properly I'd need to spend probably a day writing and testing a new method in the data model before starting on reworking the code itself. That code was written by a person no longer with the company. He was a decent programmer but had an unfortunate tendency to copy and paste code all over the place.

One sad part of this tale is because the hardware on which we run the system is impressively fast, as is the database program we're using, this horribly inefficient code doesn't seem to be slow. Despite these bogosities I keep turning up in the code base, the product has scaled decently well. We're also a niche product, so we're not having to handle thousands of hits a second on the web site--it's more like hundreds a day.

What do you do if you find bad code in your systems? Fix it? Leave it alone? Analyse to determine if it's worth fixing? Fix the worst of it and leave the rest?
 
Last edited:
Being an end user, I use it.

The problem with in house software in my experience is that the developers at corporate HQ have superfast workstations and the poor bloody infantry in the field have five year old laptops with 512MB of RAM that can't run the stuff in any time frame I'm likely to live through. The developers think I'm being awkward when I point out that running the software is not my job- it's a tool which is supposed to help me to do my job. It does not say "Beta tester" on my job description.
 
Last edited:
The problem with in house software in my experience is that the developers at corporate HQ have superfast workstations and the poor bloody infantry in the field have five year old laptops with 512MB of RAM that can't run the stuff in any time frame I'm likely to live through.
I've seen that, too, but usually on web sites where the developers made no attempt to account for the fact their end user may not be on broadband. I was quite happy with the way I tweaked our web server to send HTML and CSS files to the end user using GZ compression. Between that and light use of JavaScript, the first version of our product ran nearly as well over a dial-up connection as it did with DSL.

Coding for slower lines is not as much of a concern these days, but the JavaScript-heavy web 2.0 sites can put a real strain on an older computer. I tried one day to connect to our site on a old Pentium system (can't recall if it was a I or II). I was able to log in, but when I went to a page that I know made a lot of use of JavaScript the browser choked and became unresponsive.
 
Somewhat as an aside to this and speaking as someone who has looked at a lot of other programmers' code, I think this happens because, in part, too many programmers do not have a clue as to how system software, compilers, hardware implementation and how the hardware actually works. A bit more comprehension of these things would do wonders for optimization and execution speed.
 
Being an end user, I use it.

The problem with in house software in my experience is that the developers at corporate HQ have superfast workstations and the poor bloody infantry in the field have five year old laptops with 512MB of RAM that can't run the stuff in any time frame I'm likely to live through. The developers think I'm being awkward when I point out that running the software is not my job- it's a tool which is supposed to help me to do my job. It does not say "Beta tester" on my job description.
That's my world. Even after I got a PC upgrade to Core 2 Duo and 2 GB of RAM (whoopdedoo!), my computer still constantly stalls such that my word expander (I'm a medical transcriptionist) often doesn't manage to intercept the keystrokes in time to invoke the macro. It's depressing when all one is really running is a [not very] glorified word processing program.

ETA: I should mention that this is definitely an issue with the way they've programmed our proprietary transcription software (combined with the required McAfee antivirus suite). I think if I was running 6 cores with 12 threads the thing would still hiccup on me.
 
Last edited:
... I think this happens because, in part, too many programmers do not have a clue as to how system software, compilers, hardware implementation and how the hardware actually works. A bit more comprehension of these things would do wonders for optimization and execution speed.

It's ultimately a management failure. Personnel are being allowed to perform tasks without adequate training. But this is not a problem unique to the computer field. It happens in all disciplines, just more visibly in some than others. Those where public safety is at risk are the ones with licensing requirements. Some have argued this should happen with programming. That's a tough prospect, however, as 1) things change so fast it would be hard to establish a licensing criteria, and 2) nobody sees it as enough of a problem.
 
A few years back I was working with a conversion team upgrading a utility billing system. One of the coders (pl/sql) wrote this one script that took something like 6-8 hours to run.

I asked him if he could try to optimize it a bit as the whole conversion process would be taking around 24-48 hours to run. He absolutely refused to re-visit his script with the rationale that it worked and also I shouldn't try to change anything becuase I would probably break it.

He was an employee and I was Joe Contractor and so I tried not to cause troubles.

I looked at the script and it was the biggest piece of crap I ever saw. He performed about 20 different checks (each involving a number of poorly coded SELECTs), with a final check to see if even the record should be considered for conversion. Only about 5% of the data would have been selected for conversion. By simply moving the last check to the top, the script went from 8 hours to about an hour. Further refinement brought it down to 10 minutes.

I had to wait for him to be pulled off the project and re-wrote everything he did.

I was totally amazed at his attitude of refusing help. I wasn't being pushy and felt we all had a common goal of a successful project.

When I pointed his crap out (after he left), to the project manager, the manager asked why I didn't come forward earlier. I told him I tried to help him and really didn't want to rat him out.

They were flying this excuse of a programmer over 3 time zones and putting him into a hotel weekly for about 3 months just to write terrible code.

After that I owned the entire conversion process and had a great time.

Charlie (well written code is a beauty) Monoxide
 
In the process of closing out a system, I discovered an error (of omission) that had been in place for about 13 years. I remember creating all sorts of work-arounds to handle that problem, but had never been able to find the cause until now. It just happened that the data here at the very end brought it to light.

Guess who wrote that original code??? In my defense, I doubt anyone else would have been able to spot it. I did port it over to the test system to try out my hunch, but there was no point in fixing it for this legacy, now retired, system.
 
There is a saying I learned from the film industry:

"Always think of the next person down the line."​

In any collaborative endeavor we are always consuming input from someone else and producing output for someone else. Be considerate of the next guy see if you can't make his life a bit easier, just as you would like the person upstream to do for you.

The problem I see is that too many developers are not team players. They think only of what's best for them and their little world. According to those I've spoken with in other industries, this happens in their field too. The difference is their management culture does not permit such attitudes to survive for long. As one person put it, "They quickly learn they're replaceable."
 
What do you do if you find bad code in your systems? Fix it? Leave it alone? Analyse to determine if it's worth fixing? Fix the worst of it and leave the rest?
Like "better", "bad" often lies in the eye of the beholder. I presume by "bad" you mean "algorithmically inefficient", as presented in your fable. Whether that's really bad (in the more common sense) depends on the impact of the inefficiency.

Ultimately, somebody has to analyze to determine if it's worth fixing. That analysis might be trivial, or it might be tricky.

Suppose the old method offers some combination of robustness, reliability, flexibility, or confidence (well tested), and the performance inefficiency is negligible or at least tolerable. It works well enough, so "fixing" it doesn't gain anything you need. Whatever interfaces to it is working with the current interface and implicit side effects, so "fixing" it risks losing something you need.

Consider your fable and its trip-per-doughnut algorithm:

If there's nothing better for that intern to be doing, it works well enough (tolerable performance, confidence).

If the attendees are neither too numerous nor too hungry, it works well enough (tolerable performance).

Individual doughnut specifications can be deferred until one trip-time before consumption-time (flexibility).

The intern keeps fit for his primary role as ringer for the company softball team (side effects).

Subsequent trips can correct initial ordering errors to avoid committing the entire order to a premature and suboptimal doughnut type (robustness, flexibility).

As production pace approaches trip turnaround time, fresher doughnuts can be returned one per trip than can be had by accumulating many to return en masse (robustness? side effect? unrecognized benefit?)
 
Like "better", "bad" often lies in the eye of the beholder. I presume by "bad" you mean "algorithmically inefficient", as presented in your fable. Whether that's really bad (in the more common sense) depends on the impact of the inefficiency.

Ultimately, somebody has to analyze to determine if it's worth fixing. That analysis might be trivial, or it might be tricky.

Suppose the old method offers some combination of robustness, reliability, flexibility, or confidence (well tested), and the performance inefficiency is negligible or at least tolerable. It works well enough, so "fixing" it doesn't gain anything you need. Whatever interfaces to it is working with the current interface and implicit side effects, so "fixing" it risks losing something you need.

Consider your fable and its trip-per-doughnut algorithm:

If there's nothing better for that intern to be doing, it works well enough (tolerable performance, confidence).

If the attendees are neither too numerous nor too hungry, it works well enough (tolerable performance).

Individual doughnut specifications can be deferred until one trip-time before consumption-time (flexibility).

The intern keeps fit for his primary role as ringer for the company softball team (side effects).

Subsequent trips can correct initial ordering errors to avoid committing the entire order to a premature and suboptimal doughnut type (robustness, flexibility).

As production pace approaches trip turnaround time, fresher doughnuts can be returned one per trip than can be had by accumulating many to return en masse (robustness? side effect? unrecognized benefit?)

Don't forget:

The intern will always be able to carry the load of one doughnut. If the order size is allowed to be completely arbitrary, you may experience a sudden unexpected delay one day, only to eventually find the intern buried under an avalanche of doughnuts. If you try to fix this in your instructions to intern 2.0, you may discover that the extra processing power necessary to allow for overload detection may cost more time than the original algorithm, or will require a more expensive intern. [Robustness, marginal cost]
 
I've seen that, too, but usually on web sites where the developers made no attempt to account for the fact their end user may not be on broadband.
RANT!
I'm not on anything. It's a stand alone machine. No broadband, VPN, or phoneline.
Oh sure, there's a network connection on most drilling rigs these days, but that belongs to the customer, not to my employers who are subcontractors.
-And even if the customer would let me plug my laptop into their network, my own IT people won't! Yet I'm stuck with a 1GB database I don't need that keeps trying to go online for updates and to send data back to HQ. All this to do a report I could easily emulate in a single Excel page. What part of this the brains back at base can't get their heads around, I dunno.
 
RANT!
All this to do a report I could easily emulate in a single Excel page.
Ah, technology... all too often a solution in search of a problem.

Somehow I suspect even Excel is a bloated solution to a problem that was long solved handily by a brief telephone conversation with a good drilling secretary. MIRU NUBOP, etc.
 
I well recall the days before we had computers on rigs at all. We used about 0.01% of the paper we do now and the job still got done.
I actually started using my own (386sx) laptop and Canon BJ01 printer for reports long before there were PCs on site, but I noticed immediately that this impressed people with style over substance.
I feel we use the things all wrong- as high tech typewriters mostly- but raising any objection that the king is in his underwear immediately results in cries of "Luddite".
I accordingly keep my mouth shut and seethe internally.
 
One day our bosses decided to hire a consultant to supply where I worked with "forward indicators" to enable us to focus spending better. A group of us were given the task of comparing these indicators with actual trends ...to confirm that the consultant was doing his job, and thus get (handsomely) paid.

The consultant used complex formulae, latest computers and he took a week to give us a months predictions, using data we collected. One bright spark in our section devised a simpler method to check these "predictions"

It reached the stage where our "checking" from the same data available produced a result 2 days earlier than the consultant ..and our results were identical to the consultants.

The Bosses higher up were not amused, they didnt like the fact that young blood using newer thought processes could out perform the "old school" using trusted old time methods.

They were even less amused when funding for the consultant was withdrawn... and he was a mate of the boss who initially recommended him.
 
The general function of a consultant is to pick the brains of regular staff, then present their thoughts to management.
This can occasionally have the advantage that management is finally forced to listen to what their employees have been vainly trying to tell them for years.
 
Somewhat as an aside to this and speaking as someone who has looked at a lot of other programmers' code, I think this happens because, in part, too many programmers do not have a clue as to how system software, compilers, hardware implementation and how the hardware actually works.

The developers at my company took a long time to grasp the concept that if you are making database queries over a network, there will be significant - and unpredictable - time delay.

There's also one developer who couldn't quite grasp that I was unable to remotely connect to her laptop - which was at home and NOT connected to our VPN - to reset her laptop password, but she's a special case. Very special.
 

Back
Top Bottom