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
Zombywuf
Mar 29, 2008

floWenoL posted:

Oh, how cute, it's someone who hasn't ever needed to program for performance!

Never found I could do much better than just telling gcc to do a profiled build. Unless I did it in asm. If you're playing guess-what-the-compiler-will-do you're wasting time.

Adbot
ADBOT LOVES YOU

tef
May 30, 2004

-> some l-system crap ->

Zombywuf posted:

If you're playing guess-what-the-compiler-will-do you're wasting time.

Optimizing SQL is fun, eh ?

Context: This is the code zombywuf normally optimizes for performance, and most of that is nearly all guessing what the compiler will do (in a non-deterministic way)

tef fucked around with this message at 16:06 on Dec 24, 2008

floWenoL
Oct 23, 2002

Zombywuf posted:

Never found I could do much better than just telling gcc to do a profiled build. Unless I did it in asm. If you're playing guess-what-the-compiler-will-do you're wasting time.

There's a difference between second-guessing the compiler and making your code amenable to optimization. A compiler (even with profile-driven optimization) is a tool, not an oracle, and dropping down to writing assembly, even when possible, is a dumb move if it's not a last resort.

SporkOfTruth
Sep 1, 2006

this kid walked up to me and was like man schmitty your stache is ghetto and I was like whatever man your 3b look like a dishrag.

he was like damn.

tripwire posted:

Ulillillia released the source code for a program he designed in C which converts the sampling rate of PCM wave files.
Check it out:
http://www.ulillillia.us/files/WAVFileSampleRateGeneratorV2Source.zip
Oh my god. Oh....my god. He could have googled "sample rate conversion" and found something better.

As a DSP guy, the concept of a lookup table (especially repeating similar fractions over and over) for this leaves me speechless. :psyduck:

Sewer Adventure
Aug 25, 2004

SporkOfTruth posted:

Oh my god. Oh....my god. He could have googled "sample rate conversion" and found something better.

As a DSP guy, the concept of a lookup table (especially repeating similar fractions over and over) for this leaves me speechless. :psyduck:

He's autistic

biznatchio
Mar 31, 2001


Buglord

Sewer Adventure posted:

He's autistic

Proof that being autistic doesn't mean you're some sort of savant.

Rottbott
Jul 27, 2006
DMC

Mikey-San posted:

why
It was poorly desgined to begin with, then maintained and added to over many years by bad programmers. There are also hundreds of global variables, many of which have incorrect hungarian prefixes where the type has been changed but not the variable name.

tripwire
Nov 19, 2004

        ghost flow

Rottbott posted:

It was poorly desgined to begin with, then maintained and added to over many years by bad programmers. There are also hundreds of global variables, many of which have incorrect hungarian prefixes where the type has been changed but not the variable name.

God I hate hungarian notation. How in the hell did anyone ever think it was a smart idea :psyduck:

narbsy
Jun 2, 2007

tripwire posted:

God I hate hungarian notation. How in the hell did anyone ever think it was a smart idea :psyduck:

Ask nebby!

Steampunk Mario
Aug 12, 2004

DIAGNOSIS ACQUIRED

tripwire posted:

God I hate hungarian notation. How in the hell did anyone ever think it was a smart idea :psyduck:

Its original intent - to document the purpose and usage of the variable - was a great idea. Somehow it got perverted into being only the type, which is obviously useless.

tripwire
Nov 19, 2004

        ghost flow

Steampunk Mario posted:

Its original intent - to document the purpose and usage of the variable - was a great idea. Somehow it got perverted into being only the type, which is obviously useless.

At the risk of opening a can of worms, its retarded in either form. You are only adding more headaches for yourself later on.

Steampunk Mario
Aug 12, 2004

DIAGNOSIS ACQUIRED

tripwire posted:

At the risk of opening a can of worms, its retarded in either form. You are only adding more headaches for yourself later on.

I'm not some hungarian evangelist - I'm just saying its current usage is unambiguously worthless, but its original intent had potential to make more readable, self-documenting code. Not that it matters since most of us don't get to decide on the coding style for our particular codebase anyway.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Steampunk Mario posted:

