|
Internet Janitor posted:I just rewrote the whole mess as: Fixed that for you
|
# ? Sep 24, 2011 23:38 |
|
|
# ? Jun 6, 2024 09:57 |
|
I always love when people declare all their variables at the beginning of the method in order to maintain compatibility with ANSI C in Java
|
# ? Sep 24, 2011 23:56 |
|
Internet Janitor posted:to this. Does anybody still intentionally write method signatures like that? What is the deal with that hostent pointer? You shouldn't be calling it now anyway because it's obsolete (so I guess it's a good thing it wasn't used). Use getnameinfo instead.
|
# ? Sep 25, 2011 05:14 |
|
Aleksei Vasiliev posted:I always love when people declare all their variables at the beginning of the method in order to maintain compatibility with ANSI C I still think it's a good practice regardless. Perhaps declaring at the start of your scope makes more sense, but I like it. Also I still do this.
|
# ? Sep 25, 2011 16:46 |
|
Declaring it at the start of your scope is the only sane way to do it. The only time I have to break that in Java is when I'm dealing with resources that need to be closed in a finally block.
|
# ? Sep 25, 2011 16:56 |
|
What? Why would you declare variables at the start of your scope? It adds extra lines and confusion.
|
# ? Sep 25, 2011 17:48 |
|
Sorry I got confused with what he meant by the start of the scope. What I mean to say is that you should declare variables exactly where you need them, and not sooner. The exception being what I said about resources and the finally block.
|
# ? Sep 25, 2011 18:24 |
|
geonetix posted:I still think it's a good practice regardless. Perhaps declaring at the start of your scope makes more sense, but I like it. Also I still do this. With few exceptions, you should declare variables as near as possible to their first use. Anything else is madness. edit: what Meat said.
|
# ? Sep 25, 2011 19:41 |
|
Well, call it a coding horror if you wish. It's the way I learned it, and the way my company does it. Considering I tend to keep my functions short and the number of variables to a minimum, it hasn't bothered me yet. Luckily for the world, I don't actually write code anymore. Now other people get to deal with my code and post it here if they run into it.
|
# ? Sep 25, 2011 20:33 |
|
geonetix posted:It's the way I learned it, and the way my company does it. This embrace of ignorance causes most of code in this thread.
|
# ? Sep 25, 2011 21:37 |
|
Brecht posted:Coding horror spotted~ Except for Javascript, which helpfully pulls all declared variables to the top of the scope regardless of where you actually defined them. Fun times.
|
# ? Sep 25, 2011 21:42 |
|
Brecht posted:Coding horror spotted~ But why? Pretty much every cs class at my university teaches students to declare variables at the beginning of the function(obviously not loop counters and poo poo like that).
|
# ? Sep 25, 2011 21:45 |
|
Because:code:
|
# ? Sep 25, 2011 22:00 |
|
Even ANSI C only requires top-of-scope, and not top-of-procedure.
|
# ? Sep 25, 2011 22:10 |
|
Dicky B posted:Because: angrytech posted:But why? Pretty much every cs class at my university teaches students to declare variables at the beginning of the function(obviously not loop counters and poo poo like that).
|
# ? Sep 25, 2011 22:25 |
|
I prefer to declare variables at the point of first use, because then it's completely impossible to accidentally use the variable uninitialised.
|
# ? Sep 25, 2011 23:21 |
|
Janin posted:Even ANSI C only requires top-of-scope, and not top-of-procedure. Does that not mean you can trivially satisfy that requirement by inserting a { before every declaration and inserting a matching } before the next unmatched }?
|
# ? Sep 25, 2011 23:42 |
|
I never expected the Coding Horrors thread to theorycraft a new form of coding horror. Good job!
|
# ? Sep 25, 2011 23:43 |
|
Vanadium posted:Does that not mean you can trivially satisfy that requirement by inserting a { before every declaration and inserting a matching } before the next unmatched }? I desperately want an excuse to do that now, preferably somewhere I'll never work again.
|
# ? Sep 25, 2011 23:47 |
|
NotShadowStar posted:Except for Javascript, which helpfully pulls all declared variables to the top of the scope regardless of where you actually defined them. Fun times. On a related and thread relevant note, at one point Amazon and Intel broke their sites for the Firefox 4.0 beta by putting the new EcmaScript 5 directive "use strict" at the top of one of their JS files and then concatenating it with other JS files that were non-compliant. Apparently, for intel at least, this was because they ran it through JSLint, which recommendeds the directive, but doesn't catch the errors that result from the non-compliant code.
|
# ? Sep 25, 2011 23:52 |
|
Vanadium posted:Does that not mean you can trivially satisfy that requirement by inserting a { before every declaration and inserting a matching } before the next unmatched }?
|
# ? Sep 26, 2011 00:28 |
|
Orzo posted:Not that I agree with declaring the variables at the top of scope, but this is the wrong reason. There's a difference between declaration and construction. In C++ the declaration will cause the default constructor to be invoked.
|
# ? Sep 26, 2011 01:39 |
|
More proof that C++ is a horror all its own.
|
# ? Sep 26, 2011 01:48 |
|
GrumpyDoctor posted:In C++ the declaration will cause the default constructor to be invoked.
|
# ? Sep 26, 2011 02:11 |
|
Orzo posted:I guess I forgot about that. A horror indeed. Yeah I'm usually a Java guy but I was debugging some C++ code recently and until I found this out it was all the way.
|
# ? Sep 26, 2011 02:30 |
|
I find that completely intuitive and I don't understand what is supposed to be about it.
|
# ? Sep 26, 2011 03:20 |
|
Non c++ programmers talking about c++
|
# ? Sep 26, 2011 03:31 |
|
TasteMyHouse posted:I find that completely intuitive and I don't understand what is supposed to be about it. code:
baquerd fucked around with this message at 03:53 on Sep 26, 2011 |
# ? Sep 26, 2011 03:51 |
|
baquerd posted:
In C++, declaring an object like this: code:
The C++ equivalent of the Java style stuff you wrote above is: code:
e: augh you edited while I posted. In C++, stack variables are allocated by the compiler. There's no way to have a stack object without the full memory footprint of a constructed object. TasteMyHouse fucked around with this message at 04:03 on Sep 26, 2011 |
# ? Sep 26, 2011 04:00 |
|
Well, in GrumpyDoctor's example you're dealing with a struct, not a class. I agree that it would be very unintuitive if declaring a reference called a constructor somewhere, but the linked code instantiates a struct of the given form on the stack. edit: ^^^ ok yeah, beaten.
|
# ? Sep 26, 2011 04:03 |
|
Internet Janitor posted:Well, in GrumpyDoctor's example you're dealing with a struct, not a class. I agree that it would be very unintuitive if declaring a reference called a constructor somewhere, but the linked code instantiates a struct of the given form on the stack.
|
# ? Sep 26, 2011 04:06 |
|
I am enlightened. In that case, I retract my statement. C++ is pants-on-head crazy.
|
# ? Sep 26, 2011 04:08 |
|
Internet Janitor posted:I am enlightened. In that case, I retract my statement. C++ is pants-on-head crazy. You were completely right, though. There's no C#-ish distinction between reference types and value types in C++ -- any type can be allocated on the stack or the heap as need be.
|
# ? Sep 26, 2011 04:28 |
|
TasteMyHouse posted:Is there a language where you can where you can declare (but not construct) objects on the stack?
|
# ? Sep 26, 2011 04:30 |
|
TasteMyHouse posted:Is there a language where you can where you can declare (but not construct) objects on the stack? C# won't call the default constructor if you just declare an instance. code:
|
# ? Sep 26, 2011 04:49 |
|
Sedro posted:C# won't call the default constructor if you just declare an instance. What happens if you do this? code:
|
# ? Sep 26, 2011 04:54 |
|
TasteMyHouse posted:What happens if you do this? Compiler error.
|
# ? Sep 26, 2011 04:58 |
|
I find that distasteful.
|
# ? Sep 26, 2011 05:00 |
|
Sedro posted:C# won't call the default constructor if you just declare an instance. I'm having a hard time figuring out where that might be in any way useful.
|
# ? Sep 26, 2011 05:08 |
|
|
# ? Jun 6, 2024 09:57 |
|
GrumpyDoctor posted:In C++ the declaration will cause the default constructor to be invoked. I love it how "butt" is this thread's "foo"
|
# ? Sep 26, 2011 05:09 |