45

I really like following style standards, as those specified in PEP 8. I have a linter that checks it automatically, and definitely my code is much better because of that.

There is just one point in PEP 8, the E251 & E221 don't feel very good. Coming from a JavaScript background, I used to align the variable assignments as following:

var var1        = 1234;
    var2        = 54;
    longer_name = 'hi';

var lol = {
    'that'        : 65,
    'those'       : 87,
    'other_thing' : true
};

And in my humble opinion, this improves readability dramatically. Problem is, this is dis-recommended by PEP 8. With dictionaries, is not that bad because spaces are allowed after the colon:

dictionary = {
   'something':        98,
   'some_other_thing': False
}

I can "live" with variable assignments without alignment, but what I don't like at all is not to be able to pass named arguments in a function call, like this:

some_func(length=      40,
          weight=      900,
          lol=         'troll',
          useless_var= True,
          intelligence=None)

So, what I end up doing is using a dictionary, as following:

specs = {
    'length':       40,
    'weight':       900,
    'lol':          'troll',
    'useless_var':  True,
    'intelligence': None
}

some_func(**specs)

or just simply

some_func(**{'length':       40,
             'weight':       900,
             'lol':          'troll',
             'useless_var':  True,
             'intelligence': None})

But I have the feeling this work around is just worse than ignoring the PEP 8 E251 / E221.

What is the best practice?

EDIT many years later

Don't align. Sooner or later a new variable that is longer will come and your will have to hit spacebar here and there for a while until everything looks good again. Not worth it.

EDIT even more years later Just use a code formatter like black and use it as pre-commit and/or your CI. Then forget about this.

bgusach
  • 14,527
  • 14
  • 51
  • 68
  • 13
    The best practice is to follow PEP 8. Get used to not aligning assignment operators and dictionary values. Your humble opinion may easily change if subjected to working with it each day. –  Nov 21 '12 at 16:37
  • Are you even able to pass arguments by name in Javascript? So why would you do this horrible thing? – jadkik94 Nov 21 '12 at 16:42
  • In JS is a good practice to pass a specifications object instead of 15 ordered arguments. Douglas Crockford dixit, and I totally agree. – bgusach Nov 21 '12 at 16:49
  • 7
    The choice of alignment style is **arbitrary**. (As are any style rules, regardless of the fervency with which people will argue about them.) Nobody's going to hold a gun to your head if you don't follow one point of PEP8. You're free to set up a house style in your organisation. If you were curating a very big open-source project things would be different, but for private code whatever works for you is fine. (Personally, I don't align because I prefer large areas of whitespace to carry meaning as separators, and because I use garish highlighting for readability.) – millimoose Nov 21 '12 at 16:55
  • 1
    @delnan, my "humble opinion" totally changed. Three years later I find funny how I tried to write such horrible constructs hehe. – bgusach Jul 16 '15 at 09:52
  • Only 9 years late to the party! Today, I would suggest choosing a standard and sticking to it by putting auto-linting steps into your CI/CD pipeline. For python, probably best to stick to Black https://black.readthedocs.io/en/stable/ as this removes any decision effort and can automatically commit changes https://github.com/GeekZoneHQ/web/blob/master/.github/workflows/black.yml back to the repo. Then again, I fully appreciate that CI/CD was in its infancy in 2012! – James Geddes Nov 18 '21 at 08:25
  • 1
    @JamesGeddes yeah, this question doesn't make that much sense nowadays. Just use a code formatter in your pre-commit hooks and CI and stop worrying. – bgusach Nov 18 '21 at 08:59
  • Agreed, I just thought I'd share my twopence worth just in case anyone else found it helpful. I would generally prefer putting linting in the pipeline as then you are not reliant on everyone having local linting. Interesting how much things have changed! – James Geddes Nov 18 '21 at 09:54

3 Answers3

21

Best practice is subjective, but the most common practice is to stick to PEP8.

I definitely don't suggest creating dictionaries every time you want to call a function with named arguments. That's quite wasteful. I don't see why your original some_func call wouldn't work. I definitely break my function calls into lines if they get too long and unwieldy. But I do not align them. I imagine that the reason for the recommendation is because it can get to be a huge pain to maintain all of the spacing correctly over time, and the consensus was maintainability over the gain in prettyness.

If you're working on your own code, align away, who cares? PEP8 is a guideline, not a law.

acjay
  • 34,571
  • 6
  • 57
  • 100
  • 2
    You are right in some points, but well... fortunately my IDE aligns automatically based on a special character ":", "=" etc. It doesn't take any time at all to keep it "pretty". For me maintenance is also about reading fast and comfortable. – bgusach Nov 21 '12 at 16:54
  • So then either change your IDE's settings, or keep aligning. Seems like a win-win to me :) – acjay Nov 21 '12 at 16:55
  • Best answer. As long as I code alone, I'll slightly change the standards to (subjectively) enhance readability :) – bgusach Nov 21 '12 at 17:28
2

So, what I end up doing is using a dictionary, as following:

specs = {
    length:      40,
    weight:      900,
    lol:         'troll',
    useless_var: True,
    intelligence:None
}

IMHO - this is less readable (were it valid syntax without quoting the keys), and if I happen to want to add some_longer_named_varible, I'm really not sure if I'd want to muck about re-spacing everything else.

I think you should just bite the bullet I'm afraid.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • I just fixed the typo of the key values. It's true that if some var name is very long, alignment can make it kind of confusing. In those rare cases, I just declared that long name in the last position and without alignment. But to me, readability is enhanced in most of cases, eyes know where to look at. – bgusach Nov 21 '12 at 16:47
  • 1
    I love lining up assignments that are reasonably close in length, but I won't bother with huge ugly spaces to accommodate one long guy. Mostly lined up is better than a jumbled mess. And if a few extra seconds here and there amid the hours is the price of self satisfaction and beautiful code, that's a price I am more than willing to pay. – Charlie Gorichanaz Apr 22 '16 at 20:55
2

I'd recommend sticking to PEP8. What happens if you need to change the name of one of your variables? An automated refactoring tool will change this:

var1        = 1234
var2        = 54
longer_name = 'hi'

to this:

var1        = 1234
var2        = 54
even_longer_name = 'hi'  # not aligned any more!

You'll end up making more work for yourself to keep things aligned.

Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
  • 15
    The editor I use to write code aligns automatically code with a simple keystroke. So, it is not big deal. The only problem comes with too long variables, but in those cases, I just don't align that assignment and if possible, put it in the last position. – bgusach Nov 21 '12 at 16:56
  • @ikaros45: the bottom line is that it's a personal choice. If you're the only person who's going to read your code, do whatever you like best and don't feel too bound by style guides like PEP8. Conversely, if you're writing code as part of a team it's important to stick to whatever style guide the team has agreed on, so you don't piss off everyone who has to read your code. – Benjamin Hodgson Nov 21 '12 at 17:04
  • If you want my opinion, I prefer the non-aligned style, as PEP8 recommends. I find the eye tends to get lost going from one side of that chasm of whitespace to the other. Like when you read the contents page of a book where the chapter titles are separated from the page numbers. – Benjamin Hodgson Nov 21 '12 at 17:06
  • 2
    It's personal taste then... For me if aligned, it looks like a table. You know where things are just by taking a quick look. – bgusach Nov 21 '12 at 17:13
  • 8
    Alignment also makes it easier to notice many kinds of mistakes such as things that are the same and are supposed to be different. – Steve Jorgensen Jan 29 '19 at 23:25