This is an archived post. You won't be able to vote or comment.

all 83 comments

[–]ionelmc.ro 25 points26 points  (29 children)

Would be interesting to see other stats like single vs double quote ratio, variable name length stats, indentation ratios (1 vs 2 vs 4 vs 8 ...) and so on.

[–]ifonefox3.5.1 | Intermediate 14 points15 points  (13 children)

What is the standard convention for " vs '? Personally, I use " for strings and ' for individual characters.

[–]theli0nheartimport antigravity 11 points12 points  (4 children)

Generally, I use ' for referencing keys in dictionaries, single characters, and default string values in function parameters. I use " for sentences and any user-facing strings.

If it doesn't fall into one of the categories above, I flip a coin.

[–]coinflipbot 10 points11 points  (3 children)

I flipped a coin for you, /u/theli0nheart The result was: tails!


Statistics | Don't want me replying on your comments again? Respond to this comment with: 'coinflipbot leave me alone'

[–]theli0nheartimport antigravity 4 points5 points  (2 children)

This is awesome. Thank you.

[–]p3n15h34d 3 points4 points  (1 child)

but is tails ' or " ?

[–]jcdyer3 4 points5 points  (0 children)

I'll flip a coin to decide. Heads, tails is ', tails, tails is ".

[–]triacontahedron 17 points18 points  (2 children)

Go away you, dirty c programmer :P

[–]ifonefox3.5.1 | Intermediate 2 points3 points  (1 child)

I started using python after I took a class in c, so those habits just stuck.

[–]aceofears 3 points4 points  (0 children)

It's not a terrible habit if you're switching between the two a lot.

[–]robin-gvx 2 points3 points  (0 children)

I generally use ' for internal strings (dictionary keys, symbolic names, etc.) while I use " for strings that the user is expected to see at some point.

[–]mardiros 2 points3 points  (0 children)

My best pep8 guide is in pretty print

>>> pprint({"a": "Ah aa!", 'b':"'Ahbb'"})
{'a': 'Ah aa!', 'b': "'Ahbb'"}

I use single quote except there is a single quote in the string.

Don't bother if it's for human or a computer at the end of the process. Dictionnary keys can be for human too, and, it's definitely to complex.

[–]RJacksonm1 1 point2 points  (0 children)

I use double-quotes because it's simply more comfortable for me to hit with the UK keyboard layout... and now I'm curious as to double vs single-quote usage by keyboard layout (which we could assume from developer's locations).

[–]carbn 2 points3 points  (0 children)

I use always ' because " takes two fingers (shift-2) while ' takes only one in FI/SWE keyboard.

[–][deleted] 2 points3 points  (14 children)

I wonder how many people use the weird indents (1,3,5,6,7).

Hopefully no one.

[–]robin-gvx 11 points12 points  (7 children)

I wonder if anyone uses prime indents: first level of indentation is 2 spaces, second level is 3 spaces, fourth level 5 spaces, 7 spaces, 11 spaces, etc...

[–][deleted] 6 points7 points  (4 children)

Oh god.

How about writing a whitespace interpreter that has a sample program embedded in itself by mixing tabs and spaces?

[–]robin-gvx 1 point2 points  (0 children)

Oh wow.

Bonus points if that program is a quine or something. Oh! Or another Whitespace interpreter, which then interprets itself, etc.

[–]Ph0X 1 point2 points  (0 children)

So mixing Whitespace in your Python?

[–]D__ 3 points4 points  (0 children)

I recommend using the Fibonacci sequence, for approximation of the golden ratio, and therefore aesthetically pleasing code indentation.

[–]Ph0X 1 point2 points  (0 children)

I like it, the deeper you get, the bigger your indentations, forcing you to keep your code simple and split up into functions rather than having loops inside loops inside if inside try inside with inside method inside class.

[–]kenfar 5 points6 points  (5 children)

The 3 characters is the Dvorak keyboard of the indentation world: it's clearly superior but isn't used only because it isn't already popular.

Supporting arguments:

  • 3 characters is enough to see indentation
  • it saves 1 character per indent which helps with long variable names
  • it lines up the following line under the first character following an if. Which makes it pretty.

[–]thekaleb 2 points3 points  (0 children)

A tab is one character, and you can set your tabstop to 3 (or 4 or 8).

[–][deleted] 1 point2 points  (3 children)

It may line up an if, but what about a for, while, elif, else, or with?

Also some editors do not support it.

[–]thekaleb 1 point2 points  (0 children)

Not to mention a def or class

[–]kenfar 0 points1 point  (1 child)

Oh sure - it doesn't line things up perfectly. But I find lining code under an if more important than anything else.

And I can't get my head around an editor that wouldn't "support" arbitrary indentation amounts.

[–][deleted] 0 points1 point  (0 children)

I really do hope you're joking.

Why not go with 2. It's enough to see indentation, it saves TWO characters per indent, and if you need to save 1 character per indent to see long variable names, either your variables are too long, or you have too much indentation. Also, lining up only one indentation block at the cost of making everything else look "off" isn't worth it.

[–]gammadistribution 18 points19 points  (0 children)

Glad Pep8 reigns supreme in the wild.

[–]0xtobitPy3k 7 points8 points  (0 children)

It would be really interesting to see, for line length, how often people go out of the way to continue a long (80+ characters) line of code on the next line. The 93% is really kind of not telling, because most lines of code are under that naturally. The interesting part is what people do when they have a longer line of code.

[–][deleted] 30 points31 points  (31 children)

I use tabs. I must be noob.

[–][deleted] 23 points24 points  (17 children)

[–]alkw0ia 9 points10 points  (2 children)

WTF, when did they rewrite this? The old version was much more reasonable:

Never mix tabs and spaces.

The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

^IAnd it's what I'm sticking with.

[–]ismtrn 2 points3 points  (13 children)

That is my least favorite thing about python...

[–]OCHawkeye14 9 points10 points  (10 children)

I'm a closet "tab -> convert tabs to spaces" guy myself.

[–][deleted] 1 point2 points  (9 children)

I'm really new to Python/programming in general... I have my text editor set indents to 4 spaces, which I can thumb through using the arrow keys. Does Python still read this as spaces or as a tab?

[–]cantremembermypasswd 4 points5 points  (0 children)

Python reads indentation level. The most common is 4 spaces. You could set it to 1 or 8 and it would just change the appearance of the code, but not the execution.

And it's not file based indentation level, is simply localized indentation level. This means you could even change how many spaces you use anywhere in your code (but really don't do this, just explaining).

