Hacker News new | ask | show | jobs
by Retra 4037 days ago
What advantage does 'visually balanced' give? That's like saying the letter A is better than B because it's 'stands up better.'

Indentation is how you should identify blocks.

1 comments

Indentation to identify blocks can break down in things like switch statements:

switch( c ) {

   case 'A':
     simple_stuff();
     break;

   case 'B': {
     int temporary_variable = 0;
     complex_stuff( temporary_variable );
 }
To me, the advantage of braces on newlines is it makes it extremely easy to tell where blocks start and end---whether you're using your favorite IDE, or reading the code on a blog, or reading the code with "cat". I actually think it's a case where Python has the design advantage on C (since if you do braces-on-newline consistently enough, you end up duplicating python but with superfluous braces added in).
Fundamentally, the problem is that braces are needed at all. Humans are bad at matching them up, so they don't match the humans' intention when indenting. Confusion.

It's all backward. The IDE should SHOW you the blocks somehow (background tone changes etc), and let you edit the blocking explicitely. If they look wrong, you select and hit a key. Now you're in agreement with the compiler.

Yeah, this is one thing Python got right. You should indent anyway, and then the curly braces aren't needed anymore.

I don't know what the point is in pretending IDEs or advanced text editors don't exist.

I'm not seeing what the issue here is, besides that this won't compile because of mismatched braces. There is no need to put braces around the contents of a case block. Unless I'm switching on an enum, I rarely find a compelling reason to use switch/case, and there's usually a cleaner way to do it.
"There is no need to put braces around the contents of a case block."

There is here.

In some dialects, you can only declare a variable at the start of a block. From the perspective of the compiler, a "case block" isn't actually a block, just stuff between labels. In order to declare temporary_variable, it may be necessary to put the braces, and it is probably best practice as temporary_variable may otherwise be exposed to later cases (and in C++, a jump over a variable declaration seems to produce an error).