Hacker News new | ask | show | jobs
by bobbylarrybobby 1977 days ago
For as long as I live, I’ll never understand style guides that permit omitting brackets around a single line following an if statement (or for, while, etc), nor code formatters that dont automatically insert them.
5 comments

I'm so glad that modern languages are opinionated and have default formaters that come with them. Jumping from one C codebase to another is really a nightmare, especially if you want to review code. That's a security issue in my book, as you end up with more reading complexity.
I'm convinced the GNU style is just for gatekeeping at this point. You REALLY have to want to work on a codebase with that after reading anything remotely Linux inspired.
For as long at I live I'll never understand why why there wasn't a test suit to make sure that module and especially THAT module worked correctly.

Changes were made to that module without tests being run and then it was put into production code.

They add visual noise. The grammar of C is not the same as the grammar of it's offshoots and block statements aren't part of control structures. Moreover, GCC warns about extra statements with the same indentation level with -Wall on.
>They add visual noise.

Which is a good thing, since it prevents errors such as this one. Extra verbosity with mandatory braces is noise (to the degree that without it, the compiler could still infer what we mean), but some degree of verbosity is a good thing, as it helps make patterns in the code more evident.

In which case we might want to call it a more fitting error than the dismissing "noise".

How about "dither"?

IMO you get accustomed to single statement-braces very quick and they leave less room for errors.
That is probably why styles like

  if (cond) {
    // statement
  } else {
    // statement
  }

existed. Allow you to quote everything while not waste so much vertical space.
and remove bugs
I agree - it should be a compile or runtime error.
Well if you compile with `-Werror -Wall` (and you should), it would throw in error in GCC 6+:

https://developers.redhat.com/blog/2016/02/26/gcc-6-wmislead...

It can make code bloated and harder to follow.

TBH it would be nice if someone made C with indent scoping instead of block scoping but I don’t think that’s truly practical with the preprocessor.

What's the practical difference between:

   if (foo)
      bar;
   else
      baz;
   
and:

   if (foo) {
      bar;
   } else {
      baz;
   }
The latter has one extra line, which is hardly bloat. It also has the braces, of course, but the upside of this style is that it's clear where the compound statement begins (it's the line that doesn't start with a brace!), and where it continues.
harder to follow? In what sense? Because in my book the fact that you can write both the line with braces and without make it much harder to think about, whereas in most other languages I don't need to think about it because there's only one way to write that.