Earlier quoted context omitted.
I put it on the line that makes the code following it the most clear
How do you handle auto-format? If a rules doesn't survive that, its not practical, at least for me.
Humans should think of sizeof() as a function, says Linus Torvalds
41–50 of 140 posts
Re: Humans should think of sizeof() as a function, says Linus Torvalds
#42IIRC, if the argument to sizeof is a type, the parentheses are mandatory, anyway, so using it like it was a function is more consistent. I think the reason it is not a function from the standard's point of view is that C does not have any builtin functions (unless my memory totally fails me in this case), all functions have to be either defined locally or #included.
In C there are no functions that take a type as argument. So sizeof(type) is a special case anyway. I'm all for using sizeof like a function, but that doesn't make it consistent. sizeof is just a special syntactical construct. I like to think that sizeof is called an operator just for syntactic convenience much in the same way as typedef is a storage-class specifier.
int *foo;
// code
foo = malloc(sizeof(int));
a few months later, change foo to be a double. Code still compiles, no warning, but you're allocating half the memory you need.Re: Humans should think of sizeof() as a function, says Linus Torvalds
#43Earlier quoted context omitted.
I put it on the line that makes the code following it the most clear
How do you handle auto-format? If a rules doesn't survive that, its not practical, at least for me.
Re: Humans should think of sizeof() as a function, says Linus Torvalds
#44IIRC, if the argument to sizeof is a type, the parentheses are mandatory, anyway, so using it like it was a function is more consistent. I think the reason it is not a function from the standard's point of view is that C does not have any builtin functions (unless my memory totally fails me in this case), all functions have to be either defined locally or #included.
Another oddity of C that amuses me is the do/while loop without braces: int i = 4; do printf("hey\n"); while (--i > 0); Even though do/while is a keyword bracketing pair in C, it still only lets you use a single statement (because nested whiles). So everybody uses braces, and thus it looks quite disturbing without them.
Remove the newline and it looks ok to me:
int i = 42;
do printf("hey\n");
while (--i > 0);
Another possibility would be: int i = 42;
do printf("hey\n");
while (--i > 0);Re: Humans should think of sizeof() as a function, says Linus Torvalds
#45Ah, but in Haskell, `return` is a function, though it shares only a few similarities with with its counterpart in C-like languages.
However, when using continuations or a continuation passing style, the continuation is a function that behaves almost exactly like traditional return when called!
(define (multiply x y return)
(return (* x y)))Re: Humans should think of sizeof() as a function, says Linus Torvalds
#46Earlier quoted context omitted.
Exactly. I heard people arguing on the Internet that you should not add parentheses when sizeof is applied to an expression, only a type. I just cannot understand why they bother. Just add a parenthesis and it is always right. Much less cognitive burden.
This is the same people that find it "better" to write JS without the semicolons.
Re: Humans should think of sizeof() as a function, says Linus Torvalds
#47Re: Humans should think of sizeof() as a function, says Linus Torvalds
#48Earlier quoted context omitted.
This is the same people that find it "better" to write JS without the semicolons.
I don't believe its equivalent. When I'm writing Scala code in the backend and have to switch to frontend I often find myself omitting the semi-colons but going back and fixing due to self-imposed coding conventions. For Javascript just as in Python or Scala, semi-colons are optional for a good reason.
It's not about forgetting them once in a while, it's about playing a game of "needs a semicolon or doesn't" which increases the (already high) number of things the developer needs to worry about
Re: Humans should think of sizeof() as a function, says Linus Torvalds
#49What kind of monsters don't use parentheses on sizeof. Is it the same guys that put { on new lines and have giant comment templates for every one line function.
{ on new lines seems to have dominated the majority of code I've worked with although I've always preferred putting it on the same line myself.
To me its more about conserving an extra bit of vertical space... I like my methods/functions quite compact. In the past I liked the balanced nature of the braces though.
Makes absolutely no appreciable difference though of course, as long as you don't get into a formatting war with your team.