12

They may not be pretty, but they work. Sometimes a best-practice has to take a back seat to 'good enough'. What is the best example of a kludge that you've ever seen passed off in code?

10

Most of computer graphics is a big kludge as doing the "right thing" is usually very expensive. A few examples:

10 accepted

I always loved this one:

// Fixed NPE
try{
...offending code...
} catch (NullPointerException e) {
    // fixed
}

Plain... simple... fixed the "problem", but so not right.

The sad part was that this was in 10 places in the file.

9

It was something along the lines of:

FILE *fh = fopen ("file.in", "r");
if (fh == NULL) fh = fopen ("file.in", "r");
if (fh == NULL) fh = fopen ("file.in", "r");
if (fh == NULL) return -1;

No, I don't know what they were thinking. Maybe another program could deposit that file on the file system really fast.

5

Does Carmack's fast inverse square root count as a kludge?

5

In C code:

system("echo Press Return to continue");

Apparently printf() was busted that day and they needed a workaround, even if it involved a fork and exec of another process or two. :-)

4

In the source to an old version of WebKit:

m_allowFontSmoothing = (nameStr != "Ahem");

Apparently it was some special casing to say they could pass Acid3.

4

Threading is hard, but Application.DoEvents() is real easy!

LoadData();
Application.DoEvents();
PrepareOutputUI();
Application.DoEvents();
// Application.DoEvents();
foreach(var thing in whatever)
{
    Application.DoEvents();
    ProcessThing(thing);
    Application.DoEvents();
}



Application.DoEvents();
3

One example is that it's not unheard of to not release memory when the process is shutting down (even though it's arguably a "best practice" to release what you allocate), and insead let the O/S reclaim it after the process has gone (which is "good enough").

One could also argue that "good enough" is itself "best practice".

2

The code for fetchmail (a command-line mail retrieval tool) had hard-coded special cases for certain popular mail servers that don't comply to standard protocols.

The earliest versions of the Netscape browser (maybe when it was called Mosaic?) did client-side load balancing when you access one of their servers.

ie, the browser would actually go to a random mirror of the site when you tried to access it because the browser was hard-coded to recognize that address.

I, personally, have kept javascript functions in a database table, then spit it out into a page to be evaluated at runtime. And I'm only slightly ashamed of that.

1

GC.Collect(); //Garbage collection C#

Although I've read everywhere you shouldn't need to use it, I've seen memory issues in two applications where calling this method once in a while resolve the memory problems.