5 ms·
> Spaces are for the human eye, while braces are for the compiler and for the editor auto indenter too. I don't like doing the job of the compiler but this is
by pmontra 10y ago
> Spaces are for the human eye, while braces are for the compiler
and for the editor auto indenter too.
I don't like doing the job of the compiler but this is an exception.
I already wasted more time in a few months of Python by hand aligning and debugging code after I moved it around than it saved time for me by not having to type } or end at the end of the block. Note that Python has a { in most cases: it's a mandatory :
And how about the time lost because of "IndentationError: unexpected indent" when copy-pasting code from the editor to the command line interpreter?
To the point of bashing C, the post is an answer to a very few points of https://eev.ee/blog/2016/12/01/lets-stop-copying-c/ https://eev.ee/blog/2016/12/01/lets-stop-copying-c/ It's mostly about syntax and it's an interesting read. I particularly like "No hyphens in identifiers"
- pjc50 10y agoOh god, this is one of my least favourite parts of python. It is unnecessarily hard to restructure if-then code. It is risky to cut-and-paste from webpages. And as you say, you can't always paste from the editor to the interpreter. Python has an "invisible close brace" character, the newline, which closes a varying number of braces. When you think of it like that, it makes much less sense.
- voyou 10y ago"Python has an "invisible close brace" character, the newline, which closes a varying number of braces" The idea that the end of a block is invisible isn't really right, though. The newline doesn't end the block. The block ends at the next line containing non-whitespace text at a lower indentation level. In other words, it only ends when it becomes visible that it has ended.
- infogulch 10y agoI'm seeing several people complaining about this, but I've always found changing indentation very easy. Just select then [Shift+]Tab to [un]indent. I do this regardless of the presence of braces.
- pjc50 10y agoIt's not that it's hard to change, it's just that you have to keep track of it, and if it's damaged you have to work out what it's supposed to be. In an Algol brace-blocked language (or Pascal, Verilog etc which use begin/end), you can just put stuff in the middle of a block, check that what you've just inserted is balanced, let the editor reformat it, and you're fine. Whereas if you've lost the indentation from if a: print b a = a + 1 then you have to work out all over again whether the second line is part of the block or not.
- infogulch 10y agoAh true. Thanks for the explanation. So maybe what C's missing is a standardized, popular, universal, automatic fmt-on-save. This eliminates its' issue problem with improperly indented code.
- pjc50 10y agoYeah, most projects have a coding standard and you can run indent(1) to enforce it. I don't think it's high on the list of issues with C though.
- robert_tweed 10y agoPerhaps C's biggest mistake (at least in terms of syntax) is allowing fewer braces than it could have. Specifically the fact that statements like if and for which allow blocks, they aren't required, so you can end up with code like this: if(foo) bar; baz; Which, when you throw in a preprocessor has probably caused more bugs than anything other than out-of-bounds pointer errors. Of course the fact that everything, including a block, is a single statement is C is one of the things that gave me my first big "whoa" moment when learning to program, and makes me glad that C was the first "real" language I learned (after Basic). K&R C is far more elegant than people give it credit for, probably because modern, real-world C is full of all sorts of weird stuff (and often egregious preprocessor abuse - yes, I'm looking at you PHP), or people learn C++ first and assume it's all the same.
- OskarS 10y agoI totally agree, the "bare" if/for/while block is a real curse and should never have been allowed. I decided a while ago to always put in braces in C-style languages even for single statements. If nothing else, it just looks better. The code looks more "balanced", i think, an "if" without curly braces looks like it's toppling over or something.
- geoka9 10y agoI suspect this is the reason the Golang designers made braces mandatory even for one-statement if clauses.
- RUG3Y 10y agoIs the above code example, though legal, considered bad practice?
- chelahmy 10y agoEven if it is done correctly with a comma after the 'bar' instead of a semicolon, I think it is a bad practice to separate statements with commas. But if you think in semicolon all of the time and skip some brackets then you will mostly get similar buggy code as the above.
- 10y ago
- dom0 10y agoInteresting. I don't think I ever had a bug due to wrong indentation. I admit that I seldom copy-and-paste code from web pages, but even then I don't remember ever having troubles with it. By no means do I intend to invalidate your experience, just saying that I/some/a lot of people have no problems with this. When discussing Python there are some issues that regularly come up (package boilerplate, complex data model, asyncio), but the indentation isn't mentioned in my experience. I'm doing Python for 4+ years and mostly used (and still do use) C and C++ before that, and some Basics and php before that. What I feel is very important when editing source (not just Python, any source) is to have an editor that isn't entirely bad about handling indentation. Very simple editors just delete/replace a selection when hitting tab. Slightly smarter editors prepend a tab to each line, or indent one level. Emacs knows enough about indenting things that it cycles between possible indentation levels. An editor supporting shift-tab (reverse indenting) is rather helpful. Personally I find it easier to restructure Python code than eg. C code, because I don't have to adjust braces manually. In Python I can just move a block around and, if necessary, move it horizontally to the correct level, whereas in bracelang I often have to adjust braces as well, which are outside the moved section and need extra cursor navigation. Pycharm, by way of it's IntelliJ heritage is pretty good here; when moving code it automatically adjusts the indentation if it is distinct (eg. move a few statements from an `if` to a upper level and it adjusts the indentation automatically to match the surrounding block, since the statements can't possibly be indented due to the lack of a if/for/while/def). CLion can do similar things. The IntelliJ IDEs do these things for any supported language, sometimes across languages (paste some Java code into a Kotlin file - IntelliJ will automatically translate it into Kotlin, adjust the code style and sometimes even rename variables by type to match).
- ant6n 10y agoI never really had issues with copy/cut/pasting code and indentation until I started using pycharm, which _insists_ on randomly re-indenting code on paste, even if you disable it. In Pycharm, 'paste simple' is your friend.
- gpderetta 10y agoFor what is worth, we had two unrelated production bugs because of wrongly indented return statements in python code. Luckily we only use python for non-critical glue code so it was more of an annoyance than anything, but still... For what is worth, for C-like languages, I do not have to indent my code, my editor of choice does it for me according to brace placement.
- Certhas 10y agoIn the command line interpreter you should use %paste, which will paste with the corrected indentations. I'm seriously baffled by people complaining about indentation errors. The indentation is so tightly tied to the meaning of the code, isn't it almost always obvious? Do you use an editor where you have to indent each line individually rather than moving all selected lines with tab/shift-tab?
- pmontra 10y agoI'm using emacs. When I paste I lose the selection. I'll check if there is some special command in the Python mode for that. I'll also google that %paste. Thanks. Edit: I googled that and installed IPython 5.1.0 (sudo pip install ipython) I can paste lines at any indentation level now and they work, or type spaces at the beginning of the line. %paste is not required. I would upvote you again if I could. Thanks again!
- paulddraper 10y ago> and for the editor auto indenter too. The fact that you need an editor that parses C and adds in characters for you should send up a red flag. DRY. I shouldn be working with human-generated code, not redundant machine-generated code. The only program I want writing code for me is my compiler.
- deleted 10y ago[deleted]