blufive: (Default)
[personal profile] blufive
Imagine a cartoon. One character reaches into the mouth of another. The arm goes in to the shoulder, they grab hold of something, and PULL the victim inside-out.

I did that to a computer program today - a big forest of nested if-then-elses, 3-4 layers deep. This code gets executed at least 10+ times whenever someone loads a page from the system, and is almost guaranteed to be a performance bottleneck as soon as we get above about 20 simultaneous users.

By the time I was done, 412 loooong lines of code had been reduced to 165 shorter lines, it's faster to execute, and I'm within striking distance of (1) sharing huge slabs of code with another, very similar, function and (2) replacing dozens of string comparisons with an integer-based switch/case.

I appreciate that a large proportion of my readers would understand the above better if it was written in Martian, but I reckon that's a good afternoon's work.

Of course, when I go in tomorrow, I'm likely to find that the overnight build has exploded in flames, and no doubt in three weeks' time we'll find some hideous bug I caused in the process, but...

Date: 2003-07-31 16:35 (UTC)
From: [identity profile] makyo.livejournal.com
Well done, that programmer!

I've done something like that a few times, although not on quite the same scale. Looking through someone else's code, spending an hour trying to understand exactly why they've done something convoluted and complicated, gradually coming to the realisation that actually there isn't any good reason for it, and ripping the entire wodge out and replacing it with a few alternative lines of code. A sort of `cruftectomy', I guess.

It's a nice buzz when it works, though - here's hoping the overnight build remains resolutely unimmolatory :)

nicholas

Date: 2003-07-31 16:49 (UTC)
From: [identity profile] makyo.livejournal.com
Of course, sometimes there is a reason for weird and convoluted code. Someone once told me of an occasion when a clever optimisation had spectacular and unforeseen circumstances. He'd been part of a team working on really optimised real-time embedded assembly code for a car engine-control system. They spent months lovingly crafting carefully-written, hand-optimised code to be burnt onto a chip and installed in a test car. And everything worked brilliantly.

Then the US wing of the company got to work modifying the system for the American market. Some manager noticed that the UK team hadn't been quite as aggressive as they might have been with the use of the sign bit in one of the registers, and fired off a really harsh memo to higher management on both sides of the Atlantic, slating the UK team for this oversight. This obscenely wasteful squandering of valuable bits was duly `corrected', and the system installed in a test car and sent off round the track.

I'm told that everything was fine up to 127 mph, at which point the car promptly flipped over on its back in a rather spectacular and aggravating manner - having abruptly decided that it was now travelling 128 mph in reverse.

nicholas

Date: 2003-07-31 17:23 (UTC)
From: [identity profile] pickledginger.livejournal.com
Hurrah for slimmed-down code!

::crosses fingers::

Date: 2003-08-01 00:39 (UTC)
From: [identity profile] yonmei.livejournal.com
Very cool. Very satisfying. What were you coding in?

Date: 2003-08-01 00:51 (UTC)

Date: 2003-08-01 01:02 (UTC)
From: [identity profile] blufive.livejournal.com
I should point out that there was nothing horrendously bad about the code before I hit it. It could have been better documented, and there was excessive duplication, but it was logically sound, and there were no examples of the kind of baroque constructs, so beloved of one of the consultants who laid the groundwork for the system, which I've found in several other places.

Imagine a branching tree of if-then-elses, but with the same decisions deep in each fork. That's how I "inverted" it - taking one of the deeper if-then statements and pulling it out so it happened first - thereby eliminating a swathe of duplication. I also applied a few "else if" clauses to reduce repeated evaluation of string comparisons.

I'm probably going to have to answer a charge that I made the code MORE convoluted, but I reckon it was worth it for the performance improvement and centralised definition of constants. I think, once people grok it, my new code is actually no more complex than the old code, just very different in its approach to the problem.

Date: 2003-08-01 01:03 (UTC)
From: [identity profile] blufive.livejournal.com
P.S. The build is GREEN!

Profile

blufive: (Default)
blufive

April 2024

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated 2026-03-23 13:57
Powered by Dreamwidth Studios