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
Ogive
Dec 22, 2002

by Lowtax
About 12 years ago, on day 1 of a new internship, I found a comment along the lines of "this client/server app assumes the client and server will both be running in the same process."

Took me a while before I told the boss there needed to be a hard reset.

Adbot
ADBOT LOVES YOU

Zombywuf
Mar 29, 2008

Janin posted:

cout isn't a singleton, LogSingleton isn't a (proper) singleton, almost any use of "singletons" is dumb, and the particular implementation of static methods proxying to private method is absurd.

Hey, did you know Java methods are pass by reference?

zergstain
Dec 15, 2005

Zombywuf posted:

Hey, did you know Java methods are pass by reference?

Eh, not really. There's no way that I know of (last Java I used was 1.4 in college) to change the value of an argument and have the calling function know about it. Of course since the variables just store references to their objects, everybody operates on the same object.

Pookster
Jul 4, 2007
I recently started a new job and one of the tables in a database I'm now working with:

code:
mysql> DESCRIBE Services;
<snip>
1847 rows in set (0.01 sec)
Yes, that means there are 1847 columns in this one table. This is on a MySQL 4.0 server. We're trying now to move to MySQL 5.1 but 5.1 has stricter rules about number of columns/column size and this table is actually too wide.

duck monster
Dec 15, 2004

Janin posted:

cout isn't a singleton, LogSingleton isn't a (proper) singleton, almost any use of "singletons" is dumb, and the particular implementation of static methods proxying to private method is absurd.

