3 ms·
And don't forget: if(a) if(b) x; else y; This doesn't do what the indentation implies. (http://en.wikipedia.org/wiki/Dangl
by jacobparker 13y ago
And don't forget:
if(a)
if(b)
x;
else
y;
This doesn't do what the indentation implies. (http://en.wikipedia.org/wiki/Dangling_else http://en.wikipedia.org/wiki/Dangling_else)
Walter Bright (creator of D, etc.) has an article where he discusses his interesting choice to make that grammar form illegal in D. He mentions that after doing this he found a bug in D's runtime library. http://www.drdobbs.com/cpp/dangling-else-yet-again/231602010 http://www.drdobbs.com/cpp/dangling-else-yet-again/231602010
- angersock 13y agoJust see that made me foam at the mouth a little...grr. We generally don't even like have non-curlied one-liners in our conditionals.
- mercurial 13y agoThis makes a good point for Python syntax, where you can't by construction end up in situations like that.
- jacobparker 13y agoNote that any static analyzer that is worth something will warn about this in C/C++. For example, clang will warn with the option -Wdangling-else (implied by -Wall)
- maw 13y agoTrue, but the flip side is that in Python automatic indentation cannot possibly work.
- safod 13y agoWhy not? vim indents one more when encountering a colon at the end of a line. It also de-indents when encountering a return or break statement. Of course in all other cases you have to de-indent manually, however, hitting backspace once is not more work than typing a closing curly brace.
- maw 13y agoWhat I mean is that you can't tell your editor to indent an arbitrary block of code.
- sp332 13y agoRight, because a block of un-indented python is syntactically incorrect. It would be just like stripping all the brackets out of some C code and asking the editor to add brackets to the resulting mess properly.
- papsosouid 13y agoAs maw points out, that is not a pure win, it is a tradeoff. And it is a tradeoff that I've found to be a really bad one. Being able to run indent over your code and have it automatically formatted is really helpful in large, long term projects where people come and go and weren't effectively forced into using one style.
- Luyt 13y agoYou can do that with Python code, too. https://github.com/hhatto/autopep8 https://github.com/hhatto/autopep8
- papsosouid 13y agoSort of, but not quite. It is quite easy to get situations where code is wrong because of indentation mistakes, copy+paste, etc. All autopep can do is make your wrong code look consistent. Languages with some sort of block delimiter can't get wrong in this manner, so it is safe to munge together random sources with various indenting messes and then just run a tool over them.