Code Reviews in Python - PyZh

33
Code Reviews in Python César Cárdenas Desales Zürich Python User Group, June 2016

Transcript of Code Reviews in Python - PyZh

Page 1: Code Reviews in Python - PyZh

Code Reviews in PythonCésar Cárdenas DesalesZürich Python User Group, June 2016

Page 2: Code Reviews in Python - PyZh

2

About me

● Software Architect at Webrepublic AG

● Python user since v.2.0

● Co-organizer of the Swiss Python Summit

Page 3: Code Reviews in Python - PyZh

¿What are Code Reviews?

3

Systematic examination of computer source code…- Wikipedia

Page 4: Code Reviews in Python - PyZh

Benefits

4

● Timeless

● Spread knowledge

● The Ego Factor

● Increase quality of software

Page 5: Code Reviews in Python - PyZh

Timeless

5

● Being here for a while (Fagan, 1976)

● Not going away any time soon

● Technology agnostic

● Not a fad

Page 6: Code Reviews in Python - PyZh

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

Page 7: Code Reviews in Python - PyZh

Spread knowledge

7

● Beginners learn from the experts’ code

● The experienced get to know other modules or systems

● Increases “Bus Factor”

Page 8: Code Reviews in Python - PyZh

The Ego Factor

8

● No “quick and dirty” fixes

● Reputation at stake on each commit

Page 9: Code Reviews in Python - PyZh

Increase quality of software

9

● Reviewed solutions tend to be better

● Defects are found and fixed early

Source: IBM Systems Sciences Institute

Page 10: Code Reviews in Python - PyZh

Assign Reviewers

10

● Everyone participates

● Works well with couples

● ¿How long? ~1 sprint or week

Page 11: Code Reviews in Python - PyZh

Assign Reviewers

11

● 50%

● 100%

Page 12: Code Reviews in Python - PyZh

12

Code Review with Github

ImplementationPull

Request

Discusion

Preparation Revision Closure

Revision

Merge

Branch

12

Commit

Page 13: Code Reviews in Python - PyZh

Preparation

13

1. Branch

2. Commit

3. Pull Request

Page 14: Code Reviews in Python - PyZh

Revision

14

4. First response

5. Review code

6. Written comments / discussion

Page 15: Code Reviews in Python - PyZh

Closure

15

6. Implement changes under agreement

7. Merge

Page 16: Code Reviews in Python - PyZh

1616

Rule #1

It’s not personal

Page 17: Code Reviews in Python - PyZh

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

Page 18: Code Reviews in Python - PyZh

Code conventions and style guides

Criteria - Pythonic code

18

Pycodestyle / Flake8 / Pylint

Page 19: Code Reviews in Python - PyZh

Code conventions and style guides

Criteria - Pythonic code

19

Pythonic code

pycodestyle / Flake8 / Pylint

Page 20: Code Reviews in Python - PyZh

Code conventions and style guides

Criteria - Pythonic code

20

Pythonic code

pycodestyle / Flake8 / Pylint

Page 21: Code Reviews in Python - PyZh

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....

Page 22: Code Reviews in Python - PyZh

Pythonic code

Criteria - Pythonic code

22

Page 23: Code Reviews in Python - PyZh

Pythonic code

Criteria - Pythonic code

23

Page 24: Code Reviews in Python - PyZh

Pythonic code

Criteria - Pythonic code

24

Page 25: Code Reviews in Python - PyZh

Pythonic code

Criteria - Pythonic code

25

Page 26: Code Reviews in Python - PyZh

TODO/FIXME: Name and shame, ticket number, date

Criteria

26

Cyclomatic complexity

Page 27: Code Reviews in Python - PyZh

Criteria

27

Input data validation

Updated comments & docstrings

Dead or commented out code

Page 28: Code Reviews in Python - PyZh

Criteria

28

exec() / eval()

Passwords or tokens in repository

Page 29: Code Reviews in Python - PyZh

Automated unit tests: unittest / pytest / nose / coverage

Criteria

29

Efficient algorithms

Edge cases / off by 1 errors

Duplicated code / copy & paste

Page 30: Code Reviews in Python - PyZh

Criteria

30

Antipatterns

● God class/function

● Lasagna code

● Magic numbers

● Reinventing the wheel (!)

Page 31: Code Reviews in Python - PyZh

Criteria

31

Antipatterns

● Circular dependencies(relative imports)

● Premature optimization -> YAGNI

● Boat Anchor

● Object Orgy

Page 32: Code Reviews in Python - PyZh

Criteria

32

Is there a better solution?

Is it scalable?

Page 33: Code Reviews in Python - PyZh

33

Let’s make it happen.Thanks!

[email protected]@ccdesales