I think they are great, because it lets you have resources that really really should be singular , like data base connections, but you just loving know dipshit junior developers will be re-instantiating over and over against. (They should be re-instantiating the cursor, but the cursors just hang off the connection.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

duck monster posted:

I think they are great, because it lets you have resources that really really should be singular , like data base connections, but you just loving know dipshit junior developers will be re-instantiating over and over against. (They should be re-instantiating the cursor, but the cursors just hang off the connection.

I never understood the hate for singletons either. I always get the feeling that some self-proclaimed expert blogger wrote an entry once that "SINGLETONS ARE BAD!!" and then everyone hopped on the :bandwagon: and parroted it as The New Wisdom.

Just use the right design pattern for the right job. Are singletons the solution to every problem? Of course not -- some of the concerns and drawbacks are well-founded. But at the same time, the code contortions that some people suggest or use to avoid them can be just as ridiculous when a simple singleton would be just fine in that particular context.

I should start up a joke programming blog where I write posts like "Functions with multiple arguments are bad!" and rationalize it with horrible advice just to see how many people hop on board. Things like: "It doesn't scale; if you want to add arguments later, you have changed the entire signature of the function. Instead, all of your functions should return void and take an array of key/value pairs. This array should be used to return multiple values from the function as well, where the user supplies the keys representing the return values of interest with NULL value placeholders."

(As I was typing this, I realized I was describing AmigaOS 2.0+ tag lists. :smith: )

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

Flobbster posted:

:words:

Singletons are almost always pointless, and trying to shoehorn whatever problem you have into a GoF pattern is retarded

If patterns have any utility at all, it is not as glorified cookbook recipes

Borsalino
Mar 11, 2009

~ JAH LOVE ~
code:
        for(int i=0; i<6; i++){

                switch(i){

                    case 0: ps=*((double*)input[i]);
                            break;
                    case 1: month=*((int*)input[i]);
                            month2 = month;
                            break;
                    case 2: year=*((int*)input[i]);
                            year2 = year;
                            break;
                    case 3: months=*((int*)input[i]);
                            break;
                    case 4: mean=*((double*)input[i]);
                            break;
                    case 5: variance=*((double*)input[i]);
                            sigma=sqrt(variance);
                            break;
                    default:
                            break;
                }//end switch                                                                                                                                                    


        }//end for   
:psyduck:

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
What exactly is it about the for-switch loop that causes it to spread so widely?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Ryouga Inverse posted:

What exactly is it about the for-switch loop that causes it to spread so widely?

People frequently internalize processes as a sequence of steps which are, mentally, highly related. When they code this, they promptly put down a looping construct, because it seems natural to code a sequence as a loop. Then, while realizing the loop, they wind up specializing each step, because the language they are in does not operate the same way their mental processes do. If they're not the kind of person who looks over their own code immediately after writing it to take out stupid mistakes, they will never notice that the for loop is now completely unneeded; it does not even occur to them that the loop in their mind does not correspond to a loop in the code unless they are prompted to reexamine their own code.

Edit: In my experience, this very commonly happens when the code is being written inside a function which will itself be called many times in a loop; the aspiring programmer is in a "loop mindset" of sorts and winds up duplicating looping constructs because they are unclear in their mental model of exactly what the control flow they want is. This seems especially to happen if the control flow is being changed because they are still revising their overall design.

ShoulderDaemon fucked around with this message at 02:30 on Mar 17, 2009

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

ShoulderDaemon posted:

:words:

You could've just said "some people don't read what they write" and saved yourself a lot of handwaving.

I think you even tried to say that somewhere in the middle there.

Mill Town
Apr 17, 2006

Flobbster posted:

I should start up a joke programming blog where I write posts like "Functions with multiple arguments are bad!" and rationalize it with horrible advice just to see how many people hop on board. Things like: "It doesn't scale; if you want to add arguments later, you have changed the entire signature of the function. Instead, all of your functions should return void and take an array of key/value pairs. This array should be used to return multiple values from the function as well, where the user supplies the keys representing the return values of interest with NULL value placeholders."

I once worked on a project where functions did actually accept arguments like that. Of course they were key/value pairs from an HTTP POST, but still :psyduck:

I replaced it with a mechanism that used reflection to determine the name of the arguments a function took and then parse the arguments into the proper type, which is probably equally weird.

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"

Zombywuf posted:

Hey, did you know Java methods are pass by reference?

That code is C++, so "reference" is actually the insanely dangerous "any future code could change the variable used in this context to anything" kind, not Java/C#/Python copied pointers.

duck monster posted:

I think they are great, because it lets you have resources that really really should be singular , like data base connections, but you just loving know dipshit junior developers will be re-instantiating over and over against. (They should be re-instantiating the cursor, but the cursors just hang off the connection.

A singular resource is called a "global", and all the pretty accessor methods in the world won't make it not a global.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Janin posted:

That code is C++, so "reference" is actually the insanely dangerous "any future code could change the variable used in this context to anything" kind, not Java/C#/Python copied pointers.

what

Morton the Mole
Feb 24, 2005
Paid money to stop lurking.

Janin posted:

That code is C++, so "reference" is actually the insanely dangerous "any future code could change the variable used in this context to anything" kind, not Java/C#/Python copied pointers.


A singular resource is called a "global", and all the pretty accessor methods in the world won't make it not a global.

const fixes it
how is that insanely dangerous

const type& variable

Morton the Mole fucked around with this message at 05:09 on Mar 17, 2009

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"

Avenging Dentist posted:

what

What does this code print?

code:
int foo = 1;
some_function (foo);
cout << foo << endl;
:iiam:

Because C++ references have no special format at the call site, and can be stored or passed onward for later use, code that uses references can be very difficult to read and debug. In addition, some compilers (depending on optimization flags) will allow references to point to NULL, breaking their "always valid" semantics. Finally, the marketing of references as "safe" pointers tricks inexperienced users into subtle errors like storing a reference to a stack variable.

Morton the Mole posted:

const fixes it
how is that insanely dangerous

Not every C++ programmer knows to avoid non-const references, or is aware of their performance benefits.

Morton the Mole
Feb 24, 2005
Paid money to stop lurking.

Janin posted:

What does this code print?

code:
int foo = 1;
some_function (foo);
cout << foo << endl;
:iiam:

Because C++ references have no special format at the call site, and can be stored or passed onward for later use, code that uses references can be very difficult to read and debug. In addition, some compilers (depending on optimization flags) will allow references to point to NULL, breaking their "always valid" semantics. Finally, the marketing of references as "safe" pointers tricks inexperienced users into subtle errors like storing a reference to a stack variable.


Not every C++ programmer knows to avoid non-const references, or is aware of their performance benefits.

No, but why would you call it insanely dangerous. Also you don't have to avoid a non-const reference if a non-const reference is needed. :|

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Janin posted:

What does this code print?

code:
int foo = 1;
some_function (foo);
cout << foo << endl;
:iiam:

http://codepad.org/s1Vd99ty

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"

Morton the Mole posted:

No, but why would you call it insanely dangerous. Also you don't have to avoid a non-const reference if a non-const reference is needed. :|

Non-const references are insanely dangerous because they allow arbitrary unchecked modification of local variables without any accompanying indication at the callsite. To have even a basic understanding of how reference-using code works, a reader must memorize the type signatures of every function and method called (and, if one uses a reference parameter, everything *they* call). At least in C and C# there are sigils to tell the reader "WARNING: this call might do anything to the parameter!".

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

Janin posted:

Non-const references are insanely dangerous because they allow arbitrary unchecked modification of local variables without any accompanying indication at the callsite. To have even a basic understanding of how reference-using code works, a reader must memorize the type signatures of every function and method called (and, if one uses a reference parameter, everything *they* call). At least in C and C# there are sigils to tell the reader "WARNING: this call might do anything to the parameter!".

If you don't know what a function does you probably shouldn't use that function

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"

Avenging Dentist posted:

http://codepad.org/s1Vd99ty

http://codepad.org/r279Xoew

Otto Skorzeny posted:

If you don't know what a function does you probably shouldn't use that function

Thanks, I'll remember this sage advice the next time I'm fixing somebody else's code.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Otto Skorzeny posted:

If you don't know what a function does you probably shouldn't use that function

wait do you mean i need to stop doing this in my code
code:
srand(time(0));
typedef int (*func_type)();
func_type f = ( rand()/sizeof(void*) ) *sizeof(void*);
int i = f(1,2,3,4,5);
fuuuuuuuuuuck

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

oh fabulous, a tired talking point about an easy-to-diagnose problem that only occurs when people are intentionally trying to be retarded

oh poo poo i might have to load up gdb what has this world come to

narbsy
Jun 2, 2007

Avenging Dentist posted:

oh fabulous, a tired talking point about an easy-to-diagnose problem that only occurs when people are intentionally trying to be retarded

oh poo poo i might have to load up gdb what has this world come to

Or just plain retarded and don't understand pointers.

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"

Avenging Dentist posted:

oh fabulous, a tired talking point about an easy-to-diagnose problem that only occurs when people are intentionally trying to be retarded

oh poo poo i might have to load up gdb what has this world come to

Do you think I just heard about references and decided to hate them based on the name? It may be an easy problem to diagnose in a 20-line program directly in front of you, but you try tracking down the source of an irregular once-per-day crash by talking to a receptionist over the telephone. Then tell me how references are safe.

narbsy posted:

Or just plain retarded and don't understand pointers.

Obviously somebody that advocates C in favor of C++ doesn't understand pointers.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Seriously dude, this code is precisely as easy to debug: http://codepad.org/wGa2VjI4

And before you say "oh but you can assert(sc) in that example, making it easier to find the point of failure", you can do that with references too if you're paranoid.

Janin posted:

Do you think I just heard about references and decided to hate them based on the name? It may be an easy problem to diagnose in a 20-line program directly in front of you, but you try tracking down the source of an irregular once-per-day crash by talking to a receptionist over the telephone. Then tell me how references are safe.

I'm sorry to hear about your inability to run any profiling software.

EDIT: forgot fflush

Avenging Dentist fucked around with this message at 05:52 on Mar 17, 2009

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

Janin posted:

you try tracking down the source of an irregular once-per-day crash by talking to a receptionist over the telephone

What do references have to do with this

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"

Otto Skorzeny posted:

What do references have to do with this

References are supposed to guarantee that they point to a valid, allocated region of memory.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Janin posted:

References are supposed to guarantee that they point to a valid, allocated region of memory.

Nope, sorry. While a program containing null references is not well-defined, the ISO standard does not dictate the behavior of compilation/execution of such a program (given that the dereferencing of a null pointer is explicitly undefined).

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Janin posted:

References are supposed to guarantee that they point to a valid, allocated region of memory.

It's as much a guarantee as anything else in C++, which is to say that it's basically worthless except as a signal to other programmers.

I think there's a good case to be made that "silent" pass-by-reference is a bad idea, but complaining that references aren't totally sound in a language that's basically unsound in a thousand different ways is a bit over-dramatic.

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"

Avenging Dentist posted:

Nope, sorry. While a program containing null references is not well-defined, the ISO standard does not dictate the behavior of compilation/execution of such a program (given that the dereferencing of a null pointer is explicitly undefined).

Oh good, so all I have to do to make reference code safe is...treat every reference as a magical pointer?

rjmccall posted:

It's as much a guarantee as anything else in C++, which is to say that it's basically worthless except as a signal to other programmers.

I think there's a good case to be made that "silent" pass-by-reference is a bad idea, but complaining that references aren't totally sound in a language that's basically unsound in a thousand different ways is a bit over-dramatic.

The problem is I'm working on code written by people who have been told "pointers are not safe, don't use them, always use references, they are safe!". I wouldn't mind the feature so much if it came with some way to indicate to future readers when it was in use. It's the combination of "any of these hundred function calls could validly change my local stack" and "I don't know which ones".

TOO SCSI FOR MY CAT fucked around with this message at 06:11 on Mar 17, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Janin posted:

Oh good, so all I have to do to make reference code safe is...treat every reference as a magical pointer?

No, you open up a debugger/profiler like anyone in the world who's diagnosing a segfault.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Janin posted:

The problem is I'm working on code written by people who have been told "pointers are not safe, don't use them, always use references, they are safe!".

Yeah, this is a terrible attitude for plenty of reasons. It's even true in an extremely specialized sense: you can't make an initially-invalid reference without somehow going through a pointer, you can't easily access memory outside the referenced object, etc. But ultimately, you do have to treat a reference like the pointer it is.

The Red Baron
Jan 10, 2005

Janin posted:

Oh good, so all I have to do to make reference code safe is...treat every reference as a magical pointer?


The problem is I'm working on code written by people who have been told "pointers are not safe, don't use them, always use references, they are safe!". I wouldn't mind the feature so much if it came with some way to indicate to future readers when it was in use. It's the combination of "any of these hundred function calls could validly change my local stack" and "I don't know which ones".

If the people who worked on the code before you couldn't understand the semantics of references (which is scary enough by itself), I can hardly see how that's the language's fault any more than it would be Java's fault if a programmer couldn't understand the "reference by value" passing that has already been discussed ad nauseum (and I've seen several people who've struggled with exactly that. They were programming 101 students, though). If you violate a function's preconditions or an invariant of the language itself, your program is in an inconsistent state by definition.

Allowing a null reference to be passed is no more a design flaw of C++ than allowing NULL to be passed to strlen is a design flaw of C. They're both precondition violations and will most likely have the same outcome.

shrughes
Oct 11, 2008

(call/cc call/cc)

Flobbster posted:

I should start up a joke programming blog where I write posts like "Functions with multiple arguments are bad!"

Well, this statement alone is actually 100% correct.

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.
And now we flame each other over currying.

biznatchio
Mar 31, 2001


Buglord

Janin posted:

Oh good, so all I have to do to make reference code safe is...treat every reference as a magical pointer?

I think C++ is not the right language for you. Your desire for 'safety' clearly surpasses the level of 'safety' inherent in the language itself.

Maybe you'd feel better with a nice cup of Java.

necrobobsledder
Mar 21, 2005
Lay down your soul to the gods rock 'n roll
Nap Ghost

Ryouga Inverse posted:

What exactly is it about the for-switch loop that causes it to spread so widely?
I see this happen when I write something during test and in practice it's a simple iteration:

code:
for(LoopVariable i in collectionC) {
  switch(i) {
     case...
  }
}
The underlying psychological reasoning is surprisingly close to why people overengineer solutions - to anticipate boogeyman problems and not adhering to the principle of YAGNI.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

biznatchio posted:

Maybe you'd feel better with a nice cup of Java.

Actually, I did a quick Google search about null reference detection in Valgrind and found a research paper all about debugging null references in Java.

Adbot
ADBOT LOVES YOU

Fehler
Dec 14, 2004

.
I actually wrote this a few weeks ago - it's kinda like a for-switch, just without a proper for/switch:

code:
    int curline = 0;
    while(!feof(file) && curline < 3) {
        fgets(line, sizeof(line), file);
        if(curline == 0) {
            ...
        } else if(curline == 1) {
            ...
        } else if(curline == 2) {
            ...
        }
 
        curline++;
    }
I actually had a good reason for this as well, as there was gonna be a final else that handled all lines after the third one. It turned out I didn't need that though and I never bothered to change it back...

This is not production (or even commercial) code, though.

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