code reviews in python - pyzh
TRANSCRIPT
Code Reviews in PythonCésar Cárdenas DesalesZürich Python User Group, June 2016
2
About me
● Software Architect at Webrepublic AG
● Python user since v.2.0
● Co-organizer of the Swiss Python Summit
¿What are Code Reviews?
3
Systematic examination of computer source code…- Wikipedia
Benefits
4
● Timeless
● Spread knowledge
● The Ego Factor
● Increase quality of software
Timeless
5
● Being here for a while (Fagan, 1976)
● Not going away any time soon
● Technology agnostic
● Not a fad
Timeless
6
● Fad?○ Machine learning? ○ SOA? ○ OOP?○ Client server architecture?○ Big data?○ Cloud computing?○ SVN
● Fad ○ Aspect Oriented Programming○ Java applets○ Visual Programming○ Enterprise JavaBeans○ CGI (Web)○ Webmasters
Spread knowledge
7
● Beginners learn from the experts’ code
● The experienced get to know other modules or systems
● Increases “Bus Factor”
The Ego Factor
8
● No “quick and dirty” fixes
● Reputation at stake on each commit
Increase quality of software
9
● Reviewed solutions tend to be better
● Defects are found and fixed early
Source: IBM Systems Sciences Institute
Assign Reviewers
10
● Everyone participates
● Works well with couples
● ¿How long? ~1 sprint or week
Assign Reviewers
11
● 50%
● 100%
12
Code Review with Github
ImplementationPull
Request
Discusion
Preparation Revision Closure
Revision
Merge
Branch
12
Commit
Preparation
13
1. Branch
2. Commit
3. Pull Request
Revision
14
4. First response
5. Review code
6. Written comments / discussion
Closure
15
6. Implement changes under agreement
7. Merge
1616
Rule #1
It’s not personal
Basic Rules
17
● Respond (ACK) and solve in a timely manner
● Not too little, not too much (400 loc. max.)
● Ask before overwriting somebody’s code
● Use checklists
Code conventions and style guides
Criteria - Pythonic code
18
Pycodestyle / Flake8 / Pylint
Code conventions and style guides
Criteria - Pythonic code
19
Pythonic code
pycodestyle / Flake8 / Pylint
Code conventions and style guides
Criteria - Pythonic code
20
Pythonic code
pycodestyle / Flake8 / Pylint
Criteria - Pythonic code
21
Zen of Python
>>> import thisThe Zen of Python, by Tim Peters
Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts....
Pythonic code
Criteria - Pythonic code
22
Pythonic code
Criteria - Pythonic code
23
Pythonic code
Criteria - Pythonic code
24
Pythonic code
Criteria - Pythonic code
25
TODO/FIXME: Name and shame, ticket number, date
Criteria
26
Cyclomatic complexity
Criteria
27
Input data validation
Updated comments & docstrings
Dead or commented out code
Criteria
28
exec() / eval()
Passwords or tokens in repository
Automated unit tests: unittest / pytest / nose / coverage
Criteria
29
Efficient algorithms
Edge cases / off by 1 errors
Duplicated code / copy & paste
Criteria
30
Antipatterns
● God class/function
● Lasagna code
● Magic numbers
● Reinventing the wheel (!)
Criteria
31
Antipatterns
● Circular dependencies(relative imports)
● Premature optimization -> YAGNI
● Boat Anchor
● Object Orgy
Criteria
32
Is there a better solution?
Is it scalable?