I'm not some hungarian evangelist - I'm just saying its current usage is unambiguously worthless, but its original intent had potential to make more readable, self-documenting code. Not that it matters since most of us don't get to decide on the coding style for our particular codebase anyway.

"Purpose and usage" of a variable are a kind of typing; even if the compiler doesn't enforce them, typedefs and the like are a better way to encode that information than naming conventions. Hungarian notation sort of vaguely makes sense if you are stuck with a type system less adequate than C's and don't even have something like a preprocessor available; even then, I would advise changing toolchains or supplying descriptive comments at variable declarations instead.

twodot
Aug 7, 2005

You are objectively correct that this person is dumb and has said dumb things

ShoulderDaemon posted:

"Purpose and usage" of a variable are a kind of typing; even if the compiler doesn't enforce them, typedefs and the like are a better way to encode that information than naming conventions. Hungarian notation sort of vaguely makes sense if you are stuck with a type system less adequate than C's and don't even have something like a preprocessor available; even then, I would advise changing toolchains or supplying descriptive comments at variable declarations instead.
This is kind of bizarre in my opinion. With a sufficiently strong type system you can claim any information is part of the typing. There are trade offs involved with deciding what information you put into a typedef and what information you put into a variable name. Personally, I think it can be useful to put information like whether a variable counts characters or bytes into the variable name rather than as a comment in the variable declaration or as a named type. Jumping between usages and definitions has costs especially in more object oriented code where definitions are non-local.

Hungarian notation isn't anything more than a standardized method of encoding information about a variable into its name. This is something that is always done when a variable name is created, standardizing it isn't intrinsically bad. Though most people's usages of Hungarian notation is very bad.

twodot fucked around with this message at 09:04 on Dec 29, 2008

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

twodot posted:

Hungarian notation isn't anything more than a standardized method of encoding information about a variable into its name. This is something that is always done when a variable name is created, standardizing it isn't intrinsically bad.

Types aren't anything more than a method of encoding information about a variable in the same place the variable is created. They have many advantages over name-based methods: they are easier for tools to parse and reason about, many languages feature some form of checking that expressions are made of compatible types, many compilers are able to use them to decide on optimal storage representations, and you don't have to retype them over and over whenever you use the variable.

But we're getting way afield of the topic of the thread; if someone wants to debate this more, maybe they should start another "hungarian notation isn't that bad really" thread for it.

twodot
Aug 7, 2005

You are objectively correct that this person is dumb and has said dumb things

ShoulderDaemon posted:

Types aren't anything more than a method of encoding information about a variable in the same place the variable is created. They have many advantages over name-based methods: they are easier for tools to parse and reason about, many languages feature some form of checking that expressions are made of compatible types, many compilers are able to use them to decide on optimal storage representations, and you don't have to retype them over and over whenever you use the variable.
Sorry, I shouldn't have phrased putting information into a name and into a type as mutually exclusive, because they are not at all. The information can easily be put in both places. If a static tool can take advantage of the information absolutely put it into the type, if a person can take advanatage of the information, it may be better off in the name.

Erasmus Darwin
Mar 6, 2001

twodot posted:

If a static tool can take advantage of the information absolutely put it into the type, if a person can take advanatage of the information, it may be better off in the name.

However, redundancy between the two should be minimized. As long as the information's available in the type, then the IDE should be able to provide it to the user in a variety of ways without needlessly complicating the naming scheme used for variables. You could even have it display in-line ghostly (light-gray inactive text) Hungarian type prefixes to all the variables for users who want the Hungarian look.

That being said, I realize that it's easy to pass the buck to the IDE when such a feature may not actually exist.

6174
Dec 4, 2004

tripwire posted:

God I hate hungarian notation. How in the hell did anyone ever think it was a smart idea :psyduck:

Maybe this is my own crackpot theory, but I blame Fortran.

