Most disgusting code you've seen?

Nov 27, 2011 at 3:35am
What is the most hideous code you've seen, aside from poor spacing?

This is just awful:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx
Nov 27, 2011 at 4:17am
If you consider that hideous, then either you live in a bubble or you are very, very strict. (Don't get me wrong, it has significant design issues, and it uses some pretty MS-ended stuff, but that's about it.) It is well-formated and very readable, at least.

I wish I still had access to the most hideous code I've ever seen. It was some twenty years or so ago. I had obtained some example code to do hidden-line removal (for 3D stuff) in one of the old BASIC dialects (it needed very little massaging to work with GW-BASIC).

However, on inspection, it was clear that the author was a "Real Programmer" -- it was near impossible to analyze its workings without more effort than it was worth. It was also the best example I have ever seen of true "spaghetti code" -- something many of you young ones talk about but have never actually seen.

A good description would be something like: a long list of short, random BASIC commands, without any commentary, liberally interspersed with GOTOs whose targets were bound to (apparently) uninitialized variable values.

What was amazing is that the program worked very well, producing the same, correct output every time.

I wish I could abuse the author by name, but he's probably rearranging the furniture in his nursing home or sleeping right now, so I'll accept that as proper punishment. Just thinking about it now gives me the willies (honest!), so I'm just going to recommend some of the stuff you find at Coding Horror and the occasional true horror found over at The Daily WTF.

This is cute too: http://codinghorrors.com/?c=c&id=1296
Nov 27, 2011 at 5:26am
closed account (1yR4jE8b)
Pretty much everything in libvlc....what a monstrosity.
Nov 27, 2011 at 10:51am

This is just awful:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx


It is not awful. It is just low-level. It is in C.
If it was written in something high level it wood look something like this (pseudocode):

1
2
3
4
5
6
7
if (arg 1 is not given)
  print "Usage: ..."
else if (arg 1 is not a directory)
  print "Not a directory"
else
  foreach file in directory arg 1:
    print file.name


Nov 27, 2011 at 3:13pm
I was referring not to the formatting (which is perfect) but to the way it looks - it's just hard to read.
Nov 27, 2011 at 5:21pm
Pretty much everything in libvlc....what a monstrosity.
Abandon all hope, ye who enter here.
http://www.cplusplus.com/forum/lounge/24239/

It was also the best example I have ever seen of true "spaghetti code" -- something many of you young ones talk about but have never actually seen.
Oh, yeah? One time I worked on an interpreter for a language that had not one, but three unstructured jump constructs. goto was the nicest one.
One was a jump that skipped to the closest line that contained only a tilde. There was another version that jumped backwards. The obvious beauty of it is that you could inadvertently break code by moving it.
The other was skip which basically made a relative jump to some line. This one's even nicer for its unintuitiveness: skip -1 did not jump to the previous line; it jumped back on itself, so it was always an infinite loop.
I've seen scripts using all three at the same time.
Nov 27, 2011 at 5:48pm
closed account (1yR4jE8b)
@helios

Yeah, I started poking around in libvlc after seeing that thread when you first posted it ;)
Nov 27, 2011 at 6:27pm
A game called "The Powder Toy" has literally unreadable code. The main function was 2-3 kLOC alone. I'm not sure if the guy who maintains it now has fixed the old code but in the version I looked at there was a stark contrast between his code (which is very neat and readable) and the code written by the original author (which is really bad).
Last edited on Nov 27, 2011 at 6:28pm
Nov 27, 2011 at 7:09pm
I am not going to lie to you guys but this code was literally the hardest thing I ever had to write.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

void main ()
{
std::printf ("Hai Word!!");
for (int i ; i < 10 ;)
{
std::printf
    ("Hai guys.");
if (i = 3)
{
    printf ("Easter Egg.");
}
while (1){
    for (;;;)
{
    system ("cls");
    printf ("Ima firing mah lazer..");
}
}
}
system ("pause");
return 0;
}
Last edited on Nov 27, 2011 at 7:10pm
Nov 27, 2011 at 7:48pm
My vote goes to the C++ programmers of the early noughties.
The speed freaks who wrote hybrid, hacky C/C++ code.

One such coder wrote a Milkshape3D model loader.
He read the whole binary file in one go, and with minimal error checking.
Then he used structs and a casted pointer to "pull out" the data from memory.

