views:

324

answers:

5

What code have you written where you were forced to break conventions? You know you were going against best practices but there just seemed no other way around it. What was that code and what made you do it?

+1  A: 

Not so much unconventional code but I worked on a contract where I was coding against the production system for 12 months - Vanilla ASP calling VB6 dlls on SQL Server two thousand and something. So I had to think carefully before doing a CTRL + S.

Ferdeen
+1  A: 

In a word, using goto.

There are circumstances when coding in C where nothing else will do.

Perhaps I'm exaggerating: one circumstance. When wishing to return from a function, possibly from a nest of loops, and you need to be certain that all allocated resources are freed up, and you can't afford to write the same bailout-code over & over, a single goto statement will suffice. Let me illustrate ...

int parseConfigFile( char *filename )
{
  int fd = -1;
  char *mem = NULL;

  fd = open( filename, FLAGS );
  if( fd < 0 ) goto error;

  mem = malloc( BUFFER_SIZE );
  if( NULL == mem ) goto error;

  /* load the file content, etc. */

  return 0;
error:
  if( mem != null ) free( mem );
  if( fd > -1 ) close( fd );
  return -1;
}

More recently languages have garbage collection & exceptions, so this sort of voodoo isn't necessary.

Martin Cowie
I often use goto-s in C/C++ for error handling. It is very convenient when a function allocates many resources in sequence, and if any of these fails, the already allocated resources need to be freed in reverse order.
David Hanak
<pedant alert>'using goto' is two words</pedant alert>
johnc
That is a common use of goto in C.
Ferruccio
+14  A: 

In C/C++ many years back I tried this:

void FillRect (int x, int y, int w, int h);
void DrawRect (int x, int y, int w, int h);

void DrawButton (int x, int y, int w, int h, int state)
{
  (state == Highlighted ? FillRect : DrawRect) (x, y, w, h);
}

and to my surprise it worked as I had intended it to. Only afterwards did it occur to me that this generated fewer opcodes than doing a traditional if/else since the parameters are pushed in common code (once) rather than in each branch (twice).

Skizz

Skizz
Suddenly I feel very sick...... (still +1 for sharing this)
EricSchaefer
+1 Wow. I didn't even realize you could do that.
cletus
They're just pointers to the function, so it should work... yet it would be hard for somebody else to understand your code.
Osama ALASSIRY
Nice, I didn't know you could do that either!
pezi_pink_squirrel
Wow! That's something I never thought I'd see.
Ray Hidayat
A: 

Developing my own string class was probably the worst case of reinventing the wheel. But using std::string I just ran out of memory (this was back when 64 bits OS'es were quite rare; buying a Sun was just too expensive). Now, as it happened my strings had a fairly short average length, they were almost all allocated up front, and none were longer than 255 characters. As a result, I managed to get sizeof(string) down to 8, and use no pool memory for strings up to 6 chars.

MSalters
+2  A: 

I actually used a cross join. True story.

It was between two 100K deep tables, and it was both fast and logically correct to do so. First and only time I've ever seen, let alone used, a cross join and it solved a problem approx 20 times faster than the extant COM+ technique.

annakata