Good Programming Practices: Andrew Showers, Salles Viana Alac
Good Programming Practices: Andrew Showers, Salles Viana Alac
Know when to be inconsistent, guidelines will serve as a rule of thumb and not an
absolute. Ask yourself if the rules being applied make the code more readable.
Code Refactoring
● Rewriting code for clarity, not bug fixing. Similar to writing paper drafts
VS
Avoid Deep Nesting
VS
Avoid Explicit Comparisons (when possible)
https://docs.python.org/3/library/stdtypes.html#truth-value-testing
VS
Avoid Long Lines
● Too many operations per line is confusing to read
VS
One Statement per Line
VS
Strive for Simplicity
Code should be explicit and straightforward
VS
Strive for Simplicity (cont.)
Use list comprehensions, filter(), and map() where applicable
VS
VS
Strive for Simplicity (cont.)
Use enumerate to keep track of index and element values
VS
Commenting
● Explain logic in a clear and understandable manner
○ Avoid jargon when possible
○ Aim for explanation to be understood by non-programmers
VS
Commenting (cont.)
“At the beginning of every routine, it is helpful to provide standard, boilerplate
comments, indicating the routines purpose, assumptions, and limitations. A
boilerplate comment should be a brief introduction to understand why the routine
exists and what it can do."
https://msdn.microsoft.com/en-us/library/aa260844(v=vs.60).aspx
Examples of function boilerplate:
Documentation
● Sphinx
○ http://www.sphinx-doc.org/en/stable/index.html
○ http://www.sphinx-doc.org/en/stable/ext/example_google.html
● EpyDoc
○ http://epydoc.sourceforge.net
● PythonDoc
○ http://effbot.org/zone/pythondoc.htm#syntax
Black-Box Testing
● Given a function, you know what the output should be for given inputs
○ Select cases which cover all typically expected behavior
○ Software can verify function still works by running these tests
● DocTest
○ https://en.wikipedia.org/wiki/Doctest
Avoid Convoluted Tricks
Just because you can doesn't mean you should
Examples:
Ok now?
Another common mistake...
Another common mistake...
Const/reference
Ok now?
Const/reference
getName() → always returns a COPY of name…
Const/reference
Ok now?
Const/reference
Where does the compilation error happen? What if getName() was not const?
Const/reference
Const returned value: you Const function:
can’t modify the returned - Can’t modify object.
reference. - → can be applied to a const object
[i]
Code reuse
Avoid having duplicate code
Typo: missing a * →
will have to fix here
AND in the
constructor...
Matrix a(5,2);
Matrix b(3,3);
Now we only
have to fix the
typo here...
References
PEP 8 -- Style Guide for Python Code:
https://www.python.org/dev/peps/pep-0008/