3 ms·
return i++ is not ambigous. It will return the current value of i, e.g. if i was 2 it will return 2, not 3. But that's just how the post increment operator work
by einaregilsson 15y ago
return i++ is not ambigous. It will return the current value of i, e.g. if i was 2 it will return 2, not 3. But that's just how the post increment operator works (in every language that I know of), that has nothing to do with semicolon insertion.
- eropple 15y ago"return i++" is not ambiguous, on that you are correct--but it is terrible practice because it lends itself to being misconstrued. Writing clear code is superior to writing "cute" code, and "return i++" is a case of the latter. In any case where the increment actually matters you are directly affecting a shared state and such changes should be made contextually obvious to the state of the object--one's neck would need to be very, very beard to think that this is immediately obvious and apparent in a contextually valid way. Similarly, for many programmers, myself included, a semicolon is read as a period is in English--the end of the statement. (You keep saying that "but people don't get annoyed at Python when it does it," and guess what? I certainly get annoyed at it--Python consistently frustrates me because I find it considerably less readable.) Omitting semicolons is less immediately clear than including them and, again, can be misconstrued. Only it can be misconstrued by the program interpreter, too. (Oops.)
- einaregilsson 15y agoWell, at least we agree that 'return i++' is terrible practice (although it has nothing to do with semicolons) :) But when you write Python code, do you then end every line with a ; ? To make it more readable for yourself?
- eropple 15y agoI prefer to avoid Python entirely, but there are cases where I have to fiddle with preexisting code or the nature of the problem makes more sense to use it, i.e. a library already exists that doesn't elsewhere. In those cases, I write idiomatic Python, without semicolons, because That's The Way You Write Python. Consistency is important, and the Python community has essentially said "don't use semicolons." The JavaScript community has essentially said "use semicolons aggressively". I write code according to those idioms so other people are able to more easily use it.
- snorkel 15y agoIt is ambiguous when the coder intended to return directly without returning any value like so: while (word = getNextWord()) { doSomething(word) if (i > limit) return i++ } Sure, this can be written better but a semi-colon after "return" makes it less ambiguous.