r/Python • • Jan 01 '11

PEP 8 & Underscores

I opened the threading module documentation and saw a note at the top describing a change in the naming convention for functions from the Java inspired API in camelCase mixedCase to PEP 8 style recommended underscores. No deprecation of the old camelCase mixedCase names yet.

I’m a big fan of stylistic conventions and I basically follow PEP8, but I just do not like the use of underscores. I realize these types of preferences are as subjective as say liking the color red more than blue, but I’m curious to know how many other people just write their code following the naming conventions of languages like Java, C#, and Javascript libraries?

I came to Python from Perl and one of the things that attracted me to Python was readability. It read like pseudocode and I think a big part of that was because there wasn’t $, @, and % all over the place in addition to the removal of { and ;. I liked that I could write an if statement without wrapping it in parenthesis.

even better was I didn’t have to reach to the top row typing some of these symbols over and over again. Sure it seems like a small thing to just reach, but with some symptoms of RSI and doing it for many hours a day I think it really does incur a significant cost in comfort. I don’t like typing _ anymore than I like reaching up to type $.

I actually was sort of attracted to some of the standard library modules all lowercase convention. I know readability can be an issue here, but os.path.dirname is better than os.path.dirName or os.path.dir_name. You do really see the mix though of the different styles within the standard library. I know the primary argument for the underscore is readability, but I actually think the gap between dir and name is just too significant and when you are looking at a full page of names like this the underscores just begin to jump out as a pattern of white space. Maybe this comes from using too much camelCase mixedCase.

EDIT: Some people who may come across this post may be interested in a solution to the more serious issue of typing underscores - a pain and slow to type when you are doing it over and over and over again. I found an open source program today called AutoHotKey. Install that and then create a simple text file with a ".ahk" extension. Type "+space::Send _" into the file without the quotes and save. Double click to launch. Put in your startup folder. You can now type underscores pressing the shift + space key.

I am currently having some issues with the shift key sticking at times and thus typing undescores when I mean space. It doesn't seem to be keyboard dependent. So this may be a flaw in the software :( . Any other recommendations helpful.

1 Upvotes

56 comments sorted by

View all comments

3

u/yetanothernerd Jan 02 '11

camelCaseIsReallyUgly.

underscores_are_better

0

u/_pep_8_ Jan 02 '11

Well you need to qualify that. According to GVR in PEP8 underscores are "ugly!" whenever there are caps in it. In any case, PEP8 requires that you write "ugly" CamelClase for class names. But ultimately these are subjective opinions. Most of the "readability" arguments here are subjective supported by unrealistically long examples to make their case.

I've pointed out, in other comments, why I think mixedCase is more readable with code example that people actually use in the real world. The logical reason why underscores are more readable is they create a bigger white space gap between multiple words within an identifier. Yes, that is more readable but we don't really read code like we read the English sentences people are using around here and that extra whitespace gap becomes a nuisance.

A sentence in code is composed of various parts of different kinds of significance. I don't write "ifidentifier:" for an if statement. I write "if identifier:". I don't write "whileidentifier=1:", I write "while identifier = 1:". My brain parses it like this (if, identifier, :) or (while, identifier, =, 1, :). If I used underscores my brain would break it apart like this (while, a, long, identifier, with underscores, =, 1, :) because that's how our brains, from reading English all the time, are trained to chunk things and concidentally why people think its more "readable". Yet that readability is not a huge advantage in this context.

When you are looking at thousands of lines of this stuff a day you are creating a huge penalty by having to see your identifiers all over the place creating extra whitespace where you really don't the whitespace to be significant. Your brain is going to be able to grok the identifier from its first few letters anyway after some minimal exposure, but its very hard for your brain not to attach significance to notice the increased gaps from underscores because you are so trained to do it from normal reading.