WRITING QUALITY
CODE
IDEAS, TECHNIQUES AND TOOLS FOR
IMPROVING THE QUALITY OF WRITTEN CODE
Radosław Jankiewicz / /@radek_j radekj
WHO AM I
Programming in Python since 2007
Web applications (mostly)
RANDOM FACT...
 
AGENDA
1. Definition of code quality
2. Why is it important
3. How to measure the quality of code
4. How to improve quality of written code
Good practices
Useful tools
Other hints
5. Q&A
HOW TO DEFINE HIGH QUALITY
CODE?
CHARACTERISTICS OF SOFTWARE
QUALITY
External
Internal
External characteristics of software quality
Corectness
Usability
Efficiency
Reliability
Integrity
Adaptability
Accuracy
Robustness
Internal characteristics software quality
Maintainability
Flexibility
Portability
Reusability
Readability
Testability
Understandability
GOOD CODE FROM THE
DEVELOPER'S POINT OF VIEW:
UNDERSTANDABLE
UNDERSTANDABLE
Time spent by a programmer
Understanding code
Modyfying existing code
Writing new code
Highcharts.com
http://blog.codinghorror.com/when-understanding-means-rewriting
HOW IMPORTANT IS HIGH QUALITY
OF CODE
POOR QUALITY CODE COSTS
Time per feature
Code quality
Code quality vs time for feature
Dependency of code quality and time required for implementing a new feature
Highcharts.com
HOW TO MEASURE THE QUALITY OF
CODE
 
SOFTWARE QUALITY METRICS
Cyclomatic Complexity
Halstead complexity measures
Maintainability Index
CYCLOMATIC COMPLEXITY
Construct Effect
on CC
Reasoning
if +1 An if statement is a single decision.
else +0 The else statement does not cause
a new decision.
for +1 There is a decision at the start of
the loop.
Boolean
Operator
+1 Every boolean operator (and, or)
adds a decision point.
Full table: https://radon.readthedocs.org/en/latest/intro.html
CYCLOMATIC COMPLEXITY
def example(foo, bar, baz):
    if foo > bar:
        if foo > baz:
            return foo
        else:
            return baz
    elif foo == bar:
        return bar
    else:
        return baz
CC = 4
HALSTEAD METRICS
η1 = the number of distinct operators
η2 = the number of distinct operands
N1 = the total number of operators
N2 = the total number of operands
HALSTEAD METRICS
def example(foo, bar, baz):
    if foo > bar:
        if foo > baz:
            return foo
        else:
            return (baz / 3)
    elif foo == bar:
        return bar
    else:
        return baz
η1 = 7 (example, if, else, elif, (, ), >, ==, /, return)
η2 = 4 (foo, bar, baz, 3)
N1 = 16 (all operators)
N2 = 14 (all operands)
HALSTEAD METRICS
Program vocabulary:
Program length:
Calculated program length:
Volume:
Difficulty:
Effort:
Time required to program: seconds
η = +η1 η2
N = +N1 N2
= +Nˆ η1 log2
η1 η2 log2
η2
V = N ηlog2
D = ⋅
η1
2
N2
η2
E = D ⋅ V
T =
E
18
MAINTAINABILITY INDEX
MI = 171 − 5.2 ln V − 0.23G − 16.2 ln L
V is the Halstead Volume
G is the total Cyclomatic Complexity
L is the number of Source Lines of Code (SLOC)
RADON
CC number
$ radon cc ./url.py ­s
./url.py
    M 287:4 URLMethodsMixin.resource_url ­ C (18)
    M 35:4 URLMethodsMixin._partial_application_url ­ C (17)
    M 85:4 URLMethodsMixin.route_url ­ C (16)
    C 31:0 URLMethodsMixin ­ B (7)
    M 539:4 URLMethodsMixin.static_url ­ A (5)
    F 753:0 static_url ­ A (3)
MI index
$ radon mi ./url*.py ­s
./urldispatch.py ­ A (56.71)
./url.py ­ A (46.64)
RADON - CC RESULTS
CC
score
Rank Risk
1 - 5 A low - simple block
6 - 10 B low - well structured and stable block
11 - 20 C moderate - slightly complex block
21 - 30 D more than moderate - more complex
block
31 - 40 E high - complex block, alarming
41+ F very high - error-prone, unstable block
RADON - MI RESULTS
MI score Rank Maintainability
100 - 20 A Very high
19 - 10 B Medium
9 - 0 C Extremely low
WEB FRAMEWORKS - MI RESULTS
Rank Pyramid (187
files)
Flask (61
files)
Django (836
files)
A 97.8% 100% 98.3%
B 1.6% 0% 0.3%
C 0.5% 0% 1.3%
PYLINT
Static code analysis
Coding Standard
Error detection
Refactoring help
Fully customizable
Editor/IDE integration
PYLINT
def example(foo, a, blah):
    qux = 123
    if foo > a:
        return foo
    else:
        return datetime.now()
