by sago · 11y ago · 0 comments
I know some people program with one editor maximised and can have 200 character lines if they like, but a specific width as a convention is helpful. And the convention that is already widely used is going to make fewer people have to accommodate you. The 'ish' is useless in that regard.
Ayup. That reason is teletypewriters (aka ttys).
The biggest problem I have with most programmers is naming things well. 80 columns is anathema to good naming. People start using mbl instead of maxExchangeBufferLengthBytes.
No, I'm not a Java guy. I spend a LOT of time chewing through embedded C code. I don't even have good autocomplete in most embedded IDE's, and I STILL prefer maxExchangeBufferLengthBytes. Here's why--that name tells me many things at a glance:
max--limit of some form, probably should be positive
ExchangeBuffer--a buffer and it's functional purpose
LengthBytes--a length (so likely unsigned and if I see differently I should pause) in bytes (not items). You would be amazed at the number of bugs this circumvents.
That's a huge amount of information conveyed in 30 bytes. But that's almost half of 80 columns. Add in something like 8 column indents and you can't even name things well.
And, that still probably doesn't give me enough information. I need to look at that to see if that is constant, and I probably need to see if I can touch that without concurrency considerations.
I read code far more often than I type it. I read code after I have forgotten about it far more often than when it is fresh.
Naming things to enable me to come back to my code 6 years later, refresh my memory, and not screw up while doing it is the single most important task in my programming.
80 columns is simply not conducive to that.
That's why pilots are recommended to scan horizontally no more than about 10 degrees at a time when looking out for other aircraft: http://www.skybrary.aero/index.php/Visual_Scanning_Technique
Having lines be too short is also a concern for legibility, because even when lines are short, there's always the risk of the reader missing the next line at every line break.
The 80 character line is actually an artifact of punch cards, which supported 80 characters per card. And that's probably a remnant of some automatic loom that for some design reason, worked best with instructions fed in chunks of 80s.
The exact value of 80 characters might be because of IBM punched cards, but that value was chosen because printed media already had that limit, or a smaller limit, e.g. broadsheet newspaper columns. People have been writing that way for millennia.
Two other points:
(1) 70 or 90 might have been acceptable alternatives, but 80 was chosen. It's approximate. Fewer limits the amount of information per line, and more limits the font size you need to scan comfortably. A wider screen allow you to open up several windows. Having one big window isn't such a good idea.
(2) I have never been bothered by this limit, except when others break it and I have to read their code. Occasionally, sticking to it requires some thought regarding program layout. If you find yourself exceeding 80 columns often, you're probably doing something wrong. You might be nesting your code too deeply when you could call a function which handles some of your conditional logic, or your function names are too long, which suggests the underlying functions are doing too much and should be split up.
Except that you are wrong.
One of the links in the comments here has research that shows that reading speed increases significantly to at least 90 characters and may increase further than that.
http://psychology.wichita.edu/surl/usabilitynews/72/LineLeng...
I had to move my head with every line I read. (The PDF is somewhat easier on the eyes.) Not a single reference to aviation, the context where most of the related research has been done, and where research matters most, as people's lives depend upon it.
The article was specifically about reading news on-line, and was a very small study. Reading code is somewhat different to reading prose in natural language such as news. To understand a portion of code at all, you need to take in every token. With very long lines, this means having to move your eyes back and forward. Laying it out vertically makes scanning a lot easier.
It's a bit handwavey otherwise. I'm also a long-names fan, sometimes I go overboard, but in 20 years I've not been able to logically break my line in an obvious place only once or twice.
class VendorOrderShipmentResource():
...
def post(...):
...
for item in data.order_items:
...
if not shipment_qty:
abort(400, message='All order_items must have a quantity_to_ship.')
That line goes out to col 86. I realize that it's possible to break it like this (#1): abort(
400, message='All order_items must have a quantity_to_ship.')
or this (#2): abort(
400,
message='All order_items must have a quantity_to_ship.'
)
or this (#3): msg = 'All order_items must have a quantity_to_ship.'
abort(400, message=msg)
or this (#4): abort(400, message='All order_items must have a '
'quantity_to_ship')
I'm not really a fan of any of those:1. I'm not a fan of this one, in this case at least, because it "looks weird" due to the mismatch between the length of the function name, and the length of the next line. Also, I'm not a fan of "hiding" the closing parenthesis at the end of the line, rather than putting it on new line at the original indent level (like #2).
2. I'm not really a fan of this because the first argument and the function name are so much shorter than the message argument, that it looks really lopsided.
3. I'm not a fan of this because it seems stupid to create a new variable just to use it on the next line (and never again).
4. I'm not really a fan of breaking strings up this way, but sometimes I'm forced to. Breaking strings up this way tends to be really fragile when you want to modify them, and might need to reflow the entire wrap.
Personally, I'm just a fan of increasing line length to ~100. The number of things that go over 100 columns (in practice) is much smaller than the number of things that go over 80 columns.
Why is 3 stupid? That would be my preference. It is clear, simple and gets compiled away (in many cases). I alias in variables frequently, to make things more explicit. For example, I might write
iterations = 300
for _ in range(iterations):
...
which makes it less likely I'll end up repeating things or copy and pasting when new cases come up, e.g. if not shipment_qty:
abort(400, message)
elif shipment_qty < 0:
abort(400, message)
What happens in your case if you come back and edit the message and add a few characters, exceeding your longer line length?When I'm forced to break a string it is usually an indication that I need a better way of defining text than inline in expressions in code.
abort(400,
message='All order_items must have a quantity_to_ship.')
(not giving the parens an entire line)I'm not a fan of 80 characters, but I am a fan of less than 120 characters, if simply that there is a point where a line won't fit in a terminal thats in half of a "standard laptop" screen (i.e. the limit to where I could put two files next to each other)
this_is_my_very_long_variable_name = (
this_is_one_variable_that_goes_into_calculating_it +
this_is_another_variable_that_needs_considering)
The issue wasn't the fact that some expressions are longer than 80 cols (that one is longer than 120, even, its 136 characters minimum), but that they can't be clearly and appropriately written in 80 cols. class Batman():
# ...
def handle_jokes(self, villains):
for villain in villains:
if villain.supports_jokes:
formats, transports = villain.query_joke_formats_and_transports()
# ...
That long line is 82 characters. Assume there's a good reason formats and transports are queried together. In practice this leads to variable names f and t. :( query = villain.query_joke_formats_and_transports()
formats, transports = query
or formats, transports = (
villain.query_joke_formats_and_transports())
or formats, transports = \
villain.query_joke_formats_and_transports()
Of which, we tend to do the first.Again, the longer version is clearer and simpler, but the shorter version is not so confounding that it is a slamdunk argument for longer lines, particularly as, for any length line, you could get this problem, so you'll have to do something like it eventually. If you're seeing this all over the place, it may be worth wrapping the API in more pythonic terms. 'Getters' taking no arguments, for example, are often a code-smell in python, though queries (as in your case) are a good exception, since it is also not great to have properties do expensive work (like querying a DB, say).
Sure:
thisIsAnOutgoingBuffer = this_is_a_function(descriptiveKeywordArg = maxExchangeBufferLengthBytes, ...)
It's been a while, but I seem to recall that Python would require a continuation character to break anywhere prior to the comma.
outgoing_buffer = a_function(
descriptive_keyword=max_exchange_buffer_length_bytes
...)
Breaks nice and semantically. Calling functions with arguments on new lines is very pythonic, so much so that it is a lot of the examples of indentation in PEP8. For functions with more arguments, it is readable for the same reason that you might want to define variables on successive lines.Interestingly, notice that in your quick back-of-the-envelope example your variable and function names are quite redundant. I know it was throwaway and it wasn't your point: I'm not suggesting you were holding it up as good naming, but it is interesting that the toy examples being quoted have to work quite hard to make it bad. That suggests to me that a lot of the problem is manufactured, in practice.
thisIsAnOutgoingBuffer = this_is_a_function(
descriptiveKeywordArg=maxExchangeBufferLengthBytes,
more_long_param=a_nested_function_call_with_long_params(
inner_param=55,
another_inner_param="Asdf"
)
)Yeah, that was nice for 80 line terminals in 1986.
This is 2015. We have 27" screens. Two of them for most of us.
And on our lowly laptops, we have hi-dpi fonts (which makes smaller fonts sizes read great) and widescreens.
The number of programmers I see manically toggling through their code in one window is crazy. I think this is part of the legacy of some high-profile IDEs that gave you one window at a time.
What proportion of your code lines are > 80 cols? Multiply that by the amount of extra width to get the wasted screen estate. I'll take that and put more code/docs/tests/output on my screen at the same time and have to toggle less.
The idea that this is about teletype is very amusing, but rather naive.
Even on a widescreen laptop you can have 2 > 100 lines windows side by side. And for multi-monitor setups you can go even wilder.
>The number of programmers I see manically toggling through their code in one window is crazy. I think this is part of the legacy of some high-profile IDEs that gave you one window at a time.
If you have tons of junk in front of you, you're not focusing on your code as you should be.
Is that junk?
How many of your lines require that 100 columns, in your average code?
Of course, this can just descend into a pissing contest. The point isn't who-has-the-most-windows, but that the logic of 80cols has a practical point, and programmers who continue with the default have reasons that aren't just about teletype or not moving with the times.
Plus, with most editors that can jump in / out of functions, it's not like I'm losing my place. In fact, my editors all save my place in files on exit.
And you know you can put different parts of the same file in columns too? I often need to write code that is coupled to another part of the same file.
if __name__ == '__main__':
def this_is_a_function(*args, **kwargs):
def this_is_a_helper_function(arg1,
arg2=('this line is already indented 39 '
'chars!')):
this_long_variable = ("Don't put an expression here unless you "
"want to use useless intermediate variabls.")
return this_long variable if var else 'really short here'
return 1
And some people want to insist on 8 char indents! 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.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.
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.
if __name__ == '__main__':
def this_is_a_function(*args, **kwargs):
def this_is_a_helper_function(
arg1,
arg2='this line is now indented 12 chars!'
):
this_long_variable = (
"Don't put an expression here unless you "
"want to use useless intermediate variabls."
)
return this_long variable if var else 'really short here'
return 1 Flat is better than nested.
Too many levels of nesting is definitely a code smell. Often it's better to pull those helper functions out, and/or create function or method factory functions at a higher level of organization.Anywho, do you have a genuine example of where a closure is helpful for you in python? I'm very curious.
(Go, for some reason, doesn't like splitting lines at all, no matter how wide. I've had code reviewers criticise me splitting lines at 200, which I don't understand at all.)
Recently code-editors have started to learn how. Don't wrap your lines at all; let the editor do that, in realtime, and you can have your windows at whatever width you want.
As far as I can tell, that reason is the IBM 80 column punch card standard launched in 1928:
https://en.wikipedia.org/wiki/Punched_card#IBM_80-column_pun...
You can't standardise based on what you are fond of. That's the point of a standard. When such a large proportion of people, and the standard library, and most shells, use 80, then using 120 will inconvenience them. If you load my 80col code into your 120 col editor, it will not inconvenience you at all, barring an occasional extra line break.
No.
>You can't standardise based on what you are fond of.
Sure you can. That's why company and team-wide standards are for.
Nobody has to be forced to use some BS 70's throwback rules just because they are in PEP8.
When lines are longer it becomes more difficult for your eyes to keep track of where the next line is. Furthermore, code is not formatted as paragraphs but as separate lines, so you end up wasting a lot of screen estate for those few long lines. Having a low maximum means it will be close to the average and a larger proportion of the screen is utilized.
Actually this claim is disputed: http://psychology.wichita.edu/surl/usabilitynews/72/LineLeng...
I like to joke God made The VT52 with 80 columns because that's how terminals should be.
Another I use is that "80 columns should be enough for anyone".
Now, seriously, code that's too wide (or too long) is a code smell. You probably should name your variables better, have fewer them in scope or just refactor the code to be more readable. Remember: code is executed by very cheaply and read by very expensive humans. Optimize for the latter.
You're assuming you have just one window open, aren't you? There's less wasted space with 80 cols if you have multiple columns (of the same file, or different files). The math is elsewhere in this thread.
Settings > Editor > General:
* check "Use soft wraps in editor"
* check "Use original line's indent for wrapped parts"
* enter 4 for "Additional shift"
* if you open huge files with wide lines, check "Show soft wraps for current line only" to prevent slowness
I like standards, but 80 chars just isnt enough and never was. Is 96 the next reasonable number?
I’ve seen 66 characters recommended as a maximum line width as a style guide for typewritten documents. You also want some extra room for extra margins, line numbering, etc. If 66 is taken as a good minimal number, then 64 (2⁶) is too small, so let’s add, say, 16 to 64, giving 80 columns.
Paper documents have the form that they do for a reason. You never saw someone writing sideways on a paper (landscape alignment). This is because readability suffers when lines are too long. Even if boxes resized automatically, it’s the line length that’s the problem, not the resizing or scrolling.
Most lines in code that is well structured and simple are less than 80 cols wide. Lines that require more than that can be broken. I think in 20 years of Python I've only seen a couple of cases where there wasn't an obvious semantic break point, most of the time such breaks are easy to place at logical points.
When you have 80 cols you have more code in your window. You can use the resolution to have taller windows and more windows side-by-side. 1920 pixels gives you nicely 3x80 col editors. Which allows you to look at the code, and its tests, and the shell at the same time, for example.
Depending on your font. I use 1920 (-Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO10646-1), and I get almost 4x80 col's, 3x96 works nice.
As the commenter above stated, he found considerably less than 1% of his lines required > 80 cols, so rather than go 96 cols and have 17% of my screen real estate wasted, I'll have the extra column of code or documentation or tests, or output, and have to toggle less.