9 ms·
>If your language has braces, then you are indenting for the sake of humans (because humans are good at noticing alignment and edges), and bracing for the sake
by okasaki 12y ago
>If your language has braces, then you are indenting for the sake of humans (because humans are good at noticing alignment and edges), and bracing for the sake of computers. That’s double the effort for something as mundane as where a block ends. If they get out of sync, then your code naturally breaks in a way that’s very difficult for you to detect.
No, I use braces for the computer, and the computer automatically and unambiguously indents it for me.
If anything the wasted effort happens in whitespace significant languages like Haskell and Python where the computer can't tell what you want and you have to cycle through all sensible indentations (for every line!)
- vertex-four 12y ago> you have to cycle through all sensible indentations What? My editor indents stuff for me as per PEP8. I don't have to think about it. Why do you?
- okasaki 12y agodef fun(): for x in xs: if a: b c There's no way for an editor to know where to re-indent c to. You have to cycle through the indents manually.
- seanmcdirmid 12y agoAuto indent, tab, and shift tab are your friends. Really, it isn't that bad, considering the curly brace alternative is even worse from an effort perspective.
- okasaki 12y agoTo each their own. I came to C++ from Haskell and found braces to be superior (except visually).
- vertex-four 12y agoHow did you end up with c there, honestly? It's not a thing that's ever happened to me.
- nothrabannosir 12y agoPerhaps I misunderstand, but vim, not a particularly uncommon editor, treats backspace at the start of a line by removing one indent level. So, you're at `b`, you hit enter: next line, same indent level. Now you hit backspace: indent level matches `if`. Again: indent level matches `for`. Type c, there it is. If you had curly braces, you'd use } instead of backspace. Literally the same number of keystrokes, no? Or were you talking about something else and did I misunderstand?
- seanmcdirmid 12y agoThe computer is really quite proficient at inferring block structure from indentation, and if you use a modern editor this structure is manipulated rather effortlessly. The main problem with indent oriented languages is that not everyone is or wants to use a modern editor. Then there are LISPers who think braces everywhere are a swell idea.
- marcosdumay 12y agoWhat's effortless is putting begin and end of blocks symbols only where they matter. Haskell is particularly guilty of creating several different indenting possibilities, and does require constant intervention. Python is less bad, but still requires more effort than C for example.
- seanmcdirmid 12y agoYou really have to design your language around it for it to work well. Haskell has a a lot of syntactic craziness in the name of simplicity.
- masklinn 12y agoWhat? Haskell has a base language expressed in terms of braces and semicolons, and a relatively simple set of layout rules defining how the compiler can insert these braces and semicolons automatically.
- seanmcdirmid 12y agoRight. I meant that Haskell has a whole lot of other weird syntax to consider, that make it hard to read compared to Python. It's use of indentation is not one of the problems.
- codygman 12y agoCharitably speaking, that is very confusing considering you responded to: > Haskell is particularly guilty of creating several different indenting possibilities, and does require constant intervention with: > You really have to design your language around it for it to work well. Haskell has a a lot of syntactic craziness in the name of simplicity. Where "it" would seemingly be indentation, implying Haskell wasn't designed around making indentation simple.
- gnuvince 12y agoI like writing compilers, how do languages like Haskell and Python handle blocks-by-indentation? I figure you have some sort of INDENT token, but how do you know its width? Also, in specifying the grammar, with explicitly delimited blocks you just say `block := OPEN stmt_list CLOSE`. What about with indentation?
- TazeTSchnitzel 12y agoI think they specify "INDENT foo DEDENT" and the lexer tracks indentation and issues INDENT/DEDENT as appropriate.
- falcolas 12y agoThis is correct. A bit of state (an indentation width stack) in the lexer creates the block boundaries as clearly for the interpreter as normal braces does. Any problems in parsing are typically only encountered by stateless parsers.
- dochtman 12y agoI have one (probably quite naive) implementation here: https://github.com/djc/runa/blob/master/runac/parser.py#L54 https://github.com/djc/runa/blob/master/runac/parser.py#L54 (Runa uses tabs for indentation, not spaces, but it's basically the same thing.) Whitespace at the start of a line is a separate token, then I add a thin layer of processing that turns a change of indentation level (i.e. count chars in the token contents) into INDENT or DEDENT tokens.
- zak_mc_kracken 12y agoYou're using a double standard, here: if the computer is good at indenting brace based languages, then it is equally good at indenting languages without braces just based on the grammar.
- mindslight 12y agoNot at all. In this case, by 'languages without braces' we mean languages where indentation is part of the grammar. Python's grammar includes explicit INDENT/DEDENT tokens. It is impossible to reindent this mangled python: if z: print 'a' print 'b'
- adrusi 12y agoWhy would it be improperly indented in the first place? How is removing the indentation any different from removing braces? Sure, when you paste code somewhere that expects prose (Facebook status updates, for example), it will consolidate consecutive whitespace and thereby remove the indentation, but you shouldn't be pasting code into a place like that anyway, because no matter what you do it'll be unreadable, and most people aren't going to be interested enough to put it into their editor to autoformat.
- mindslight 12y agoI did not make the value judgment you are arguing against.
- mixmastamyk 12y agoEditors know that a python line ending in a colon must be indented and so indent it automatically. It wouldn't be hard to use the same method to reindent it.
- unwind 12y agoNo, lines following a line ending in a colon must be indented. But the OP's point is that you can't know how many lines make up the indented block, since there is no explicit block-end-marker left if the indent is gone.