Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Opinion Haver
Apr 9, 2007

ymgve posted:

Also bad code: Using floating point numbers for representing money.

I seem to recall the MONEY type being specially designed to avoid that sort of stuff, but I'm not 100% certain

Adbot
ADBOT LOVES YOU

Zombywuf
Mar 29, 2008

yaoi prophet posted:

I seem to recall the MONEY type being specially designed to avoid that sort of stuff, but I'm not 100% certain

That only works if your calculations are using the MONEY type and that your code contains lots of comments about rounding.

Zombywuf fucked around with this message at 15:05 on Dec 4, 2009

zootm
Aug 8, 2006

We used to be better friends.

yaoi prophet posted:

I seem to recall the MONEY type being specially designed to avoid that sort of stuff, but I'm not 100% certain
The MONEY type is, yes, but that doesn't stop the fact that the variable which is being put into the MONEY field has type TYPE_FLOAT.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Mill Town posted:

DB DB DB... Th-th-th-that's all folks!

:downsrim:

I chuckled. I think us PHP users have to find humor where we can

xarph
Jun 18, 2001


xarph posted:

This one is tame, except it does it for EVERY. SINGLE. PROMPT. in a 100+ step installation checklist.



Our install script* broke again on the latest build from Japan.


:v:

Yes, I know it's the leading space that somehow got in there but the error message amused me.

*text file full of carriage returns** with the occasional "no"
**does still using this term mean I'm old? :(

spiritual bypass
Feb 19, 2008

Grimey Drawer
What's that, gramps? Some sort of newline?

Opinion Haver
Apr 9, 2007

xarph posted:

Our install script* broke again on the latest build from Japan.


:v:

Yes, I know it's the leading space that somehow got in there but the error message amused me.

*text file full of carriage returns** with the occasional "no"
**does still using this term mean I'm old? :(

images is 404ing for me, gently caress waffleimages

yippee cahier
Mar 28, 2005

A consultant sent his source code to me recently, and I had the pleasure of rewriting it. I don't think this guy knows about the standard library.

code:
void convertUnsigned16BitToChars( unsigned short value16Bit, char * returnString ){
    char worker;

    worker = (char)((value16Bit & 0xF000) >> 12);
    if(worker >= 0 && worker < 10){
        worker = worker + 48;
    }else{
        worker = worker + 55;
    }
    returnString[0] = worker;
    
    worker = (char)((value16Bit & 0x0F00) >> 8);
    if(worker >= 0 && worker < 10){
        worker = worker + 48;
    }else{
        worker = worker + 55;
    }
    returnString[1] = worker;    

    worker = (char)((value16Bit & 0x00F0) >> 4);
    if(worker >= 0 && worker < 10){
        worker = worker + 48;
    }else{
        worker = worker + 55;
    }
    returnString[2] = worker;
    
    
    worker = (char)(value16Bit & 0x000F);
    if(worker >= 0 && worker < 10){
        worker = worker + 48;
    }else{
        worker = worker + 55;
    }
    returnString[3] = worker;

    return;
}
replaced with a call to sprintf().

code:
double power(double number, int toThe){
	double ret=1;
	int i;
	if(toThe==0)return 1;
	else if(toThe<0){
		toThe=-toThe;
		for(i=1;i<=toThe;i++)ret*=number;
		return 1/ret;
	}else{
		for(i=1;i<=toThe;i++)ret*=number;
		return ret;
	}
}
replaced with a call to pow().

code:
double degreesToRadians(double degrees){
	return degrees/180*PI;
}

double radiansToDegrees(double radians){
	return 180/PI*radians;
}
poo poo yeah, abstract away this insane complexity. He rewrote all the trigonometric functions too.

code:
char getLargestCHAR(char in1, char in2){
	return ( in1 >= in2 ) ? in1 : in2;
}
oh my god

I'd caution you against using any of this code in your own programs, as it's under copyright.

Blue Footed Booby
Oct 4, 2006

got those happy feet

sund posted:

code:
double degreesToRadians(double degrees){
	return degrees/180*PI;
}

double radiansToDegrees(double radians){
	return 180/PI*radians;
}

Even ignoring what's in the standard library (I don't know poo poo about C++ either) he managed to make the function name by itself more characters than doing it the "hard" way instead of just calling it "toRads" or something. :smithfrog:

That Turkey Story
Mar 30, 2003

Blue Footed Booby posted:

