Live data from Hacker News

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

lkml.org

11–20 of 140 posts

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

#11
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.

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.

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

#12
post #9
post #8

I'd like to confess that I'm one of the return() people, and I also do if() even in cases where the language doesn't require it. This behavior isn't borne out of confusion about what is and what isn't a special language construct. Rather, it helps me prop up the illusion that programming is about using a few simple primitives instead of being the chain of compiler directives it actually entails - it's an esthetic cho…

Doesn't `if` always require parenthesizes?

Yes, in C, but I believe Udo was talking about other languages (like Go for instance)

> and I also do if() even in cases where the language doesn't require it

This sentence could be parsed in multiple ways though :)

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

#13

Who the hell doesn't think of `sizeof` as a function?

It's an unary operator, with 2 forms:

   sizeof(type)
or

  sizeof expression
but e.g. unlike a function, it doesn't evaluate the expression.

    sizeof(my_function()) doesn't call my_function.

    sizeof(a = 12) doesn't assign 12 to a.

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

#14
post #9
post #8

I'd like to confess that I'm one of the return() people, and I also do if() even in cases where the language doesn't require it. This behavior isn't borne out of confusion about what is and what isn't a special language construct. Rather, it helps me prop up the illusion that programming is about using a few simple primitives instead of being the chain of compiler directives it actually entails - it's an esthetic cho…

Doesn't `if` always require parenthesizes?

Not in all languages. For example, Go and Swift don’t require it.

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

#15
post #9
post #8

I'd like to confess that I'm one of the return() people, and I also do if() even in cases where the language doesn't require it. This behavior isn't borne out of confusion about what is and what isn't a special language construct. Rather, it helps me prop up the illusion that programming is about using a few simple primitives instead of being the chain of compiler directives it actually entails - it's an esthetic cho…

Doesn't `if` always require parenthesizes?

No, not always. Not in Go, or in Rust, or Ruby, for example. The assumption being in these languages that if is followed by an expression and that expression is parsed to its natural end anyway - but this only works if the syntax allows for that end to be found without ambiguity.

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

#16
post #7

Who the hell doesn't think of `sizeof` as a function?

Well it's an operator, and algebra said all them are just functions... Just like `+` is a function Anyways not all those writing code have CS background

Except sizeof is a compile-time operation, not runtime. If sizeof were a function,

    int *foo = malloc(sizeof(*foo));
would make no sense.

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

#19
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.

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

#20

Who the hell doesn't think of `sizeof` as a function?

It's an unary operator, with 2 forms: sizeof(type) or sizeof expression but e.g. unlike a function, it doesn't evaluate the expression. sizeof(my_function()) doesn't call my_function. sizeof(a = 12) doesn't assign 12 to a.

So is "sizeof(a = 12)" equivalent to just "sizeof(a)"?
Post reply on HN