Page 2 of 3

Re: examples of bad code (and why) ?

Posted: Sun Apr 18, 2010 12:57 am
by EssEss
thats what I'm talking about .. I would have even never though to look there. I never touch db stuff.

any bad code you've seen out there ? I'm going to go out and hit some random oss sites and do some pure browsing for a couple of hours.

Re: examples of bad code (and why) ?

Posted: Mon Apr 19, 2010 11:04 pm
by sry_not4sale
EssEss wrote:something new to ponder:

Code: Select all

/**
 * @public
 * @brief turn on load indicator
 */
static inline void loadLEDOn( void ) {

}
I name funcs like this: [things]+[action]() -> [loadLED]+[On](), but I notice the conflict with how I 'naturally describe it' in the comment.

As I just typed this out moments ago (literally), I remember that Fred pointed this out to me before, but I dont know which way he leans .. or what most people prefer.

btw: this is in a header, hence the @public, even though I marked it static

opinions ?
Personally, when using an acronym in a CamelCase method/var name, I would switch to First letter uppercase only, for example in this case loadLedOn - seems to make things much nicer to read.

Re: examples of bad code (and why) ?

Posted: Mon Apr 19, 2010 11:26 pm
by EssEss
EssEss wrote:thats what I'm talking about .. I would have even never though to look there. I never touch db stuff.

any bad code you've seen out there ? I'm going to go out and hit some random oss sites and do some pure browsing for a couple of hours.
this is harder than I thought.
sry_not4sale wrote:I would switch to First letter uppercase only
thanks for the camelcase tip!

Re: examples of bad code (and why) ?

Posted: Tue Apr 20, 2010 12:42 am
by Fred
Yep, he's right, and I don't follow it! :-o Maybe I'll go about fixing that at some point. The official Java std is as Aaron said.

Re: examples of bad code (and why) ?

Posted: Wed May 19, 2010 5:18 pm
by Fred
More gold, via nhtshot, courtesy egor, written by a "now unemployed paki" LOLOL

Code: Select all

bool variable;
if(variable.tostring().length==4){}
Stunningly bad! :-)

Fred.

Re: examples of bad code (and why) ?

Posted: Thu Jul 08, 2010 10:43 pm
by KW1252
Dallas semiconductors had a real pot of gold in their one-wire sample application... The routine for reading the data stream had something like 15 nested if-then-else statements. It was pretty horrible to debug. I don't have the source code at hand, but I think you can imagine it...

Re: examples of bad code (and why) ?

Posted: Mon Aug 23, 2010 3:23 pm
by EssEss
I found a great place: http://thedailywtf.com/

Re: examples of bad code (and why) ?

Posted: Sat Jun 02, 2012 10:02 am
by Fred
LOTS of shit wrong here, and in so few lines. Readers, see if you can spot some. I'll come back post wrist healing to fill gaps.

Code: Select all

int aircor_eq(int mat)
{
    signed int cor;
    signed char ccor;
    // returns air density correction from equation in % (100 is no correction)
... <snip> ....

        cor = 529700 / (mat + 4597);
// Changed for 3.0.3w to ignore this (unused) setting always use degF internally.
//    }
//    else  {                          // deg C
//        cor = 293200 / (mat + 2732);
//    }
    if (flash5.airden_scaling) { // only if non-zero
        ccor = (unsigned char)cor - 100;    /// <<<<<<< this shouldn't be cast to an unsigned char !!!
        cor = ((signed int)(ccor * flash5.airden_scaling)) / 100;
        cor = cor + 100;
    }
    return (int)cor;
}
Source: http://msextra.com/forums/viewtopic.php?f=91&t=45364

Re: examples of bad code (and why) ?

Posted: Sat Jun 02, 2012 5:12 pm
by Peter
This reminds me of when I first started coding, and I'd get compiler errors about assigning doubles to ints and such. This is when I didn't know the difference between the variable types, and my attitude was fuck you compiler I'll just typecast that shit. Why are they typecasting cor as an int in the return statement? It's already a signed int, and I'm pretty sure typecasting only temporary changes the data type in the previous calculations. I'd hate to see their code after paying good money to install their system on one of my engines.

Code: Select all

#define PWND 2
#define FAIL 1
#define SUCCESS 0
unsigned char aircor_eq(int matF, unsigned int *correctionValue) // The air density correction value should never be negative. Neither should temperature, but their comments say they're working in F so matF. Although I would prefer matK or matR.
{
    unsigned int cor;
    // returns air density correction from equation in % (100 is no correction)
... <snip> ....
    if(matF != -4597){  //As far as I know their MAT calculation code could send me all kinds of strange shit, so we won't be dividing by 0 here. It should probably be limited to realistic values.
        cor = 529700 / (matF + 4597);
    }
    else{
        *correctionValue = 100; // No correction, because something bad is happening somewhere else.
        return FAIL;  // To be handled improperly by MS people.
    }
    if (flash5.airden_scaling_that_returns_an_int) { // only if non-zero
        cor = (((cor - 100) * flash5.airden_scaling_that_returns_an_int) / 100) + 100;  //Integer division, but I'm going to argue that more significant figures is outside the accuracy of our measurements. 
    }
    if(cor > 200 || cor < 50){ // Should be optimized(narrowed) to realistic values.
        *correctionValue = 100; // No correction, because something bad is happening somewhere else.
        return PWND; // Used by MS people to crash the system, this way your engine isn't completely ruined.
    }
    *correctionValue = cor;
    return SUCCESS;
}
In MS3 we already changed the 'correction' curve to 0.1% steps. In a future code release we'll likely merge the built-in calculation and the correction curve.

James
I guess I was wrong about assuming that we don't need more than 1% accuracy for air density. lmao

Re: examples of bad code (and why) ?

Posted: Sat Jun 02, 2012 6:56 pm
by Fred
Peter wrote:

Code: Select all

#define PWND 2
#define FAIL 1
#define SUCCESS 0
return SUCCESS;
LOL @ all of that, but, the last bit would throw an "unreachable code" error ;-)
In MS3 we already changed the 'correction' curve to 0.1% steps. In a future code release we'll likely merge the built-in calculation and the correction curve.
I guess I was wrong about assuming that we don't need more than 1% accuracy for air density. lmao
^ this is another story and another fail, but for different reasons. Discussing it may help them do something right (for a change), so I discourage that :-p