News for hackers. Stuff that matters.
Re:Avoid Else, Return Early (2013) by alunchbox on Monday March 26, 2018 @09:48AM (#16678820)
python fans are going like "what are braces?"
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @11:01AM (#16679399)
The irony is that Python actually has open-braces, but they're spelled ":" instead of "{". And the syntax effectively enforces K&R style.

When I write Python I end every block with a "pass" statement so that emacs can auto-indent my code properly. The "pass" statement thus effectively becomes a close-brace. It drives Pythonistas into conniptions, but I never have to worry about reverse-engineering a block of code to figure out how to restore the proper block boundaries after a cut-and-paste has screwed up the indentation.

Re:Avoid Else, Return Early (2013) by kamaal on Monday March 26, 2018 @12:00PM (#16679846)
That's the bad part of not having braces.

If boundaries exist they need to be clear. One shouldn't have to count the tabs that make up the level of indentation.

Its already a challenge reading code. Counting invisible tabs makes it even worse.

Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @12:16PM (#16679973)
Well, you don't have to literally count them, you use the spatial reasoning hardware built in to your retina and visual cortex to just see that this bit of code is further over to the right than that bit of code. But of course that only works for pieces of code that are small enough to fit in your field of view.
Re:Avoid Else, Return Early (2013) by p1esk on Monday March 26, 2018 @03:07PM (#16681550)
All code pieces should be small enough to fit in your field of view :)
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @03:16PM (#16681619)
Try to tell that to my current client :-(
Re:Avoid Else, Return Early (2013) by dorgo on Monday March 26, 2018 @01:56PM (#16680959)
On the other hand high counts of invisible tabs may be a hint that the code needs refactoring..
Re:Avoid Else, Return Early (2013) by tomn on Monday March 26, 2018 @05:49PM (#16682954)
Enforces K&R style? I don't know what you mean...

    def foo(bar)\
    :
        if (bar > 5)\
        :
            return bar + 1;
        #
        else\
        :
            return bar - 1;
        #
    #
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @05:59PM (#16683031)
I'm not sure whether to laugh or cry
Re:Avoid Else, Return Early (2013) by true_religion on Monday March 26, 2018 @12:40PM (#16680196)
Python specific IDEs will smartly indent code upon copy and paste.
Re:Avoid Else, Return Early (2013) by lisper on Monday March 26, 2018 @12:58PM (#16680398)
Sure, but if you somehow manage to screw up the indentation then no IDE can possibly restore it for you because the information needed to do it is lost. And there are a LOT of ways that indentation can get screwed up. If your code ever leaves the Python ecosystem (e.g. gets published on the web) then all bets are off because the entire digital world outside of the Python-sphere is built on the assumption that whitespace is fungible.
Re:Avoid Else, Return Early (2013) by true_religion on Tuesday March 27, 2018 @12:18AM (#16685195)
I don't know about that. I've never screwed up identaton so much that I couldn't correct it simply by selecting everything I just pasted and hitting tab a couple times in my IDE (PyCharm).

Similarly, copying code from StackOverflow, Github and random blogs have all worked without any issues.

So it can't be as easy as you imply.

Re:Avoid Else, Return Early (2013) by lisper on Tuesday March 27, 2018 @01:16AM (#16685461)
What can I say? It happened to me a lot before I started using the pass trick.
Re:Avoid Else, Return Early (2013) by pleasecalllater on Monday March 26, 2018 @02:22PM (#16681166)
Nope. Just try insert code formatter with 8 spaces into code formatted with 4, or 2. IDE will make something that compiles, it doesn't mean that it works properly.
Re:Avoid Else, Return Early (2013) by true_religion on Tuesday March 27, 2018 @12:24AM (#16685227)
Just tried that---typing like mad in a wordpad, with 12 spaces of indentation, then copying into PyCharm and it worked.

Mixed spaces and tabs as a next go, and it worked too---converting the pasted tabs into spaces of my style.

Smart-tabbing is pretty smart.

Granted, I knew that so long as the indents were proper (i.e it compiles), that pasting at that point would give valid code but... I can't see how using braces would have been different. Just because it compiles, doesn't mean it works. I've pasted javascript incorrectly multiple time to produce valid, but incorrect for my needs code.

Re:Avoid Else, Return Early (2013) by jonny_eh on Monday March 26, 2018 @01:09PM (#16680495)
How does an IDE know if a given line belongs in an if block or not?
Re:Avoid Else, Return Early (2013) by true_religion on Tuesday March 27, 2018 @12:21AM (#16685211)
It'll indent up to the most relevant valid block as possible, given your cursor at the moment. If you click within an if block, it'll indent into that if-block.

The indentation strategy also looks to produce syntactically valid code, so long as what you are pasting lacked indentation errors.

Re:Avoid Else, Return Early (2013) by your-nanny on Monday March 26, 2018 @10:21PM (#16684628)
That is clever. Ugly, yes. But clever.
Re:Avoid Else, Return Early (2013) by kabdib on Monday March 26, 2018 @09:52AM (#16678846)
That's fine -- the Python folks have their own share of religious wars (starting with: tabs, or spaces?) :-)
Re:Avoid Else, Return Early (2013) by tom_mellior on Monday March 26, 2018 @10:25AM (#16679116)
Do they? All the Python folks I've ever seen express an opinion on style have said "follow PEP 8".

Unsurprisingly, PEP 8 does have a rule for tabs vs. spaces: https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces (spaces, of course)

Re:Avoid Else, Return Early (2013) by sireat on Monday March 26, 2018 @02:17PM (#16681125)
I love Python but as a long time C programmer I can't understand the preference for spaces in PEP 8.

Tabs are semantic and only take one key press for movement back and forth and to delete.

If you see 1 tab you know it meant one indentation level. With spaces you have to think.

Plus with spaces you are stuck with 2/4/8 spacing(unless you reformat), with tabs you can configure your editor to your preferences.

Re:Avoid Else, Return Early (2013) by slaman on Monday March 26, 2018 @04:01PM (#16681996)
> with tabs you can configure your editor to your preferences.

Configure 1 tab to be 4 spaces.

Sometimes it's easier to go with the flow. I like tabs for the reasons you mentioned, but fixed-width spaces are a bit better for some reasons too. IDE's can do the heavy lifting of re-formatting indentation levels and converting tabs to spaces for me, and it means if I cat a file on a remote server regardless of the bash tab width settings or if I'm in your code or the stdlib it will all make sense.

Re:Avoid Else, Return Early (2013) by tom_mellior on Tuesday March 27, 2018 @04:44AM (#16686502)
> with tabs you can configure your editor to your preferences

Except that you can't because people using tabs will invariably start mixing tabs and spaces because they can't separate indentation from layout. So the code will be messed up unless you configure your editor to someone else's preferences. Also, I'm sure there is some obscure git setting to de-uglify tab users' diffs, but I'd rather not find out.

Re:Avoid Else, Return Early (2013) by e12e on Monday March 26, 2018 @05:14PM (#16682661)
Tabs are semantic. But, when you start down that route, why not dictate record-separotor as replacement for closing brace/block etc?
Re:Avoid Else, Return Early (2013) by pleasecalllater on Monday March 26, 2018 @02:25PM (#16681199)
The PEP8 myth is quite funny. Almost all people I've met who were repeating "follow the pep8" like a mantra, have never read it. The main idea behind pep8 is: be consistent... however even the python library is not consistent. And it looks like no one cares. There was a great moment to make it nice and consistent - creating the python3, where many incompatible changes were introduced. Instead, the mess is like it was, and people repeat the mantra "follow pep8".

Before you say that I'm crazy... please, go and read pep8.

Re:Avoid Else, Return Early (2013) by tom_mellior on Tuesday March 27, 2018 @04:45AM (#16686503)
> please, go and read pep8

OK. What am I looking for?

Re:Avoid Else, Return Early (2013) by jwilk on Monday March 26, 2018 @11:07AM (#16679444)
As a data point, I don't follow (all of) PEP-8.

But tabs vs. spaces is indeed a bad analogy, because of course it's 4 spaces. ;-P

pycodestyle, the Python style checker, has a few checks disabled by default (because there's no consensus they're good ideas), and even some mutually exclusive ones:

https://pycodestyle.readthedocs.io/en/latest/intro.html#erro...

Re:Avoid Else, Return Early (2013) by gambiting on Monday March 26, 2018 @10:06AM (#16678963)
As a C++ programmer I can assure you, I know plenty of individuals with very strong opinions on tabs vs spaces debate.
Re:Avoid Else, Return Early (2013) by drawkbox on Monday March 26, 2018 @10:48AM (#16679276)
And spaces sub factions/opinions on 2 vs 4 spaces.

The correct answer is whatever the project is already using, unless it is new and then you luckily get to choose.

Re:Avoid Else, Return Early (2013) by bunderbunder on Monday March 26, 2018 @10:39AM (#16679205)
These religious wars can largely be swept away with one simple, practical question: What is going to result in less noise in pull requests and the commit history, given that nowadays everyone uses a different editor with different configuration defaults, and is going to result in less time in pull requests squabbling over these kinds of formatting issues, given that nowadays everyone uses a different editor with different configuration defaults?

Tabs vs. Spaces has a fairly good answer that flows out of this way of looking at things. There is a counter-argument to that answer, but it is invalidated if you move toward conventions for argument lists and chained method calls that are also designed to limit noise in the source control system.

IMO, the argument about bracing also has a practical, non-religious answer once you start looking at your coding conventions this way.

Re:Avoid Else, Return Early (2013) by TomMarius on Monday March 26, 2018 @10:06AM (#16678959)
Tabs, everyone can configure them however they want. :-)
Re:Avoid Else, Return Early (2013) by RobertRoberts on Monday March 26, 2018 @10:53AM (#16679321)
I hate being able to configure stuff, I want other people to boss me around from anywhere in the world! Their opinion matters on my computer!
[dead]
Re:Avoid Else, Return Early (2013) by YSFEJ4SWJUVU6 on Monday March 26, 2018 @12:08PM (#16679903)
Why would you ever half-indent a block? For alignment you should of course use spaces (for whole alignment, not just the end, to be very clear).
Re:Avoid Else, Return Early (2013) by kevin_thibedeau on Monday March 26, 2018 @02:14PM (#16681095)
The war is over. Py3 doesn't allow mixed spaces and tabs in the same file. You choose your side and never get to skirmish with the other one.
Re:Avoid Else, Return Early (2013) by jjgreen on Monday March 26, 2018 @11:21AM (#16679544)
Both!
Re:Avoid Else, Return Early (2013) by sethgecko on Monday March 26, 2018 @11:20AM (#16679539)

    >>> from __future__ import braces
    SyntaxError: not a chance
Re:Avoid Else, Return Early (2013) by zerostar07 on Monday March 26, 2018 @10:47AM (#16679265)
but biased fans of both would argue that opening and closing a block in the same indent level (allman-style) is closer to python.
Re:Avoid Else, Return Early (2013) by laythea on Monday March 26, 2018 @02:33PM (#16681261)
Tell them braces are visual indicators of blocks of logic, enabling reasoning more easily on a multi-statement level.
Re:Avoid Else, Return Early (2013) by threatofrain on Monday March 26, 2018 @02:40PM (#16681318)
Lambdas shouldn't be more than one line anyway.