Skip to content

Stylesheet

Matheus Richard edited this page Mar 21, 2017 · 19 revisions

Introduction

The style sheet is entirely based on PEP 8, which is a Python conventions document. Furthermore, we are using the Wing Python IDE, which checks the code to make sure that it is following the PEP 8 standards.

Code Layout

1. Identation

1.1. Use 4 spaces per indentation level.

Wrong Example:

if (iterator > constant)
  do_something()

Right Example:

if (iterator > constant)
    do_something()  

1.2. Align operands with delimiter's opening.

Wrong Examples:

# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
    var_three, var_four)
# Further indentation required as indentation is not distinguishable.
def long_function_name(
    var_one, var_two, var_three,
    var_four):
    print(var_one)

Right Examples:

# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
                         var_three, var_four)

# More indentation included to distinguish this from the rest.
def long_function_name(
        var_one, var_two, var_three,
        var_four):
    print(var_one)

1.3. Closing brace/bracket/parenthesis may be lined up under the first character of the line that starts the multi-line construct.

Wrong Example:

my_vector = [
    1, 2, 3,
    4, 5, 6,
    7, 8, 9]

Right Example:

my_vector = [
    1, 2, 3,
    4, 5, 6,
    7, 8, 9
]

Organizing control structures

  • Use '(' in paraghaphs decision structures

Wrong Example:

if age >= 18:
print (maior de idade)

Right Example: if (age >= 18):
print (maior de idade)

  • In a Big conditions put space between they Wrong Example:

if((name=='Carlos'and age==10)or(name=='Ana'and age == 8)):

Right Example:

if((name == 'Carlos' and age == 10) or (name == 'Ana' and age == 8)):

  • Put white lines between paraghaphs

Wrong Example:

name = input ('digite seu nome')
age = input ('digite sua idade')
people = People(name , age)
print(people.getName())
print(people.getAge())

Right Example:

name = input ('digite seu nome')
age = input ('digite sua idade')

people = People(name,age)

print(people.getName())
print(people.getAge())

Indentation of control structures

  • When a structure is subordinated to another, it should be indented under that structure.

Right Example:

if (var_1 > 100): 
    do_this( )
else:
    do_that( )

Wrong Example:

if (var_1 > 100): 
    do_this( )
    else:
    do_that( )

(Wrong because the else instruction is indented under the for instruction, but is not subordinated to it)

  • Blank lines between “paragraphs”

You should group related statements as in a paragraph of a text. Also, separate them from the other instructions with blank lines.

Right Example:

var_1 = 0
var_2 = 2 ** 5
result = var_1 + var_2

string_1 = “Hello ”
string_2 = “World!”
phrase = string + string2

Wrong Example 1:

var_1 = 0
var_2 = 2 ** 5
result = var_1 + var_2
string_1 = “Hello ”
string_2 = “World!”
phrase = string + string2

Wrong Example 2:

var_1 = 0
string_1 = “Hello ”
string_2 = “World!”
var_2 = 2 ** 5
phrase = string + string2
result = var_1 + var_2
  • Complicated expressions

In overly complicated instructions you should write each condition in a line, aligned with the condition's open character.

Right Example:

if (((var_1 > var_2) && (var_1 > var_3)) ||
   ((var_1 > 1) && (var_2 < 10)) ||
   (var_3 = 0)):
    do_this( )
else:
    do_that( )

Wrong Example 1:

if (((var_1 > var_2) && (var_1 > var_3)) ||
((var_1 > 1) && (var_2 < 10)) || 
(var_3 = 0)):
    do_this( )
else:
    do_that( )

Wrong Example 2:

if (((var_1 > var_2) && (var_1 > var_3)) || ((var_1 > 1) && (var_2 < 10)) || (var_3 = 0)):
    do_this( )
else:
    do_that( )

Comments Structure

  • All comments must have a blank line above before begins
  • Comments must have a maximum of 75 characters per line
  • Single line comments must be written with # (space) (comment)
  • All comments must be idented with the code
  • All comments must begin with first letter capitalized
  • All comments must be written in English


            Right Example:

           # Loop to iterate in the range of table
            for x in range(0, 3):

                  # Printing the iterator iterator iterator iterator iterator iterator iterator
                  print(x)

            Wrong Example:

                # Loop to iterate in the range of table
            for x in range(0, 3):

             #Printing the iterator iterator iterator iterator iterator iterator iterator iterator iterator iterator iterator
                  print(x)

  • Multiple line comments must be writed with """ (space) (comment) (space) """ at the end
  • Lines bellow the first one must be allign with the line above


            Right Example:

            """ Loop to iterate in the range of table
                  of students """
            for x in range(0, 3):

                  # Printing the iterator iterator iterator iterator
                  print(x)

            Wrong Example:

            # Loop to iterate in the range of table bla bla bla bla bla bla
            # Bla bla bla bla bla bla bla
            for x in range(0, 3):

             #Printing the iterator iterator iterator iterator iterator iterator iterator iterator
              print(x)

Routines

  • Use blank lines to separate parts of the routine.


            Right example:

def showAccount(accountNumber):

....

#blank line here

def retrieveAccount(accountNumber):

....


            Wrong example:

def showAccount(accountNumber):

....

#this is not a blank line

def retrieveAccount(accountNumber):

   ....
  • Use standart identation on the parameters of the routine


            Right example:

def setBalance (
        id ,
        balance,
        date,
        user
):


            Wrong example:

def setBalance (id, balance, date, user):

    ....

Clone this wiki locally