Click here to Skip to main content
15,895,538 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Opinions Pin
leonej_dt28-Jul-10 6:31
leonej_dt28-Jul-10 6:31 
GeneralRe: Opinions Pin
supercat928-Jul-10 6:59
supercat928-Jul-10 6:59 
GeneralRe: Opinions Pin
dybs28-Jul-10 16:29
dybs28-Jul-10 16:29 
GeneralRe: Opinions Pin
SirTimothy28-Jul-10 16:58
SirTimothy28-Jul-10 16:58 
JokeRe: Opinions Pin
johannesnestler2-Aug-10 3:00
johannesnestler2-Aug-10 3:00 
GeneralRe: Opinions Pin
SirTimothy3-Aug-10 15:54
SirTimothy3-Aug-10 15:54 
GeneralRe: Opinions Pin
Lutosław30-Jul-10 2:15
Lutosław30-Jul-10 2:15 
JokeRe: Opinions Pin
SirTimothy30-Jul-10 8:33
SirTimothy30-Jul-10 8:33 
Wow! For loops are neat! I rewrote my code, got rid of the gotos, used for loops, and put in some comments like in the article you linked to. I think it's much improved now. I'm especially proud of my reduction of the number of loops. The main sorting part used to involve two nested loops (one inside the other), but I was able to reduce it to a single loop!

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define ARRAY_SIZE 20

#define PNT(s, l, a) fprintf(std##s, l, a)
#define PNT2LINES(s, l1, l2, e) PNT(s, #l1#e#l2#e, 0)

void print_array(int *array) {
	int x = ARRAY_SIZE;
	char* fmts[] = { "%d, ", "%d\n" };			/* fmts has two strings in it */
	for (--array; PNT(out, ((x == 1) & 1)[fmts], *(array = ++array)), --x > 0;);		/* These for loops are really neat!
I can do a bunch of things all in one line! Do something, test a conditional, do another thing! Or I can leave some of those
out, if I don't need them. */
	for (;;) break;		/* I can even do this! */
}

main() {
	int iarray[ARRAY_SIZE];
	int x, y;
	int ofs = iarray - &ofs;		/* ofs is an offset */
	void (*prnt_func)/* These comment things are pretty neat too. I can add annotations to my code, without having to hide them in variable names
or strings that do nothing. */(int*) = &print_array;
	"Here's how I would have had to annotate code before. It's kind of a pain.";
	srand((unsigned int)time(NULL));
	x ^= x;			/* ^= looks like a duck. I'm going to call it the "quack operator" */
	for(;((y = rand/*om number generator*/(), x < /* I'm a comment in the middle of an expression */ARRAY_SIZE) && ((((x
		= x + 1) - 1)[iarray] = /*number from 0 to 99*/(y - (y / 100) * 100)) || 1));); /* lot's of nested () there! */
	PNT2LINES(out, Before sort, ---------------, \n);
	for((*prnt_func)(&ofs+ofs),y^=y,x&=~x;(y+1>=ARRAY_SIZE&&(x&&!(y=x&=~x)))||y+1<ARRAY_SIZE;(++y,iarray[y]<(y-1)
		[iarray])&&(y[iarray]^=iarray[y-1]^=iarray[y]^=*(iarray+y-1),x|=y)); /* This used to be two loops, when it was with GOTOs,
 but I was able to
 reduce it to one. That must be faster, right? It's also still easy to read, so it's a total win-win! */
	 /* That line had a lot of quack operators! */
	 PNT2LINES(out, After sort, ---------------, \n);
	(*prnt_func)(&ofs + ofs);
}

GeneralRe: Opinions Pin
Lutosław31-Jul-10 2:07
Lutosław31-Jul-10 2:07 
GeneralRe: Opinions Pin
SirTimothy31-Jul-10 4:50
SirTimothy31-Jul-10 4:50 
GeneralRe: Opinions Pin
Lutosław1-Aug-10 21:05
Lutosław1-Aug-10 21:05 
GeneralRe: Opinions Pin
Euhemerus8-Aug-10 1:45
Euhemerus8-Aug-10 1:45 
GeneralAre you sure that it's not null? PinPopular
JeremyBob22-Jul-10 21:39
JeremyBob22-Jul-10 21:39 
GeneralRe: Are you sure that it's not null? Pin
Yusuf23-Jul-10 13:37
Yusuf23-Jul-10 13:37 
GeneralRe: Are you sure that it's not null? Pin
ThatsAlok25-Jul-10 20:42
ThatsAlok25-Jul-10 20:42 
GeneralRe: Are you sure that it's not null? Pin
J4amieC26-Jul-10 6:09
J4amieC26-Jul-10 6:09 
GeneralRe: Are you sure that it's not null? Pin
JeremyBob25-Jul-10 22:38
JeremyBob25-Jul-10 22:38 
GeneralRe: Are you sure that it's not null? Pin
Single Step Debugger27-Jul-10 5:58
Single Step Debugger27-Jul-10 5:58 
GeneralRe: Are you sure that it's not null? Pin
Lutosław30-Jul-10 2:20
Lutosław30-Jul-10 2:20 
GeneralYAVH - Yet another VBA horror Pin
Rob Grainger22-Jul-10 1:13
Rob Grainger22-Jul-10 1:13 
GeneralRe: YAVH - Yet another VBA horror Pin
Oakman22-Jul-10 3:09
Oakman22-Jul-10 3:09 
GeneralRe: YAVH - Yet another VBA horror Pin
Dan Pratt22-Jul-10 3:25
Dan Pratt22-Jul-10 3:25 
GeneralRe: YAVH - Yet another VBA horror Pin
Jeremy Hutchinson22-Jul-10 3:36
professionalJeremy Hutchinson22-Jul-10 3:36 
GeneralRe: YAVH - Yet another VBA horror Pin
supercat922-Jul-10 4:55
supercat922-Jul-10 4:55 
GeneralRe: YAVH - Yet another VBA horror Pin
Rob Grainger26-Jul-10 5:59
Rob Grainger26-Jul-10 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.