examples of bad code (and why) ?

A place to discuss software and code that isn't automotive related. Free and open source preferred but not compulsory.
User avatar
EssEss
LQFP112 - Up with the play
Posts: 244
Joined: Thu Sep 10, 2009 9:23 am
Location: Dayton, OH

Re: examples of bad code (and why) ?

Post 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.
User avatar
sry_not4sale
LQFP144 - On Top Of The Game
Posts: 568
Joined: Mon Mar 31, 2008 12:47 am
Location: New Zealand, land of the long white burnout
Contact:

Re: examples of bad code (and why) ?

Post 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.
Owner / Builder: 1983 Mazda Cosmo 12at (1200cc 2-rotor turbo) coupe [SPASTK]
165hp @ 6psi standard - fastest production car in japan Oct 82
User avatar
EssEss
LQFP112 - Up with the play
Posts: 244
Joined: Thu Sep 10, 2009 9:23 am
Location: Dayton, OH

Re: examples of bad code (and why) ?

Post 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!
User avatar
Fred
Moderator
Posts: 15431
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: examples of bad code (and why) ?

Post 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.
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
User avatar
Fred
Moderator
Posts: 15431
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: examples of bad code (and why) ?

Post 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.
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
User avatar
KW1252
LQFP112 - Up with the play
Posts: 166
Joined: Tue Jan 15, 2008 5:31 pm

Re: examples of bad code (and why) ?

Post 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...
User avatar
EssEss
LQFP112 - Up with the play
Posts: 244
Joined: Thu Sep 10, 2009 9:23 am
Location: Dayton, OH

Re: examples of bad code (and why) ?

Post by EssEss »

I found a great place: http://thedailywtf.com/
User avatar
Fred
Moderator
Posts: 15431
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: examples of bad code (and why) ?

Post 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
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
Peter
LQFP144 - On Top Of The Game
Posts: 268
Joined: Tue Dec 27, 2011 5:37 am

Re: examples of bad code (and why) ?

Post 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
:-p
User avatar
Fred
Moderator
Posts: 15431
Joined: Tue Jan 15, 2008 2:31 pm
Location: Home sweet home!
Contact:

Re: examples of bad code (and why) ?

Post 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
DIYEFI.org - where Open Source means Open Source, and Free means Freedom
FreeEMS.org - the open source engine management system
FreeEMS dev diary and its comments thread and my turbo truck!
n00bs, do NOT PM or email tech questions! Use the forum!
The ever growing list of FreeEMS success stories!
Post Reply