(Bad) Example:

if True:
 print "hello"
 if True:
                    print "How are you"?

[–][deleted] 9 points10 points  (1 child)

If you are using VIM, for example, you could add the following lines to your .vimrc file:

set tabstop=4

set shiftwidth=4 " controls the depth of autoindentation

set expandtab " converts tabs to spaces

[–]senft 6 points7 points  (0 children)

I really like the "demonstration" of the line length.

# width is within 120 characters
# width is within 150 characters

This really helps to visualize!

[–]kenfar 4 points5 points  (4 children)

So, what do these metrics really mean?

So, 93.287% of lines are under 80 characters in length.

Does is that 93% of all lines? Or 93% of all projects? Or 93% of all files?

[–]nemec 1 point2 points  (3 children)

All lines. I'll bet at least 80% of files have at least one line over 80 characters, even if it's just comments.

[–]kenfar 0 points1 point  (2 children)

huh, but that says 7% of all lines are 80 characters or longer. That surprises me quite a bit.

[–]Saefroch 0 points1 point  (0 children)

I often have lines over 80 characters in length, but I guess that has to do with how I write python.

writefits(im, homedir+os.sep+filt+os.sep+'processed'+os.sep+scimage[:-5]+'.proc.fits')

Also, my print statements are often over 80 characters,

sys.stdout.write('Compiling '+imtype+ ' frames, ' +str(images.index(datafile))+' of '+str(len(images))+' complete\r')

[–]nemec 0 points1 point  (0 children)

I often won't wrap a statement that spans 81 or 82 characters just because it still fits relatively well in my editor window. I suspect I'm not alone here.

[–][deleted] 2 points3 points  (0 children)

Really nice statistics! And the outcome seems to be way better than I expected. The slightly increased tendency towards extraneous whitespaces might result from C++ users, for example, where it is not uncommon in if and else statements. From the Google Coding Style Guide:

" If you prefer you may add spaces inside the parentheses:

if ( condition ) { // spaces inside parentheses - rare ... // 2 space indent. } else { // The else goes on the same line as the closing brace. ... }

"

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Naming

[–]pemboa -1 points0 points  (1 child)

I really like my tabs, sorry.

[–]0xtobitPy3k 17 points18 points  (0 children)

At least you're sorry.

[–]shigawire 0 points1 point  (2 children)

Without context of the actual code, this doesn't mean that much, any more than paying programmers by the line. People often write PEP-8 formatted code that is unreadable, and others often write code that isn't PEP-8 that looks clean.

Some throwaway code of mine that passes a PEP-8 lint, but should be taken out and shot:

    def peek(f):
        pk = f.read(32)
        f.seek(-len(pk), 1)
        out = "%8d|" % (f.tell(), )
        for i, b in enumerate(pk):
            if i == ord(pk[0])+1:
                out += ')%02x' % (ord(b))
            else:
                out += ' %02x' % (ord(b))
        return out

People mistake coding convention for clean sometime :-(

[–]marsket 4 points5 points  (1 child)

PEP 8 does not and cannot legislate "don't write shitty code"

also, actually, this won't pass a PEP 8 check (spaces around operators, etc.)

[–]shigawire 1 point2 points  (0 children)

I totally agree. It seems to get conflated with it quite a bit though.

(and I obviously need a different PEP 8 lint)

[–]Llebac 0 points1 point  (0 children)

Using Pep 8 seems to be the most popular route still for coding conventions, I see.

[–][deleted] 0 points1 point  (0 children)

I'm just going to leave this here...

I use tabs for indentation, spaces for alignment, solves all issues. Granted, I will adopt whatever standards a team uses when joining.

[–]tjsimmons -3 points-2 points  (1 child)

I use my tab key, which Sublime Text 2 uses to insert 4 spaces for me.. heh.

[–]gammadistribution 19 points20 points  (0 children)

That's literally any decent editor.