First a brief history lesson. The oldest versions of the language didn't have a way to declare variables. In Fortran I (mid to late 1950s), you had 26 variables corresponding to the 26 letters of the alphabet. The type of the variable was determined based on the letter used. I, J, K, L, M, and N were integers, with all others floating point. In Fortran IV (early 1960s, standardized as Fortran 66), support was added for explicit type declaration. However it still supported implicit typing. If at the beginning of a subroutine you had "IMPLICIT INTEGER (I-N)", then you were saying that any variable starting with letters I-N was an integer. This led to conventions, particularly prevalent in Fortran IV and 77 code, that integers would be started with the letters I-N whether they were explicitly declared or not. My suspicion is that the older programmers, used to the old way of things, continued to prefix their variables, and they were likely also the ones to write documentation, and of course then newer programmers mimicked their style, keeping the tradition going. While I haven't seen it, based on my experience with old Fortran code and some documentation, I wouldn't be surprised to see a few paragraphs in old books recommending the practice of adding a letter to the variable name to make the code clearer.

Now we are too my crackpot theory. I believe that Charles Simonyi originally learned to program in Fortran and would have known all of the above, or at least enough of it to follow the practice of adding a prefix to a variable. This is reasonable because he worked at Xerox PARC from 1972-1981, according to Wikipedia. Fortran was one of the main languages of that era. I also believe that Simonyi carried this convention with him to Microsoft and the Excel team. Most likely after another programmer on one of the teams at Microsoft questioned Simonyi on his naming conventions, he became defensive (as programmers tend to be), and rationalized the whole system as a means to making code easier to read and using the convenient first name/last name switch of his native Hungary as the last piece in the puzzle.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

6174 posted:

Maybe this is my own crackpot theory

That has no relation to reality at all.

Wikipedia posted:

In Systems Hungarian notation, the prefix encodes the actual data type of the variable. For example:
  • lAccountNum : variable is a long integer ("l");
  • arru8NumberList : variable is an array of unsigned 8-bit integers ("arru8");
  • szName : variable is a zero-terminated string ("sz"); this was one of Simonyi's original suggested prefixes.

Apps Hungarian notation doesn't encode the actual data type, but rather, it gives a hint as to what the variable's purpose is, or what it represents.
  • rwPosition : variable represents a row ("rw");
  • usName : variable represents an unsafe string ("us"), which needs to be "sanitized" before it is used (e.g. see code injection and cross-site scripting for examples of attacks that can be caused by using raw user input)
  • strName : Variable represents a string ("str") containing the name, but does not specify how that string is implemented.

Charles Simonyi's original paper proposed what is now know as "Apps Hungarian", which adds semantic information about how the variable is used. Even if you dislike the idea of this, it should be pretty obvious why someone would consider it a good idea. However, he unfortunately used the word "type" for this, which resulted in people misinterpreting what he meant, resulting in the awful "Systems Hungarian" used for all of the Win32 stuff.

Victor
Jun 18, 2004

twodot posted:

This is kind of bizarre in my opinion. With a sufficiently strong type system you can claim any information is part of the typing. There are trade offs involved with deciding what information you put into a typedef and what information you put into a variable name. Personally, I think it can be useful to put information like whether a variable counts characters or bytes into the variable name rather than as a comment in the variable declaration or as a named type. Jumping between usages and definitions has costs especially in more object oriented code where definitions are non-local.

Hungarian notation isn't anything more than a standardized method of encoding information about a variable into its name. This is something that is always done when a variable name is created, standardizing it isn't intrinsically bad. Though most people's usages of Hungarian notation is very bad.
I think this is the best way to frame the "pro-Hungarian" perspective that I've ever seen. I'd create a new thread, but I have little confidence that the discussion would stay at the level that has been maintained for a few posts here. I'm not saying that I agree with twodot, but one would be retarded not to acknowledge that the point brings up valid concerns.

6174
Dec 4, 2004

Plorkyeran posted:

That has no relation to reality at all.


