• 13
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Python alignment of assignments (style) [closed]

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.

  • 18
Reply Report
      • 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.
      • 2
    • Best answer. As long as I code alone, I'll slightly change the standards to (subjectively) enhance readability :)

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.

  • 2
Reply Report
      • 1
    • 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.
      • 2
    • 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.

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.

  • 2
Reply Report
      • 1
    • @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.
      • 2
    • 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.
      • 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.
      • 1
    • Alignment also makes it easier to notice many kinds of mistakes such as things that are the same and are supposed to be different.
      • 1
    • 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.

Trending Tags