************* Module a
C:  1, 0: Missing module docstring (missing­docstring)
C:  1, 0: Black listed name "foo" (blacklisted­name)
C:  1, 0: Invalid argument name "a" (invalid­name)
C:  1, 0: Missing function docstring (missing­docstring)
E:  6,15: Undefined variable 'datetime' (undefined­variable)
W:  1,20: Unused argument 'blah' (unused­argument)
W:  2, 4: Unused variable 'qux' (unused­variable)
Global evaluation
­­­­­­­­­­­­­­­­­
Your code has been rated at ­8.33/10
MORE TOOLS
R. Ganczarek
Code Quality in Python - tools and reasons
Tomorrow, 16:45 (Barria 2 room)
HOW TO IMPROVE QUALITY OF
WRITTEN CODE
PEER CODE REVIEW
Decreases number of bugs
Enforces writting neat code
Speeds up learning
Enhances the team culture
CODE REVIEW - USEFUL RULES
All changesets get code reviewed
Automate everything you can
Everyone makes code reviews / everybody gets code
reviewed
CODE REVIEW TOOLS
Pull requests inline comments (Github / Bitbucket / ...)
Gerrit
Crucible
Phabricator
many more...
READABILITY COUNTS
CODING CONVENTION
Keep the code consistent with the project's convention.
Use automatic syntax/code style guide checking.
PEP-8 is a good option.
NAMING VARIABLES, CLASSES,
METHODS...
“There are only two hard things in
Computer Science: cache invalidation and
naming things.”
Phil Karlton
Variable name for the maximum number of people in a car
At first - it should be descriptive
x = 5  # bad
data = 5  # bad
max = 5  # very bad
... but not too long ...
maximum_number_of_people_in_the_car = 123  # bad
abbreviations are acceptable
num_seats = 5  # not that bad
total_seats = 5  # good
max_passengers = 5  # good
Avoid double negative boolean logic
seat.is_not_occupied = True  # bad
seat.is_empty = True  # ok
DOCSTRINGS
MUST be valid. Wrong docstring is worse than no
docstring at all. Keep it up to date.
Do not explain the implementation details
Summarize the function's behaviour
Document function's arguments, return value(s), side
effects, exceptions raised, and restrictions on when it can
be called (all if applicable)
COMMENTS
MUST be valid. Wrong comment is worse than no
comment at all
Inline comments are unnecessary and in fact distracting if
they state the obvious. Don't do this:
x = x + 1  # Increment x
KEEP YOUR TESTS CLEAN
“If you let the tests rot, then your code will
rot too. Keep your tests clean.”
Rober C. Martin - "Clean Code"
SELF EFFORT
What else could you do to increase the quality of written
code?
More pythonic:
KNOW PYTHON IDIOMS
>>> x > 10 and x <= 20
>>> 10 < x <= 20
1
More pythonic:
KNOW PYTHON STANDARD LIBRARY
>>> colors = ['blue', 'red', 'green', 'red', 'blue', 'red']
>>> [(x, colors.count(x)) for x in set(colors)]
[('blue', 2), ('green', 1), ('red', 3)]
>>> from collections import Counter
>>> Counter(colors)
Counter({'red': 3, 'blue': 2, 'green': 1})
KNOW PYTHON'S SYNTAX
EXPRESSIONS
>>> from contextlib import contextmanager
>>> @contextmanager
... def tag(name):
...     print "<%s>" % name
...     yield
...     print "</%s>" % name
...
>>> with tag("h1"):
...    print "foo"
...
<h1>
foo
</h1>
SELF EFFORT
Read valuable books
Read documentation
Practice a lot
CHECKIO.ORG
 
 
 
 
QUESTIONS?
THANKS AND CREDITS
reveal.js by Hakim El Hattab (MIT)
Steve McConnell "Code complete 2"
Robert Martin - "The clean coder"
http://blog.codinghorror.com
http://docs.python-guide.org
https://radon.readthedocs.org
http://www.pylint.org
http://www.checkio.org
STX Next
My wife Karolina

More Related Content

PPTX
Coding standards
PDF
Python - code quality and production monitoring
PDF
Understanding, measuring and improving code quality in JavaScript
PPTX
Code quality
PPTX
Python code profiling - Jackson Isaac
PDF
Code-Review-Principles-Process-and-Tools (1)
PPSX
Coding standard
PDF
Measuring Code Quality in WTF/min.
Coding standards
Python - code quality and production monitoring
Understanding, measuring and improving code quality in JavaScript
Code quality
Python code profiling - Jackson Isaac
Code-Review-Principles-Process-and-Tools (1)
Coding standard
Measuring Code Quality in WTF/min.

