3 ms·
Why not follow pep 8's indentation rules: def this_is_a_function(*args, **kws): def this_is_a_helper_function( arg1,
by sago 11y ago
Why not follow pep 8's indentation rules:
def this_is_a_function(*args, **kws):
def this_is_a_helper_function(
arg1,
arg2='this line is indented 12 chars!'):
this_long_variable = (
'Don\'t put an expression here unless you '
'want to use useless intermediate variables')
return this_long_variable if var else 'really short here'
return 1
(for the same number of lines) or better still follow some basic common sense:
1. Local functions should be short and have short names since their scope is limited.
2. Local variables, similarly
3. Don't put long explicit strings in code.
and get
messages = dict(
long_default = \
'Don\'t write strings that may wrap in ugly ways on 80 col shells.',
short_default = 'Keep it short'
)
def this_is_a_function(parameter):
def message(long, message='this is only 36 chars'):
long_message = messages.long_default
short_message = message or messages.short_default
return long_message if long else short_message
# Do something here.
return 1
And if you have an expression that is more than 80 characters long, breaking bits of that expression and assigning them to temporary variables is not 'useless', it makes code clearer and is only rarely a performance hit.
- coldtea 11y agoIf I see a "\" I close the editor and delete the source file.
- deleted 11y ago[deleted]
- sago 11y agoHow impressive of you.
- Retra 11y agoThose are great suggestions, but may point is more that "too much on one line" isn't necessarily better than "too little over too many lines." It can be hard to get a good overview of what is happening when you've got to resort to splitting one significant token per line. In other words, I try to use vertical space for higher-level work than horizontal space, and when you have to fit both into vertical space, you lose some clarity in high-level in order to gain it in low-level.
- sago 11y agoSorry, I know that putting code up is an invitation for people to pick at the wrong feature, so I realised I was being churlish. But I think these things do have to be addressed with examples. There are few people willing to put an actual example up that a) is frequent enough to mean that 80cols can't be easily used (note, not 'preferred', we're talking about a standard, plenty of people 'prefer' 80 cols) and b) is pythonic enough that it isn't better written in more explicit steps.
- Retra 11y agoEighty columns is a good standard, I just think people forget how small it can be, and why it isn't always a problem. I've written tons of code that looks fantastic, if only I could use 85 columns, but rewriting it to use less just makes use of some pretty clunky constructs. All these extra "()"s and "/"s aren't actually helping make the line more readable, it just makes it fit into a rather arbitrary column limit. And at that point, you'd be thinking that the best way to make your code the most readable it can be is to use more columns. So I don't think 80 cols is a hard limit, anymore than I think writing code in a terminal is a hard limit. I use a non-terminal text editor. I do it because it makes it easier to read code and format it how I like. I see little reason why anyone should be jumping through hoops to accommodate that. By using a terminal editor, you've already decided you won't get the best formatting, because you sacrifice the flexibility that allows it in exchange for something else. You should be expecting things to wrap around awkwardly on occasion. Eighty columns is a good target width, but it's not worth proliferating clunky constructs to conform to. That's no better than having code wrap. (Of course, someone could also write terminal editors that allow text to go longer than 80 lines. Why are you using an editor like that if it can't do something this simple?)
- sago 11y agoThanks again. I agree, I've written a lot of code that would be clearer in 85 cols than 80, and some code that would have been clearer in 160 cols too. I think we're agreeing mostly. The problem with 'ish' is that I see it as worse than useless. 80cols-ish is what? 90 cols max? Why not just use 90 cols as the standard? If I have to scroll my whole window or resize it to read your line, is that really better than if it had an extra break? Some people may think so, but I don't, personally.