posted in Technology
Name before type: why 'age int' is better than 'int age'
benhoyt.com/writings/name-before-type/posted in Technology
Name before type: why 'age int' is better than 'int age'
benhoyt.com/writings/name-before-type/Replying to @lemmydividebyzero@reddthat.com
declaring multiple variables is less error-prone than in C. In C, the following declares x to be a pointer, but (surprisingly at first!) y to be a normal integer:
int* x, y;Whereas the equivalent in Go does what you’d expect, declaring both to be pointers:
var x, y *int
I don’t think this is a related at all. C could have easily decided that the definition makes both x and y pointers. They just decided not to so that you can declare more variables on one line by being able to do int x, *y, **z, …. It is more flexible.
Similarly that Go line could have been parsed like var x, (y*) int if they wanted to. They just made a different choice.
Replying to @kevincox@lemmy.ml
That’s why you write
int *x, y, **z;
C doesn’t write like “x is an int-pointer”, rather it says “dereferencing x will get you an int”.
Replying to @kevincox@lemmy.ml
That’s because declaring several variables on the same line is syntactic sugar, although I agree it makes little sense this way