What's hot (19)

PDF
Code coverage & tools
PPTX
Coding standards
PPT
Code review
PDF
Standard Coding, OOP Techniques and Code Reuse
PPTX
Software development best practices & coding guidelines
PPTX
Object Oriented Apologetics
PDF
Presentation slides: "How to get 100% code coverage"
PPTX
Git branching policy and review comment's prefix
PDF
Do Bugs Reside in Complex Code?
PPTX
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
PPTX
Trans coder
PDF
Code review best practice
KEY
TDD refresher
PDF
Prefer Code to Comments
PDF
PPTX
Code Coverage
PPTX
Test driven development(tdd)
PDF
Inside Requirements
Code coverage & tools
Coding standards
Code review
Standard Coding, OOP Techniques and Code Reuse
Software development best practices & coding guidelines
Object Oriented Apologetics
Presentation slides: "How to get 100% code coverage"
Git branching policy and review comment's prefix
Do Bugs Reside in Complex Code?
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
Trans coder
Code review best practice
TDD refresher
Prefer Code to Comments
Code Coverage
Test driven development(tdd)
Inside Requirements
Ad

Similar to Euro python 2015 writing quality code (20)

PDF
Software maintenance PyConPL 2016
PPTX
Improving Code Quality Through Effective Review Process
PDF
How to write good quality code
PDF
Code Reviews in Python - PyZh
PPTX
White box testing
PDF
Software maintenance PyConUK 2016
PPTX
Writing clean scientific software Murphy cleancoding
PPTX
Best-Practices-for-Writing-Clean-Code.Presentation
PPTX
Best-Practices-in-Writing-Clean-Maintainable-Code
PDF
Effective Python 90 specific ways to write better Python Second Edition Brett...
PPTX
Expanding the idea of static analysis from code check to other development pr...
PDF
Effective Python 90 specific ways to write better Python Second Edition Brett...
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
PDF
Containing Quality
PDF
Robust Python Write Clean And Maintainable Code 1st Edition Patrick Viafore
PPT
Software Engineering
PDF
Effective code reviews
PPTX
Stop wasting-time-by-applying-clean-code-principles
PDF
An Introduction To Software Development - Final Review
PDF
Systems se
Software maintenance PyConPL 2016
Improving Code Quality Through Effective Review Process
How to write good quality code
Code Reviews in Python - PyZh
White box testing
Software maintenance PyConUK 2016
Writing clean scientific software Murphy cleancoding
Best-Practices-for-Writing-Clean-Code.Presentation
Best-Practices-in-Writing-Clean-Maintainable-Code
Effective Python 90 specific ways to write better Python Second Edition Brett...
Expanding the idea of static analysis from code check to other development pr...
Effective Python 90 specific ways to write better Python Second Edition Brett...
Clean Code. An Agile Guide to Software Craft Kameron H.
Containing Quality
Robust Python Write Clean And Maintainable Code 1st Edition Patrick Viafore
Software Engineering
Effective code reviews
Stop wasting-time-by-applying-clean-code-principles
An Introduction To Software Development - Final Review
Systems se
Ad

Recently uploaded (20)

PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
Five Habits of High-Impact Board Members
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PPTX
TEXTILE technology diploma scope and career opportunities
PPTX
Configure Apache Mutual Authentication
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
Build Your First AI Agent with UiPath.pptx
PDF
Architecture types and enterprise applications.pdf
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPTX
The various Industrial Revolutions .pptx
PDF
Statistics on Ai - sourced from AIPRM.pdf
PDF
Consumable AI The What, Why & How for Small Teams.pdf
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Basics of Cloud Computing - Cloud Ecosystem
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Final SEM Unit 1 for mit wpu at pune .pptx
Enhancing plagiarism detection using data pre-processing and machine learning...
Training Program for knowledge in solar cell and solar industry
Getting started with AI Agents and Multi-Agent Systems
Five Habits of High-Impact Board Members
sustainability-14-14877-v2.pddhzftheheeeee
OpenACC and Open Hackathons Monthly Highlights July 2025
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
TEXTILE technology diploma scope and career opportunities
Configure Apache Mutual Authentication
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Build Your First AI Agent with UiPath.pptx
Architecture types and enterprise applications.pdf
A review of recent deep learning applications in wood surface defect identifi...
The various Industrial Revolutions .pptx
Statistics on Ai - sourced from AIPRM.pdf
Consumable AI The What, Why & How for Small Teams.pdf

Euro python 2015 writing quality code