3 3. Code style
Curs3d edited this page 2016-06-02 20:57:40 +03:00

Indentation

Never use tabs! Use 4 spaces to indent.

Quotes

Always use double-quotes! Only use single-quotes when the string contains double-quotes that would need to be escaped.

Capitalization

Do not use camelCase for variable or function names! Use under_score naming. camelCase is okay when used like import foo.bar.camelCase as camelCase.

Aligning variable assignments (NOTE: QUESTIONABLE SECTION)

In case you have multiple variable assignments, align the equals sign:

# bad
foo = 1
foobar = 2
a = 3

# good
foo    = 1
foobar = 2
a      = 3

Pro Tip: Use the AlignTab plugin for Sublime Text!

Horizontal spacing

Use at least one space left and one space right to equals signs, and one space right to colons.

Vertical spacing

Leave two empty lines before function definitions. In case you need to use @hook.something, add the two lines before that, directly followed by the definition.

Meaningful names

Give function and variable names meaningful names. If you want to shorten long names, that's fine, but leave a comment on assigment with the actual meaning.

Readability

Don't create long lines with lots of function calls. Split into multiple lines instead. Also avoid methods with lots of lines. Split into multiple methods instead.

# bad
foo = int(player_data[str(server.getPlayer(args[4]).getUniqueId())]["details"].["last_login"].strftime("%s"))

# good
player     = server.getPlayer(args[4])
player_id  = uid(player)
logintime  = player_data[played_id]["last_login"]
epoch_time = int(logintime.strftime("%s"))

Comments

Comments are good! Please comment everything that's not obvious or where it makes it easier to understand

PEP 8

Please take a look at PEP 8 - Style Guide for Python Code for anything that has not been covered above.