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?
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.
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.
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
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.
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.