6 ms·
Being someone that has only ever programmed in languages that use brackets, I really, really don't get this white space thing .. it appears, to me at least to m
by xd 15y ago
Being someone that has only ever programmed in languages that use brackets, I really, really don't get this white space thing .. it appears, to me at least to make reading code very difficult. With brackets I can scan a page of code and see the blocks of related code.
What are the advantages to using white space?
- jurre 15y agoWell it's a personal preference of course but in my opinion it actually makes code much more readable. It forces everyone to use the same conventions for indentation and it gets the 'clutter (brackets)' out of the way so you can focus on what's important, the code. Blocks easily identified by their indentation. It might take a little getting used to but I find it very pleasant.
- 4bpp 15y agoI can't think of "forcing everyone to use the same conventions for indentation" as a purely good thing, any more than "forcing everyone to write comments in 18th-century English" would be. Sure, you make it easier for the random onlooker to be prepared to always immediately understand what is going on, but at the same time, the programmer who writes for his own perusal or that of a small team of known others will find themselves restricted by a totalitarian scheme telling them what they can express and what they can't. The assumption that most code is written for most people to read is simply not true, and code that is intended for the masses has always been able to deal with the problem by imposing style guidelines.
- sanxiyn 15y agoI can't understand how forcing coding convention can result in "being restricted by a totalitarian scheme". We are writing in a formal language instead of English. Style is the last place you want creativity. Use you creativity to express algorithms, not brace styles.
- 4bpp 15y agoBut it's not simply a matter of creativity. Apart from contentious instances pertaining to the complete omission of indentation (if (sdfg) { a; b; }), there are also occasional cases where I'd argue that semantically, you have a block, but syntactically, you don't; for instance, I've always very much favoured the approach when writing OpenGL code to indent everything between a glBegin and glEnd to separate the vertex data from the surrounding code and make it immediately obvious how objects in the scene are grouped. I think several indentation-based languages would ignore the initial indent increase here and consider the indent decrease before the glEnd to be the end of the block containing the group of statements...
- sanxiyn 15y agoYes, that is useful. That's why Python has with statements, for example. C++ RAII or Java try-with-resources can be used similarly. http://www.python.org/dev/peps/pep-0343/ http://www.python.org/dev/peps/pep-0343/
- bickfordb 15y agoActually, it's not as totalitarian as you imply. It's just a different way to express blocks of code. Languages which mark blocks using whitespace indentation (such as Python or Haskell) often don't force any particular amount of indentation. Consecutive lines with the same amount of whitespace prefix are considered be a part of the same block. Most projects/companies have indentation/style standards so that code can be communicated across individuals. Once you add in the indentation style, braces look redundant and are just extra noise and chances for error. I must admit, though, totalitarian systems often help people work together :)
- mattdeboard 15y agoBeing someone who has only ever programmed in languages that use whitespace or parens, I really, really don't get the curly braces thing. :) White space is very readable and just as obvious a discernment of codeblocks as curly braces.
- ntkachov 15y agoAs someone who uses both constantly, Whitespace languages and curly braces are the same. For a good programmer, she can take out the curly brace and it should look like a whitespace language. Likewise, a whitespace language would just have braces inserted everywhere there would be an indent change. '{' for forward indent and '}' for backwards indent.
- nickknw 15y agoExactly, I agree. Enough with the bikeshedding! There are much more interesting things about Rust to discuss than this.
- cageface 15y agoThe problem with significant whitespace is that it makes routine tasks like copy & paste and shifting blocks around a tedious and error prone exercise instead of a trivial editor task. Give me instead a syntax that lets my editor completely and correctly reformat an entire file with a keypress. Significant indentation is the #1 thing keeping me at a distance from Haskell and Coffeescript.
- mattdeboard 15y agoWell, I dunno, I've never really had issues with it but, like I say, vast majority of my code has been in Python and Clojure. I'm not arguing against braces, btw, it's just another delimiter like whitespace. It's all just about what kinds of things you're willing to train your brain to do, IMO.
- falcolas 15y agoI've heard this before, and after working in a 250k loc Python codebase for 4 years, I can't say that this has actually ever been an issue. Any decent editor lets you handle block indentation very simply, and it's typically easy to see what the indentation level should be.
- inerte 15y agoI don't know if this will apply to you, but it's also possible to "see" the blocks just by indentation. If you assume that this is possible, and your eyes can get used to read code just by its blocking, then brackets are unnecessary, even superfluous.
- blueprint 15y agoCase in point http://coffeescript.org/ http://coffeescript.org/
- tomp 15y ago> it's also possible to "see" the blocks just by indentation. As long as there are not too many newlines within blocks...
- webjprgm 15y agoIt's like becoming bilingual. When you know only English other languages are completely confusing, but once you learn a second language you can more easily pick out pieces of others. If all you know is {}, then other code is harder to read. Once you learn either a Lisp-like language or a Python-like language then you learn to see code blocks without {}. This is what happened for me. (I was forced into learning Scheme in school. Only after that did I decide that Python was an OK language after all.)
- mindcrime 15y agoMeh... I've written both Python and C/C++/Java, and I disagree that it's possible to reach a point where the indentation completely obviates the need for brackets in identify blocks. That might be true if you always keep blocks extremely short, and if you never have more than a couple of levels of indentation, but in the real world code never quite seems to fit that Platonic ideal. I'll grant, however, that the Python approach isn't quite as bad as I thought it would be, and that it can come close to matching the readability of "code with braces." But, for me at least, braces (and vertically aligned ones at that, dangit) absolutely make code more readable. YMMV, IANAL, HTH, WTFBBQ, etc...
- rdtsc 15y agoWhen I look at code I see structure. Before I even see individual squiggles, brackets, semicolons, I see structures and blocks of code. One code-block indented in another, "oh look this is a huge block here", "these are 3 nested ones" and so on. Brackets don't help me there but indentation does. Let me ask you this, do you already indent your code? If not it is probably a tangled mess and I wouldn't want to continue the conversion with you. But, if you already indent your code, then why do you need brackets? You are wasting space and you are repeating yourself. It is like enforcing that every declaration of "int counter" to be followed by a comment saying "// declaration of counter as an int". The only reason to use brackets is to minify a piece of code or to appeal to C++ or C programmers. So Javascript benefits from having brackets. I guess Rust does too because it is trying to appeal to C++ programmers.
- cageface 15y agoBrackets give your editor the information it needs to automatically infer scoping and to do smart reformatting. Without it you're entirely on your own.
- Symmetry 15y agoAnd my editor (or any good editor I'd argue) can do the same thing with indentation just as easily.
- webjprgm 15y agoWhat counts as "good"? By your definition it's one that can provide helps with indentation-based blocks. :-) I can't depend on having a "good" editor. I have 2 work machines, 2 home machines, ssh to a VPS, ssh to a bunch of machines at work, etc. So as often as not I have to use vim to do quick edits of code. I also am generally forced to use Apple's Xcode for Obj-C applications, but use other editors for anything else. Working on my own toys vs. working at work, vs. being in school instead of a commercial entity with nice software licenses also means I can't depend on having the same text editor available, other than free ones. I'd prefer to use a programming language that was easy to read and write in just about any text editor. The only requirement I can't easily escape is that the editor must support auto-indent. (So MS Notepad is out.)