Charles Simonyi's original paper proposed what is now know as "Apps Hungarian", which adds semantic information about how the variable is used. Even if you dislike the idea of this, it should be pretty obvious why someone would consider it a good idea. However, he unfortunately used the word "type" for this, which resulted in people misinterpreting what he meant, resulting in the awful "Systems Hungarian" used for all of the Win32 stuff.

Whether Simonyi's original paper proposed Apps Hungarian or Systems Hungarian neither proves nor refutes my potential theory (which I freely admit could be wrong). However, if you actually read what MSDN claims is a reprint of the paper, you'll see that in Table 4, is a listing of "common primitive types" which would now be known as Systems Hungarian. In fact the section of the Wikipedia article you blithely copied gives an example of a Systems Hungarian prefix as sz and states it is from the original paper (it is in Table 4). This suggests that the lack of clarity in his paper comes from more than just the usage of the word "type".

Kharya
Sep 23, 2005
So how DO you chase off a half blind, crazed, sugar addict?
Oh variable naming. It's an art in and of itself:
php:
<?
$the_ID = $_GET['ID']*1;
$kill_me_now = get_magic_quotes_gpc();
...
...
...
mysql_query("UPDATE `users_table`
SET `name`='".mysql_escape_string(($kill_me_now?stripslashes($name):$name))."'
WHERE `ID`=".$the_ID);
?>
This is required for us because we switch between our development server running php 5.2.6 with magic quotes off and our live server running php 4.2.0 with magic quotes on.

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

Kharya posted:

Oh variable naming. It's an art in and of itself:
php:
<?
$the_ID = $_GET['ID']*1;
$kill_me_now = get_magic_quotes_gpc();
...
...
...
mysql_query("UPDATE `users_table`
SET `name`='".mysql_escape_string(($kill_me_now?stripslashes($name):$name))."'
WHERE `ID`=".$the_ID);
?>
This is required for us because we switch between our development server running php 5.2.6 with magic quotes off and our live server running php 4.2.0 with magic quotes on.

php 4.2.0 seems oddly appropriate for this code:ironicat:

awesmoe
Nov 30, 2005

Pillbug

Kharya posted:

Oh variable naming. It's an art in and of itself:
php:
<?
$the_ID = $_GET['ID']*1;
$kill_me_now = get_magic_quotes_gpc();
...
...
...
mysql_query("UPDATE `users_table`
SET `name`='".mysql_escape_string(($kill_me_now?stripslashes($name):$name))."'
WHERE `ID`=".$the_ID);
?>
This is required for us because we switch between our development server running php 5.2.6 with magic quotes off and our live server running php 4.2.0 with magic quotes on.

Why is your dev server running different versions of stuff than your live server?

Mikey-San
Nov 3, 2005

I'm Edith Head!

awesmoe posted:

Why is your dev server running different versions of stuff than your live server?

Factor Mystic
Mar 20, 2006

Baby's First Post-Apocalyptic Fiction
code:
year = ORIGINYEAR; /* = 1980 */

while (days > 365)
{
    if (IsLeapYear(year))
    {
        if (days > 366)
        {
            days -= 366;
            year += 1;
        }
    }
    else
    {
        days -= 365;
        year += 1;
    }
}
Whoops :ohno:

zootm
Aug 8, 2006

We used to be better friends.

Factor Mystic posted:

code:
year = ORIGINYEAR; /* = 1980 */

while (days > 365)
{
    if (IsLeapYear(year))
    {
        if (days > 366)
        {
            days -= 366;
            year += 1;
        }
    }
    else
    {
        days -= 365;
        year += 1;
    }
}
Whoops :ohno:
Is this from the Zune? :ohdear:

Edit: Holy poo poo, it is. That'll teach me to joke about such things.

zootm fucked around with this message at 14:26 on Jan 1, 2009

geetee
Feb 2, 2004

>;[
Yep :downs: Lucky for them, it only killed it for a day.

Dan Two Hundred
Oct 12, 2004
*quadruple air guitar solo*
code:
int year = ORIGINYEAR;

int yearLength;
while(day >= (yearLength = (IsLeapYear(year) ? 366 : 365)))
{
    day -= yearLength;
    year++;
}
Hire me microsoft!

mr_jim
Oct 30, 2006

OUT OF THE DARK

Factor Mystic posted:

code:
year = ORIGINYEAR; /* = 1980 */

while (days > 365)
{
    if (IsLeapYear(year))
    {
        if (days > 366)
        {
            days -= 366;
            year += 1;
        }
    }
    else
    {
        days -= 365;
        year += 1;
    }
}
Whoops :ohno:

It took me a while to notice the problem with that code. I kept thinking "Well, it looks reasonable..."
Edge-case bugs suck, but you'd think they would have done some testing of the leap-year handling code before release.

Vanadium
Jan 8, 2005

mr_jim posted:

It took me a while to notice the problem with that code. I kept thinking "Well, it looks reasonable...

I kept trying to figure out how it would arrive at a wrong answer and could not come up with anything. :shobon:

mr_jim
Oct 30, 2006

OUT OF THE DARK

Vanadium posted:

I kept trying to figure out how it would arrive at a wrong answer and could not come up with anything. :shobon:


If you're still curious, the error occurs when days == 366, as in New Years Eve on a leap year. The "if (days > 366)" statement doesn't get executed, so days never gets decremented. But the condition for the while loop (days > 365) is still true, so an infinite loop occurs.

It's a piece of code that looks ok at first glance, but it's really a time bomb. Luckily, in this case, it just caused minor inconvenience.

Vanadium
Jan 8, 2005

I ended up realising that I was looking for the wrong thing, yeah.

geetee
Feb 2, 2004

>;[

Dan Two Hundred posted:

code:
while(day >= (yearLength = (IsLeapYear(year) ? 366 : 365)))
Hire me microsoft!

That's painful to read. Less hotshot superstar, more lines of code please.

ani47
Jul 25, 2007
+
It's also incorrect :).

cerror
Feb 11, 2008

I have a bad feeling about this...
A while back after John McCain nominated Sarah Palin as his running mate, my company (Palin's ISP) had me make some super-fast emergency security upgrades to our ticket system to make sure none of our help desk techs were doing anything naughty to her account, and also a few other major ones.

Anyway, they wanted the changes done yesterday so I whipped up this ugly bit of code in the accounts perl module:

code:
# big ugly security paranoia to follow

if ($acct == 816458 || $acct == 14645 || $acct == 44617 || $acct == 19120 || $acct == 2048 || $acct == 822257) {
                                my $fireme = critalerttech($tech);
                                my $victim = critalertuser($acct);

                                my $to = "a bunch of email addys go here";
                                my $from = "my email addy goes here";
                                my $subject = "CRITICAL Account Change!";
                                my $body = "$fireme just made the following changes to $victim: $changes";
                                sendemail($to,$from,$subject,$body);
                        }
To this day I am ashamed of that if statement.

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
I guess you could have used a switch but no code with arbitrary numbers like that is going to be nice.

dancavallaro
Sep 10, 2006
My title sucks

awesmoe posted:

Why is your dev server running different versions of stuff than your live server?

This is the real coding horror, along with not using prepared statements.

Erasmus Darwin
Mar 6, 2001

MasterSlowPoke posted:

I guess you could have used a switch but no code with arbitrary numbers like that is going to be nice.

Throw them into a data structure, and you can make it look a little nicer:

code:
if ($monitored_acct{$acct}) {
  # Blah blah
}
You still have the minor sin of mixing code and data, but now you can at least move the data to the top of the script (or even in a module) with other user-configurable stuff. It's not the best solution, but at least you no longer have Sarah Palin's account number referenced on line 3248 of your script, and it'll be less hassle to maintain if, say, Natalie Portman moves to Alaska and signs up at the ISP.

And ideally, when there's some free time, you could move it to a proper configuration file that's read by the program. But there's always that sense of "Well, it works so why mess with it?"

Adbot
ADBOT LOVES YOU

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

comaerror posted:


To this day I am ashamed of that if statement.

You should be really proud of your var naming, though. $fireme is genius.

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