Live data from Hacker News

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

lkml.org

21–30 of 140 posts

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

#21
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 even in C. You can have an if condition that is actually a macro that expands with parentheses. For example the macros in ctype.h, so you can actually write

  if islower(c) {
      (..)
  }

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

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

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

#24
post #15
post #9

Earlier quoted context omitted.

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.

In Go, however, go fmt will remove the unnecessary parentheses. I was very annoyed at first, when I saw this, but I got used to it.

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

#25
post #7

Earlier quoted context omitted.

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.

So in practice it works more like a C Macro then a C function?

Edit, except it doesn't treat arguments the same, so no.

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

#26
post #21
post #9

Earlier quoted context omitted.

Doesn't `if` always require parenthesizes?

Not even in C. You can have an if condition that is actually a macro that expands with parentheses. For example the macros in ctype.h, so you can actually write if islower(c) { (..) }

Yeah, but macros are not really part of the language, they're more a writing aid than anything really.

So, for the sake of good practices, write if with parenthesis.

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

#28
post #21
post #9

Earlier quoted context omitted.

Doesn't `if` always require parenthesizes?

Not even in C. You can have an if condition that is actually a macro that expands with parentheses. For example the macros in ctype.h, so you can actually write if islower(c) { (..) }

That is cheating!

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

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

> it is not a function from the standard's point of view is that C does not have any builtin functions

The problem with sizeof is that is should be able to accept a type as an argument. No function in C can do that, according to the standard C grammar.

Somewhat similarly, the standard va_arg is a macro, not a function, also because it accepts a type.

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

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

I put it on the line that makes the code following it the most clear
Post reply on HN