|
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.
|
# ? Mar 14, 2009 20:19 |
|
|
# ? Apr 25, 2024 23:20 |
|
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?
|
# ? Mar 16, 2009 14:32 |
|
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.
|
# ? Mar 16, 2009 20:18 |
|
I recently started a new job and one of the tables in a database I'm now working with:code:
|
# ? Mar 16, 2009 22:43 |
|
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.
|
# ? Mar 16, 2009 23:04 |
|
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 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. )
|
# ? Mar 16, 2009 23:31 |
|
Flobbster posted: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
|
# ? Mar 16, 2009 23:43 |
|
code:
|
# ? Mar 17, 2009 01:36 |
|
What exactly is it about the for-switch loop that causes it to spread so widely?
|
# ? Mar 17, 2009 01:59 |
|
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 |
# ? Mar 17, 2009 02:28 |
|
ShoulderDaemon posted: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.
|
# ? Mar 17, 2009 03:24 |
|
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 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.
|
# ? Mar 17, 2009 04:17 |
|
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.
|
# ? Mar 17, 2009 04:33 |
|
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
|
# ? Mar 17, 2009 05:05 |
|
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. 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 |
# ? Mar 17, 2009 05:06 |
|
Avenging Dentist posted:what What does this code print? code:
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 Not every C++ programmer knows to avoid non-const references, or is aware of their performance benefits.
|
# ? Mar 17, 2009 05:13 |
|
Janin posted:What does this code print? 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. :|
|
# ? Mar 17, 2009 05:15 |
|
Janin posted:What does this code print? http://codepad.org/s1Vd99ty
|
# ? Mar 17, 2009 05:25 |
|
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!".
|
# ? Mar 17, 2009 05:26 |
|
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
|
# ? Mar 17, 2009 05:33 |
|
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.
|
# ? Mar 17, 2009 05:36 |
|
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:
|
# ? Mar 17, 2009 05:37 |
|
Janin posted:http://codepad.org/r279Xoew 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
|
# ? Mar 17, 2009 05:42 |
|
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 Or just plain retarded and don't understand pointers.
|
# ? Mar 17, 2009 05:44 |
|
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 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.
|
# ? Mar 17, 2009 05:49 |
|
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 |
# ? Mar 17, 2009 05:49 |
|
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
|
# ? Mar 17, 2009 05:50 |
|
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.
|
# ? Mar 17, 2009 05:53 |
|
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).
|
# ? Mar 17, 2009 05:58 |
|
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.
|
# ? Mar 17, 2009 06:03 |
|
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. 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 |
# ? Mar 17, 2009 06:05 |
|
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.
|
# ? Mar 17, 2009 06:10 |
|
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.
|
# ? Mar 17, 2009 06:26 |
|
Janin posted:Oh good, so all I have to do to make reference code safe is...treat every reference as a magical pointer? 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.
|
# ? Mar 17, 2009 06:37 |
|
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.
|
# ? Mar 17, 2009 06:43 |
|
And now we flame each other over currying.
|
# ? Mar 17, 2009 14:08 |
|
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.
|
# ? Mar 17, 2009 14:36 |
|
Ryouga Inverse posted:What exactly is it about the for-switch loop that causes it to spread so widely? code:
|
# ? Mar 17, 2009 20:09 |
|
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.
|
# ? Mar 17, 2009 21:59 |
|
|
# ? Apr 25, 2024 23:20 |
|
I actually wrote this a few weeks ago - it's kinda like a for-switch, just without a proper for/switch:code:
This is not production (or even commercial) code, though.
|
# ? Mar 17, 2009 23:10 |