And when he realized compiler-specific optimizations would break his code by aligning the struct data, he simply #defined his compiler-specific anti-optimizations (as any real programmer would do).
Last edited on Nov 27, 2011 at 7:54pm
Nov 27, 2011 at 8:47pm
@helios
You're not exactly a newbie, now...

But yeah, I forgot about VLC -- that thing's code is an abomination.
Nov 27, 2011 at 9:39pm
Duoas wrote:
But yeah, I forgot about VLC -- that thing's code is an abomination.

Such a shame, because VLC is brilliant in that it works with virtually any codec you care to mention.

The media player Banshee (which I adore, except on Windows where it doesn't work at all) has very clean source code. In fact, Banshee was part of the reason I got into C#.
Nov 27, 2011 at 9:57pm
Oof. Now I'm getting VLC flashbacks. I had to write a custom dynamic module and communicate with it by encoding pointers into filename strings just to be able to play video from generic streams. Granted, it wasn't a lot of code, but it's just too roundabout for something that's relatively simple.
Nov 27, 2011 at 10:55pm
I use VLC :(

I could never get Banshee to sync with my iPod, so I had to switch over to rhythm box :/
Nov 28, 2011 at 12:35am
Phfff... this is nothing. hahahahah. :)

I tutor/help-out students at my school (mostly the people who need a programming credit for their non-computer related majors) and I can tell you that some of the code I've seen is just horrific! Besides formatting, some of the code structures I have seen are just horrible, especially with people learning to modularize code and those who have little understanding of mathematical functions. If they created a print function, then that function will do everything but print. I've seen code where people will call on a print function that will in turn call on another print function and that function will call on another function which might be label doCalulation or something and that will do the actual printing.

Want to see UGLY code? Help out a student in your school. You will be truly amazed.
Nov 28, 2011 at 5:04am
closed account (1yR4jE8b)
+1 thepedestrian:
freminds me of looking at my own classmates codes back during my undergrad when we were studying together was horrifying.

I use VLC

I use to, then I discovered SMPlayer which blows it right out of the water and is much more stable and less library dependencies.
Nov 30, 2011 at 8:57am
I've seen code where people will call on a print function that will in turn call on another print function and that function will call on another function which might be label doCalulation or something and that will do the actual print.


Hahaha.. the above applies to real working programmer too. I usually need to maintain programs written by others and for some peculiar reason, some programmers like to wrap layer after layer of functions/classes before reaching the final destination. I guess they took the idea of abstraction to the extreme. I have to peel them off one by one before finally seeing the light at the end of the tunnel.

To me appropriate level of abstraction is fine but to become so obsessed until it is that many layers simply make me want to say enough is enough don't carry it too far! And yes nowadays during discussion when some programmers mention the word "abstraction" make me shudder!
Dec 3, 2011 at 4:23am
[troll]All code that uses streams library instead of stdio is disgusting.[/troll]
I can't remember any code written by others that was particularily gross.
The most disgusting code I ever wrote, possibly this implementation of itoa:
1
2
3
4
5
6
7
8
9
10
11
char * itoa(int i,unsigned base){
	static char s[sizeof(int)*8+2];
	char*t=s+sizeof(int)*8;
	int sgn=i>=0?1:-1;
	i/=sgn;
	do*t--="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz+/"[i%base];while(i/=base,i>0);
	if(sgn<0)*t='-';
	else++t;
	return t;
}

This is very ugly and takes advantage of weird features of arrays, but it is useful.
Or possibly this:
piece deck[7]={{{{5,0},{4,0},{6,0},{7,0}},0x00ffff},{{{5,0},{4,0},{6,0},{4,1}},0xff7f00},{{{5,0},{4,0},{6,0},{5,1}},0x7f00ff},{{{5,0},{4,0},{6,0},{6,1}},0x0000ff},{{{5,0},{6,0},{5,1},{4,1}},0x00ff00},{{{5,0},{4,0},{5,1},{6,1}},0xff0000},{{{5,0},{4,0},{5,1},{4,1}},0xffff00}};
Last edited on Dec 3, 2011 at 4:25am
Dec 3, 2011 at 6:16pm
If you (god forbid) put some spaces in there it might be slightly easier to read.
Dec 4, 2011 at 4:53pm
+1 thepedestrian

Oh hey, darkestfright agrees. Yup, this is almost universally true!
Topic archived. No new replies allowed.