6 ms·
I've never understood the love for significant whitespace. I see where the idea came from - "No more missing semicolon errors! Woohoo!" - but it should've been
by _bxg1 7y ago
I've never understood the love for significant whitespace. I see where the idea came from - "No more missing semicolon errors! Woohoo!" - but it should've been clear after trying out the idea for five minutes that it was not at all worth it. It constantly causes trouble and all just to save a couple of keystrokes.
Though: now that I think about it, most of the problems happen at the block level, not the line level. So maybe significant newlines are fine but not indentation.
- ludamad 7y agoMaybe after trying it out for five minutes you make up your mind - but mine was decidedly for it
- thelazydogsback 7y agoYeah, I'd much rather look at Python or F# than Java code all day.
- cannam 7y agoF# uses significant whitespace? I didn't know that. What does it use it for? SML and (I think) Ocaml don't, and I had assumed F# was like Ocaml, since its syntax is largely the same. In the tiny bit of F# I tried writing once I don't remember having had to pay any attention to whitespace. Significant whitespace is one of the things I didn't enjoy about Haskell and Idris. Life's too short to indent code by hand.
- _bxg1 7y agoI didn't think Haskell had significant whitespace; it doesn't have blocks really, and nested expressions can be wrapped in parentheses, right?
- jeremyjh 7y agoWhitespace is significant in Haskell. In do notation and other forms, you have blocks. You can actually use braces and semi-colons instead but I've never seen that except in some very short one-liners. https://en.wikibooks.org/wiki/Haskell/Indentation https://en.wikibooks.org/wiki/Haskell/Indentation
- desert_boi 7y agoBlocks are handled via indentation. let function1() = for i in 1 .. 10 do printf "%d " i printfn "" function1() borrowed from https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/loops-for-in-expression https://docs.microsoft.com/en-us/dotnet/fsharp/language-refe...
- viklove 7y agoJava isn't a scripting language, python is. Why not compare apples to apples? At the very least compare Python to JS...
- throwaway_pdp09 7y agoAlways liked the idea of whitespace-only indenting but always suspicious of it. Problems I've found are that automatically generated code can't just be dumped in with { and } around it. A colleague spent a few hours tracking down a python bug caused by a bad merge that messed up the indent without it outright causing the code to crash, just produce wrong output. That's IMO too expensive a bug type to permit just to save a few keystrokes. But YMMV as they say.
- _bxg1 7y agoAnd then there's tabs vs spaces (you can't tell just by looking whether or not the syntax is correct), and the fact that a block expression can't be nested inside anything else, etc.
- zenhack 7y ago+1 for the bit about machine generated code. I think if you're going to have significant whitespace, Haskell does the right thing here: the indentation rules are specified in terms of automatic insertion of braces and semicolons. Almost nobody is them in by hand, but they make codegen easier. Re: the bad merge, that can happen the other way to: this was the cause of Apple's infamous goto fail bug. I think block structured languages should do what go and some others do: braces on conditionals and loops are mandatory, parens are optional.
- mixmastamyk 7y agoIt is a great deal, millions of keystrokes and code reading impediments avoided over decades vs. one exception a colleague of someone on a discussion board had once. That’s a 1000x advantage in my book. Not mention exceptions are often avoided with a professional editor with whitespace highlighting and indentation guides.
- viklove 7y ago> one exception a colleague of someone on a discussion board had once You're not even trying to have a reasonable discussion about this, are you. At least this guy has a datapoint. You just have a hunch that you can write code faster if it's formatted slightly differently. I don't think it's true -- I don't think typing semicolons and curly braces slows down my coding enough to warrant omitting them. Most of my time coding is spent thinking and reading, probably less than 15% of my time is spent typing. So if I only type semicolons and commas 5% of that time, that means I'm saving 0.75% of my time by not typing them, which is about 14 hours a year if I work 40 hour weeks. I've definitely debugged a yaml error or two due solely to formatting in the past year, which probably cost me at least 4 hours of productivity. If my entire codebase was formatted solely by indents and returns, I imagine that number would balloon significantly, which is why I opt to use semicolons and other formatting characters. Also, they're not "reading impediments," at least not universally. Being able to use my code-editor to jump to the end of a code block (by matching a { or }) has been immensely useful in my experience.
- ChrisSD 7y agoWhitespace is always significant. At least indentation is. If not for the program, than for the programmers. Apple's infamous "goto fail" was in part caused by a mismatch between how the computer understand the block level and how the programmer did.
- profsnuggles 7y agoBut if the goto fail code had been run through an automated code formatter it would have been more obvious because the human indenting error would be fixed. Is it possible to write a formatter that would fix an issue like that for yaml or python for example?
- ChrisSD 7y agoIf it were written in Python that particular category of bug simply could not exist. The human perceived block level always matches the machine read block level. In short, there's no issue to fix.
- mannykannot 7y agoCertainly not for python, which, for one thing, allows the nesting of 'if' with optional 'else'.
- falcolas 7y agoYes. There are two indicators for parsing Python blocks: indent and dedent. They're the same as { and }, and easily parsed by a machine. Just not by a regex, which so many formatting tools rely upon.
- profsnuggles 7y agoBut if I have if (test) do this; if (test) do that; and run it through a prettifier I end up with if (test) do this; if (test) do that; (Possibly I don't write a lot of c, and when I do I always use {} even if it's a single line statement) In python if I had a similar mis-indented construct how could you possibly know if it is supposed to be a nested if statement or not? I'm not picking on python I'm curious. My preferred language is clojure. I feel like the python whitespace is probably like lisp parens. It doesn't matter after the first week of learning. But when I'm typing clojure in emacs I don't care about formatting my code at all. Emacs sees the parens and indents correctly for me whenever I hit enter. I could probably write all my code on one line, run it through a code formatter and get a reasonable result. On the rare occasion I'm fixing some python when I start a new line I sometimes need to think about indenting and tab cycle until I hit the right depth.
- williamstein 7y agoI used to love significant whitespace before prettier (and gofmt, yapf, etc.) came along. Prettier really is a total game changer for the whitespace versus braces debate. Now I constantly "canonically" format my code as I'm working on it (via a keyboard shortcut), so I don't have to think about or be misled by indentation anymore. In particular, this quote from ChrisSD below is false for me, since I never look at code (my code or other people's) without canonically formatting it first: "Whitespace is always significant. At least indentation is. If not for the program, than for the programmers." Even if browsing a library from Github, I'll first clone the code, then format it, then look at it.
- auggierose 7y agoAll you are saying is that whitespace is so significant for you that you use an extra tool to produce it ... like always.
- sullyj3 7y agoThe benefit of significant whitespace is that you can't write code like this: int foo() { a(); b(); c(); if (test()) doSomethingIfTest(); doSomethingUnconditionally();}
- _bxg1 7y agoMost modern editors will actively make it difficult to do this, and some even do format-on-save to fix it across the board.
- viklove 7y agodef foo(): a() b() c() if test(): doSomethingIfTest() doSomethingUnconditionally() You can still write this, which is equally garbage because a, b, and c mean nothing to me. I know you're just showing a code example, but my point is that unprofessional idiots will always write shit code, the language won't protect them from themselves. Your issue can be fixed with a linter, mine can't. Look at this python code: https://github.com/django/django/blob/master/django/core/mail/message.py#L193 https://github.com/django/django/blob/master/django/core/mai... In my opinion, it's rather difficult to tell where one if/else block ends, and the next begins. At a glance, I couldn't even tell you how many if/else statements there are. In my opinion, braces give me a frame of reference for my eyes to lock into, even in my periphery, and give me a far better understanding of the code structure.