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
mjau
Aug 8, 2008

Internet Janitor posted:

I just rewrote the whole mess as:
code:
void printsin(const struct sockaddr_in* sin, const char* m1, const char* m2) {
	printf("%s %s:\n", m1, m2);
	printf("  family %d addr %x port %d\n",
		sin -> sin_family,
		sin -> sin_addr.s_addr,
		sin -> sin_port
	);
}

Fixed that for you

Adbot
ADBOT LOVES YOU

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
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

Presto
Nov 22, 2002

Keep calm and Harry on.

Internet Janitor posted:

to this. Does anybody still intentionally write method signatures like that? What is the deal with that hostent pointer?
It's prototyping the gethostbyaddr function. I guess netdb.h didn't exist at the time.

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.

geonetix
Mar 6, 2011


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



in Java

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.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
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.

Orzo
Sep 3, 2004

IT! IT is confusing! Say your goddamn pronouns!
What? Why would you declare variables at the start of your scope? It adds extra lines and confusion.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
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.

Brecht
Nov 7, 2009

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.
Coding horror spotted~

With few exceptions, you should declare variables as near as possible to their first use. Anything else is madness.

edit: what Meat said.

geonetix
Mar 6, 2011


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.

Brain Candy
May 18, 2006

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.

NotShadowStar
Sep 20, 2000

Brecht posted:

Coding horror spotted~

With few exceptions, you should declare variables as near as possible to their first use. Anything else is madness.

edit: what Meat said.

Except for Javascript, which helpfully pulls all declared variables to the top of the scope regardless of where you actually defined them. Fun times.

angrytech
Jun 26, 2009

Brecht posted:

Coding horror spotted~

With few exceptions, you should declare variables as near as possible to their first use. Anything else is madness.

edit: what Meat said.

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).

Dicky B
Mar 23, 2004

Because:
code:
void fun() {
  some_type foo;
  if(something) {
    // do something with foo
  }
  else {
    // don't do anything with foo
  }
  // end of function.
  // we may have paid for the construction of foo even though we didn't use it!
}

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"
Even ANSI C only requires top-of-scope, and not top-of-procedure.

Orzo
Sep 3, 2004

IT! IT is confusing! Say your goddamn pronouns!

Dicky B posted:

Because:
code:
void fun() {
  some_type foo;
  if(something) {
    // do something with foo
  }
  else {
    // don't do anything with foo
  }
  // end of function.
  // we may have paid for the construction of foo even though we didn't use it!
}
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.

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).
It's mainly just readability, I think. Things flow much more logically if you just make declarations as they are needed. Seeing a bunch of declarations at the top of the function do absolutely nothing but waste space, since you can't tell how or when they're being used without scanning the rest of the function.

Qwertycoatl
Dec 31, 2008

I prefer to declare variables at the point of first use, because then it's completely impossible to accidentally use the variable uninitialised.

Vanadium
Jan 8, 2005

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 }?

pseudorandom name
May 6, 2007

I never expected the Coding Horrors thread to theorycraft a new form of coding horror.

Good job!

corgski
Feb 6, 2007

Silly goose, you're here forever.

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.

Primpod
Dec 25, 2007

jamming on crusty white

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.
Yeah. One of the first things you should do when learning JS is to look through the JSlint rules to see why they exist. It's a great language for syntax unexpectedly doing something bizarre.

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.

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"

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 }?
If you want to, sure.

raminasi
Jan 25, 2005

a last drink with no ice

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.

dis astranagant
Dec 14, 2006

More proof that C++ is a horror all its own.

Orzo
Sep 3, 2004

IT! IT is confusing! Say your goddamn pronouns!
I guess I forgot about that. A horror indeed.

baquerd
Jul 2, 2007

by FactsAreUseless

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 :wtf: all the way.

TasteMyHouse
Dec 21, 2006
I find that completely intuitive and I don't understand what is supposed to be :wtf: about it.

Dicky B
Mar 23, 2004

Non c++ programmers talking about c++ :allears:

baquerd
Jul 2, 2007

by FactsAreUseless

TasteMyHouse posted:

I find that completely intuitive and I don't understand what is supposed to be :wtf: about it.

code:
Object foo;
Object foo = new Object();
In the vast majority of languages, these are not equivalent statements or ideas. Declaration and definition/instantiation are separate concepts. There are many reasons you would want a placeholder object reference that doesn't take up memory or have an object assigned to it. Granted, you can do that with C++ pointers but the implementation is very odd.

baquerd fucked around with this message at 03:53 on Sep 26, 2011

TasteMyHouse
Dec 21, 2006

baquerd posted:

code:
Object foo;
Object foo = new Object();
In the vast majority of languages, these are not equivalent statements or ideas. Declaration and definition/instantiation are separate concepts. There are many reasons you would want a placeholder object reference that doesn't take up memory or have an object assigned to it.

In C++, declaring an object like this:
code:
SomeClassType hey_im_an_object;
constructs an actual object of type SomeClassType on the stack. I don't know what you'd want to happen here -- have memory allocated on the stack that you then have to manually construct an object into later?

The C++ equivalent of the Java style stuff you wrote above is:

code:
Object * foo; //a declaration of a handle for an Object that points to nothing
Object * foo = new Object(); //a declaration of a handle for an Object, 
                       //and an initialization of it to point to a new Object
Is there a language where you can where you can declare (but not construct) objects on the stack?

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

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
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.

TasteMyHouse
Dec 21, 2006

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.

edit: ^^^ ok yeah, beaten.
In C++, structs and classes only differ by their default visibility.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
I am enlightened. In that case, I retract my statement. C++ is pants-on-head crazy.

TasteMyHouse
Dec 21, 2006

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.

Plorkyeran
Mar 22, 2007

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

TasteMyHouse posted:

Is there a language where you can where you can declare (but not construct) objects on the stack?
C++ via boost::optional :v:

Sedro
Dec 31, 2008

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:
struct Foo
{
    public int Bar;
}

Foo foo; // Allocates memory on the stack, but struct fields are not initialized
Console.WriteLine(foo.Bar); // Compiler error: foo.Bar not initialized

foo.Bar = 0;
Console.WriteLine(foo.Bar); // Prints 0

Foo foo2 = new Foo(); // Every struct has an implicit (and non-overridable) default constructor
Console.WriteLine(foo2.Bar); // Prints 0

TasteMyHouse
Dec 21, 2006

Sedro posted:

C# won't call the default constructor if you just declare an instance.

What happens if you do this?


code:
struct Foo
{
    public int Bar;
    public float Baz;
}

Foo foo; 
foo.Bar = 0;
Console.WriteLine(foo.Baz); 

Eggnogium
Jun 1, 2010

Never give an inch! Hnnnghhhhhh!

TasteMyHouse posted:

What happens if you do this?


code:
struct Foo
{
    public int Bar;
    public float Baz;
}

Foo foo; 
foo.Bar = 0;
Console.WriteLine(foo.Baz); 

Compiler error.

TasteMyHouse
Dec 21, 2006
I find that distasteful.

corgski
Feb 6, 2007

Silly goose, you're here forever.

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.

Adbot
ADBOT LOVES YOU

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

I love it how "butt" is this thread's "foo"

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