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-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 22:37
Powered by Dreamwidth Studios