Hacker News new | ask | show | jobs
by kazinator 3813 days ago
> There's nothing about braces that ensures visual cues to nested code.

A machine can look at the braces and re-indent the code, so that you don't have to look at the braces. All while you rest assured that the meaning didn't change:

I just popped it into Vim, selected all and hit =:

  int func()
  {
    while dosomething()
    {
      dosomething()
        dosomething()
        doanotherthing()
    }
    dosomething()
  }
Some semicolons are expected so things are a little off.
1 comments

The braces didn't do anything for a human until indentations were added, whether by human or machine. If you lifted those braces out, you'd still know that the intent was (although in C it would be wrong :)

The only thing I miss about braces is being able to jump to the end of a block easily. In python and similar that's a bit harder to do with an editor.

> The braces didn't do anything for a human until indentations were added

Having explicit delimiters (I don't care if it's {}, begin...end, or whatever) provide several benefits that I have often used.

1. (by far the most important) They trivialize the recovery of indentation that has been trashed by a bad/misconfigured/unfamiliar editor or coworker who inserted "\t" characters into the file. Just run indent-region or send the file through indent(1).

2. Moving code is easier. Just kill/yank it to where you want it and let indent-region or whatever re-indent as needed. Without delimiters I have to either re-indent manually or use an editor with a feature that changes a region's indent level. Even with such a feature, I still have to tell the editor "move this two levels deeper". With delimiters, I can leave that work to the editor.

3. The movement advantages you mentioned.

As for the extra work of having to close blocks (including the infamous LISP ")))))" motif), that's what features like electric-pair-mode is for.

Even better, delimiters can convey more information. Color cues have proven to be very effective at conveying important information; syntax highlighting is very popular, even when showing code outside an editor (such as inside a <pre> tag). I find extending these color cues to also show nesting[1] to be almost as useful as basic syntax highlighting. (I'm using rainbow-delimiters[2] in that screenshot).

[1] https://i.imgur.com/xS4y6rT.png

[2] https://github.com/Fanael/rainbow-delimiters

I do like rainbow delimiters. Parens and others show up nicely.
> If you lifted those braces out, you'd still know that the intent was

That's also true if the intent is ambiguous (the braces say one thing and the indentation something else).

Hey, remove the braces and the intent is clear now!

Yes there are advantages to braced blocks. I sometimes miss them, but mostly not.
The language being discussed has parentheses and brackets:

    Vect n a -> Vect m a -> Vect (n + m) a
What's that around n + m, and how eager are you to replace that with multi-line indentation?
That's an expression, not a code block. I would not want to replace that with anything.
> although in C it would be wrong

Meaning that without the braces the code itself would be wrong, in C. Not that C itself is wrong.