Even ignoring what's in the standard library (I don't know poo poo about C++ either) he managed to make the function name by itself more characters than doing it the "hard" way instead of just calling it "toRads" or something. :smithfrog:

What? That's not a coding horror at all, he was just being descriptive. The benefits of functions and abstraction are far greater than saving keystrokes.

floWenoL
Oct 23, 2002

sund posted:


code:
double degreesToRadians(double degrees){
	return degrees/180*PI;
}

double radiansToDegrees(double radians){
	return 180/PI*radians;
}
poo poo yeah, abstract away this insane complexity.

Blue Footed Booby posted:

Even ignoring what's in the standard library (I don't know poo poo about C++ either) he managed to make the function name by itself more characters than doing it the "hard" way instead of just calling it "toRads" or something. :smithfrog:

I love it when people try to post coding "horrors" and it backfires.

I think embedding *180/PI or /180*PI (or /PI/180 or *PI/180) everywhere is more of a coding horror than the cited code, which isn't a horror at all.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
I've come to believe that the majority of programmers in business think that the standard libraries stop with printf() and cout.

ErIog
Jul 11, 2001

:nsacloud:

Mustach posted:

I've come to believe that the majority of programmers in business think that the standard libraries stop with printf() and cout.

You just don't know where those other libraries have been, man.

RussianManiac
Dec 27, 2005

by Ozmaugh

That Turkey Story posted:

What? That's not a coding horror at all, he was just being descriptive. The benefits of functions and abstraction are far greater than saving keystrokes.

Yea I agree. I think long but descriptive function/variable names are very good for readability, even if they loose some stupid "elite" factor. With modern IDEs especially this is not a big problem as most have nice auto-complete features, so you don't even need to type more, but spend less time trying to figure out what random abbreviation meant.

GROVER CURES HOUSE
Aug 26, 2007

Go on...

Ledneh posted:

Out of curiosity, Broken Knees Club, did you ever find out what makes GCC die without this define in your codebase?

We just removed it one day, and it worked. Still don't know what the gently caress :iiam:

CanSpice
Jan 12, 2002

GO CANUCKS GO

That Turkey Story posted:

What? That's not a coding horror at all, he was just being descriptive. The benefits of functions and abstraction are far greater than saving keystrokes.

And if they change the definition of a radian then it only needs to be changed in two places!

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

CanSpice posted:

And if they change the definition of a radian then it only needs to be changed in two places!

no one argued that this was a good reason for doing it but if you can't see why rads = degreesToRadians(degrees); is better in every way than rads = degrees/180*PI; then the coding horror, it's you

GROVER CURES HOUSE
Aug 26, 2007

Go on...

CanSpice posted:

And if they change the definition of a radian then it only needs to be changed in two places!

This can actually happen if you're working with a library that defines Pi with multiple levels of accuracy! :v:

(Both .NET and XNA have their own Pi)

Space Kablooey
May 6, 2009


Broken Knees Club posted:

We just removed it one day, and it worked. Still don't know what the gently caress :iiam:

My totally unbased guess, it had to do with the addresses reserved in the memory for the variable.

What happened when you changed the value of the define? It also just didn`t work at all?

If leaving that didn`t had any side effects, I would just leave it, if not for the phrase itself.

GROVER CURES HOUSE
Aug 26, 2007

Go on...

HardDisk posted:

My totally unbased guess, it had to do with the addresses reserved in the memory for the variable.

What happened when you changed the value of the define? It also just didn`t work at all?

If leaving that didn`t had any side effects, I would just leave it, if not for the phrase itself.

If the flag was not present in one specific file in that exact form, GCC segfaulted. We were trying to remove it because it was literally the only file left over from an obsolete and long-gone library.

CanSpice
Jan 12, 2002

GO CANUCKS GO

Ryouga Inverse posted:

no one argued that this was a good reason for doing it but if you can't see why rads = degreesToRadians(degrees); is better in every way than rads = degrees/180*PI; then the coding horror, it's you

From a readability point of view it makes complete sense, yes. All you need is to slip once and suddenly your radians are off by 5% because you've hit 170 instead of 180.

I do the degreesToRadians() everywhere, except usually I use a constant D2R or something like that.

king_kilr
May 25, 2007
Guys you should be using ruby so you can just monkeypatch a to_radians and to_degrees functions onto int.

GROVER CURES HOUSE
Aug 26, 2007

Go on...

king_kilr posted:

Guys you should be using ruby so you can just monkeypatch a to_radians and to_degrees functions onto int.

