Hacker News new | ask | show | jobs
by a3n 3818 days ago
There's nothing about braces that ensures visual cues to nested code. Significant whitespace does precisely that. And eliminates brace placement arguments.

  int func()
  {
  while dosomething()
  {
  dosomething()
  dosomething()
  doanotherthing()
  }
  dosomething()
  }
  
  def func():
      while dosomething():
          dosomething()
          dosomething()
          doanotherthing()
      dosomething()
2 comments

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

I've never used a language where space indentation was used over curly brackets. So question for those who have... When doing so, wouldn't copying and pasting code around potentially cause a lot of accidental issues? Are there some negatives and side effects of the indentation style? Just curious.
If you copy and paste code irrespective of bracket position, you will have the same problems. It turns out not to be an issue.

A big positive of using indentation is that it enforces a visual indication of scope vs. languages where culturally it is normal to find code with lots of giant one-liners and braces on one line.

But overall, you just get used to it and don't notice it. Everyone who yells about languages that use indentation is bikeshedding in the worst way.

Yeah, but you get used to the quirks of your language+editor. Shifting a block right or left isn't hard with a good editor.