Docsity

Coding standards, Summaries of Advanced Computer Programming

Partial preview of the text

Download Coding standards and more Summaries Advanced Computer Programming in PDF only on Docsity!

Coding standards

  • Coding standards are guidelines for code style and documentation
  • They may be formal PEP 8(Style Guide for Python Code) standards, or company specific standards

Why bother with a coding standard?

  • Consistency between developers
  • Ease of maintenance and development
  • Readability, usability, security, performance
  • If you deviate from the standard for any reason, document it “Code is read much more often than it is written" ------ Guido van Rossum

Not recommended Best Practice from qcore import geo import os, sys from shared_workflow import shared from subprocess import Popen, PIPE import vtk import os import sys from subprocess import Popen, PIPE import vtk from qcore import geo from shared_workflow import shared from math import * import math or from math import ceil

Not recommended Best Practice if number != None: if number is not None: if flag == True: if flag:

Bad: use global statement:

global WIDTH

Avoid or Possibly use class instead

self.width

Bad: use Magic number

fraction = available_nodes / 264

define as a constant instead:

MAX_NODES = 264 person = { 'first': 'Tobin', 'last': 'Brown', 'age': } # Bad: we have to change the replacement fields within our string, once we add new values print('{} {} is {} years old'. format( person['first'], person['last'], person['age']) ) print('{first} {last} is {age} years old'. format( ****** person)) # Output Tobin Brown is 20 years old

exec

The exec statement enables you to dynamically execute arbitrary Python code which is stored in literal strings. Building a complex string of Python code and then passing that code to exec results in code that is hard to read and hard to test. Anytime the use of exec error is encountered, you should go back to the code and check if there is a clearer, more direct way to accomplish the task. Not recommended Best Practice s = "print("Hello, World!")" exec(s)

Output

Hello, World! def print_hello_world(): print("Hello, World!") print_hello_world() Eg. In slurm_gm_workflow/install.py, this statement needs fixing, as it's hard to find what params it imports, would they have naming conflicts with the variables inside the current script etc.

Comments

  • Comments at top to describe the purpose of a script
    • Comments to describe the purpose and params of function
  • Comments to describe what does this line of code do, so it's easier to track back and for others to understand
  • Decide a docstring style
  • Comment clearly in the right place
  • Update comments when codes changes Good examples:

Auto Coding format – Black

  • Coding style is a strict subset of PEP 8 Note the line length in Black is default to 88. To reset, use option --line-length 79
  • Really useful in a team, save time and money You will have fewer discussions on where spaces should be, where a new line should be inserted etc and faster pull request processes. https://github.com/ambv/black

Summary

Comply with PEP Find a standard that suits our projects and stick to it

Further readings

https://www.python.org/dev/peps/pep-0008/ http://google.github.io/styleguide/pyguide.html http://docs.quantifiedcode.com/python-anti-patterns/