I see your clever ruse.

king_kilr
May 25, 2007

Broken Knees Club posted:

I see your clever ruse.

No clever ruse intended :confused:. I seem to recall rubies integer class isn't actually named that though, is that the alleged ruse?

dis astranagant
Dec 14, 2006

king_kilr posted:

No clever ruse intended :confused:. I seem to recall rubies integer class isn't actually named that though, is that the alleged ruse?

No one sane uses an integer for radians.

chocojosh
Jun 9, 2007

D00D.

king_kilr posted:

Guys you should be using ruby so you can just monkeypatch a to_radians and to_degrees functions onto int.

C# has extension methods that can do that.

king_kilr
May 25, 2007

dis astranagant posted:

No one sane uses an integer for radians.

Doh. I'm going to go somewhere and pay penance for my insanity.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

dis astranagant posted:

No one sane uses an integer for radians.
sure, if you care enough about accuracy to get within 25 degrees of the correct answer, then go right ahead and use those extravagant floating point operations :rolleye:

#define 2PI 6

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"

dis astranagant posted:

No one sane uses an integer for radians.

Just monkey-patch Integer to be fractional, problem solved

Pardot
Jul 25, 2001




Dijkstracula posted:

sure, if you care enough about accuracy to get within 25 degrees of the correct answer, then go right ahead and use those extravagant floating point operations :rolleye:

#define 2PI 6

π should have been defined as twice what it is. You see 2π way more often.

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane

Pardot posted:

π should have been defined as twice what it is. You see 2π way more often.

Maybe my brain is just not working tonight, but where exactly do you see it regularly? Everything that uses Pi that I can think of (area, circumference, radian conversion) uses Pi, not 2*Pi.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
c = 2*pi*r
reduced h = h/2pi
omega = f*2pi
A shitton of other physics stuff, commonly anything where you're thinking in terms of a full revolution

shrughes
Oct 11, 2008

(call/cc call/cc)
The integers are dense modulo 2pi so you can achieve arbitrary trigonometric precision.

Grazing Occultation
Aug 18, 2009

by angerbutt
I interviewed a guy a while back who claimed in his resume that he'd written 500,000 lines of code.

The interview went well enough so we asked for a code sample. This was the first function that I looked at:
code:
const string& FilenameGenerator::getFilename(const string& mode) {
    if(indexes.find(mode) == indexes.end() ) indexes[mode] = 1;
    ostringstream index;
    index << setw(3) << setfill('0') << indexes[mode];
    string* filename = new string();
    *filename = base + inst + mode + index.str() + "."+ ext;
    ++(indexes[mode]);
    return *filename;
}
He's returning a const reference to a dynamically allocated std::string object. Guaranteed memory leak with memory that didn't need to be allocated in the first place!

Blue Footed Booby
Oct 4, 2006

got those happy feet

floWenoL posted:

I love it when people try to post coding "horrors" and it backfires.

I think embedding *180/PI or /180*PI (or /PI/180 or *PI/180) everywhere is more of a coding horror than the cited code, which isn't a horror at all.

Glad I could provide some amusement! :v:

I can't imagine anyone having to think about what toRads does, but I freely admit to being an awful scrub.

Blue Footed Booby fucked around with this message at 22:35 on Dec 8, 2009

Vanadium
Jan 8, 2005

Clearly degrees and radians ought to be given in separate types so that trigonometric functions can be overloaded on them.

oldkike
Jan 10, 2003

hey

www.pleasegimmeadollar.com

Vanadium posted:

Clearly degrees and radians ought to be given in separate types so that trigonometric functions can be overloaded on them.

Or use boost.units :)

newsomnuke
Feb 25, 2007

Grazing Occultation posted:

He's returning a const reference to a dynamically allocated std::string object. Guaranteed memory leak with memory that didn't need to be allocated in the first place!
Wouldn't
code:
const string& str = fileNameGen->getFilename("blah");
delete &str;
work? This way you can return by reference which is faster than returning by value! :downs:

MarsMattel
May 25, 2001

God, I've heard about those cults Ted. People dressing up in black and saying Our Lord's going to come back and save us all.
No, because that would be deleting the stack-allocated const reference which has the same value as the leaked pointer.

Adbot
ADBOT LOVES YOU

Vanadium
Jan 8, 2005

No, it would not, it would be perfectly fine, except nobody expects to have to do that when given a const reference, or any reference at all.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply