Live data from Hacker News

Humans should think of sizeof() as a function, says Linus Torvalds

lkml.org

41–50 of 140 posts

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#42
post #6

IIRC, 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.

And you really shouldn't use it with a type if you can avoid it anyway, it makes code brittle e.g.

    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

#43
post #30

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.

I've never met an auto-formatter that I haven't hated the output of enough to not use it.

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#44
post #31
post #6

IIRC, 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.

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

#45
> "return()" is in no way a function.

Ah, 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

#46

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

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.

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#48

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

Well, semicolons may be optional in Python but the official python style strongly recommends agains (needing to use them)

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

#49
post #17

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

Most C++ codebases I've seen have the brace on a fresh line, most Java codebases at the end of the line (at a guess born of the Sun guidelines and default Eclipse formatting perhaps). I started with the former, but prefer the latter now.

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.

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#50
I respect Linus greatly but the way he to talks to his fellow 'humans' is insanely confrontational, rude and disrespectful. It overshadows his arguments (which are usually very good) and throws people on the defensive. I'm grateful for all he has accomplished and shared with us but I imagine working with him is 'hell'.
Post reply on HN