News for hackers. Stuff that matters.
Re:Let’s Stop Bashing C by pmontra on Friday December 02, 2016 @05:37AM (#13087569)
> 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/ It's mostly about syntax and it's an interesting read. I particularly like "No hyphens in identifiers"

Re:Let’s Stop Bashing C by robert_tweed on Friday December 02, 2016 @05:59AM (#13087651)
Perhaps 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.

Re:Let’s Stop Bashing C by OskarS on Friday December 02, 2016 @06:55AM (#13087878)
I 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.
Re:Let’s Stop Bashing C by geoka9 on Friday December 02, 2016 @11:01AM (#13089656)
I suspect this is the reason the Golang designers made braces mandatory even for one-statement if clauses.
Re:Let’s Stop Bashing C by RUG3Y on Friday December 02, 2016 @12:29PM (#13090531)
Is the above code example, though legal, considered bad practice?
Re:Let’s Stop Bashing C by nkurz on Friday December 02, 2016 @02:36PM (#13091576)

  if (foo)
     bar; baz;
Is the above code example, though legal, considered bad practice?

Yes, misleading indentation like this is bad practice for anyone who believes that there is such a thing as "bad practice" exists. For clarity to those who do not know C, the problem is that "baz" is executed unconditionally, even though the formatting misleadingly implies that it depends on "foo".

Unfortunately (in my opinion) there is not consensus on whether the following is bad practice:

  if (foo)
     bar;
My belief is that this formatting should be avoided, to avoid the case where someone not familiar with the rules of C (or not thinking about them) edits it to add a manually indented "baz" on the next line:

  if (foo)
     bar;
     baz;
Personally, I'm fine with a single line without braces:

  if (foo) bar;
But I believe that as soon as the body is moved to another line, braces should be required:

  if (foo) {
     bar;
  }
This belief is common, but not universally shared. Some go farther and say that braces should always be required (there is a good argument for this). Others say that a two line if statement without braces is just fine (I think they are wrong).
Re:Let’s Stop Bashing C by RUG3Y on Friday December 02, 2016 @03:27PM (#13091989)
Thanks for your informative answer.
Re:Let’s Stop Bashing C by chelahmy on Friday December 02, 2016 @01:58PM (#13091267)
Even 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.
Re:Let’s Stop Bashing C by flukus on Sunday December 04, 2016 @08:52PM (#13102895)
Does anyone do that in practice? In 10+ years I've never seen a single bug from braceless ifs.
Re:Let’s Stop Bashing C by pjc50 on Friday December 02, 2016 @05:49AM (#13087616)
Oh 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.

Re:Let’s Stop Bashing C by voyou on Friday December 02, 2016 @06:43AM (#13087833)
"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.

Re:Let’s Stop Bashing C by infogulch on Friday December 02, 2016 @11:39AM (#13090041)
I'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.
Re:Let’s Stop Bashing C by pjc50 on Friday December 02, 2016 @12:31PM (#13090547)
It'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.
Re:Let’s Stop Bashing C by infogulch on Friday December 02, 2016 @02:53PM (#13091721)
Ah 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.

Re:Let’s Stop Bashing C by pjc50 on Friday December 02, 2016 @03:07PM (#13091850)
Yeah, 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.
Re:Let’s Stop Bashing C by dom0 on Friday December 02, 2016 @06:00AM (#13087653)
Interesting.

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).

Re:Let’s Stop Bashing C by gpderetta on Friday December 02, 2016 @06:18AM (#13087732)
For 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.

Re:Let’s Stop Bashing C by alvin0 on Saturday December 03, 2016 @03:31AM (#13094978)
> 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.

Could you clarify; I think adding one/two more line to the block being moved makes this a similar operation. For that matter, the editor can unambiguously figure out the block and do auto-indentation when braces are present.

Re:Let’s Stop Bashing C by ant6n on Friday December 02, 2016 @10:33AM (#13089360)
I 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.
Re:Let’s Stop Bashing C by Certhas on Friday December 02, 2016 @06:09AM (#13087693)
In 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?

Re:Let’s Stop Bashing C by pmontra on Friday December 02, 2016 @09:20AM (#13088684)
I'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!

Re:Let’s Stop Bashing C by paulddraper on Friday December 02, 2016 @10:30AM (#13089341)
> 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.