5 ms·
As much grief as Python gets for significant whitespace, I think it really helps the readability. Not only does it enforce what should already be good indenting
by mark-r 9y ago
As much grief as Python gets for significant whitespace, I think it really helps the readability. Not only does it enforce what should already be good indenting habits, but it eliminates the noise of braces.
- wickawic 9y agoI always figured the people complaining about the whitespace thing are people whose code I really don't want to be reading anyway
- runevault 9y agoThe problem isn't whitespace period, its that if you aren't careful the significant whitespace can bite you, for example people sometimes accidentally mix tabs and spaces, which can get... ugly. Personally I prefer {} blocks so I don't have to know what sort of white space is creating the indentation, even though I do miss using python sometimes and may come back to it for the right project.
- redblacktree 9y ago> accidentally mix tabs and spaces, which can get... ugly. This is trivially solved. You can find and replace with sed[0] if you want to be fancy, or just use your favorite text editor/IDE. The teams I've worked on have never had a problem with this. There is always an established convention in a project, and if you're breaking the convention -- e.g. because you prefer tabs -- you're doing it wrong, just by virtue of breaking the convention. If your project is new and you need to set a convention, do that. (also, PEP8 recommends spaces, so there is an authority to reference as a tie-breaker) [0]: something like: find ./ -type f -exec sed -i -e 's/ / . /g' {} \;
- devrandomguy 9y agoFurthermore, there is no need for anyone to care what spacing scheme their team uses, after configuring their editor on day 1 with that team. I have Vim convert Python code to tab indentation when opening files, and have it convert to 4-spaces indentation when saving a Python code file. No one should be wasting even a flicker of brain activity over indentation in the 21st century, regardless of how backwards everyone else's preferences are. While you're at it, please also s/\s+$//g on save. Those unintentional trailing spaces really clutter up Git history, and that comprehensive, clean history is what enables the team leads to do their job without bugging you on Slack. It also signals carelessness, or at least a lack of care for personal tools, and has your name attached to it.
- ubernostrum 9y agopeople sometimes accidentally mix tabs and spaces I just tried with three different editors, and literally could not manage to get code in a file indented with a mix of tabs and spaces despite actively attempting to. Who are your co-workers who actually personally have had this happen to them in, say, this decade (not nebulous stories from the internet of "I heard once about this"), and how did they do it?
- runevault 9y agoI know I ran into it at one point, but I haven't slung python in anger in like a decade, so maybe the editors most people are using have started preventing this sort of thing better. Though I do know in Visual Studio (what I do my day to day coding in these days) I've had situations at work where tabs and spaces got mixed together and at some point it finally started complaining but not right away.
- reificator 9y agoWhy is that? My code has whitespace that reasonably closely matches Python's rules regardless of the language. I only say "reasonably closely" because I don't spend enough time in Python to really be sure about any potential gotchas. But like PEP 20 says: > Explicit is better than implicit. Brackets are explicit. Whitespace is implicit.
- kevinmgranger 9y agoIndentation is explicit.
- reificator 9y agoIndentation is implicit, because you can't see the characters that cause it without turning on whitespace rendering. You only can tell it's there by either moving the cursor around or by observing gaps between characters and other characters or characters and the left margin. And the end of a block, the lack of indentation that denotes that, is absolutely implicit.
- agumonkey 9y agoIt does cut a large chunk of issues off. Also helps "canonicalize" idioms. That said you can still have mess from time to time. But that's alright.
- blunte 9y agoLack of braces is visually appealing, but I really miss brace hopping in Vi - % to bounce between open and close brace. That also applies to languages with do/end instead of braces.
- mark-r 9y agoGood point, I use that feature in C++ quite a bit. But I find that my Python programs tend to use smaller blocks, I don't know if it's just because I'm solving smaller problems or because it's easier to break things down in Python.
- dbcurtis 9y agoI've always had the opinion that computers should serve the needs of humans, not the other way around. In languages where white space is insignificant, it is pretty much universal that we use syntactic elements to communicate block structure to the compiler, and white space to communicate block structure to humans. Having two different ways of communicating block structure is: a) redundant and wasteful, and b) a source of bugs. So, if you buy into the idea that having two different ways of communicating block structure is somewhere between an annoyance and a language design defect, and also buy into the idea that computers should serve the needs of humans, then it follows that the compiler should extract block structure from source the same way that humans do. That being: significant white space. The {} languages seem very primitive to me any more.
- qquark 9y agoYou could argue, using the same logic, that when using {} languages, the computer can serve you by automatically doing the indentation anyway. This is the case in practice: I never have to worry about indenting my code in C, as support for that in most editors is great. {} languages have the advantage of allowing you an immediate restyling of the code when the original author (who can be of varying skill level) wrote something you don't like looking at, or when you want to change the style of your own code for various reasons. I find that Python-style blocks generally cause me more mild annoyances than {} blocks. A frequent thing that happens is copy-pasting/moving a line from some place to another with a different depth: In C, I paste and run my automatic indentation, while in python I have to specify manually by how much I want to indent my new code so that it can know within which block I want it. ... And it's not like you could do so automatically: if I want to paste something after the code of an "if ...:" block, the editor cannot know if I want it inside or outside the block. On a more abstract level, I personally prefer a language where the end of a block is explicitly specified by the presence of a visible character rather than by the absence of an (arguably) invisible one, and where the form of the code does not affect its function in any way... I'm not trying to argue that much against the python logic though, because the potential for disastrous style is indeed limited, it has its specific advantages, but I don't think delimiter-based languages can be qualified as more primitive just because of this.
- ufmace 9y agoI've had mixed feelings about it, but I think one of the better things about it is that it forces less experienced scientist and data analysis programmers to use consistent whitespace.