Slide 1

Slide 1 text

Wily Python: Writing simpler and more maintainable Python Anthony Shaw @anthonypjshaw

Slide 2

Slide 2 text

Is my code complicated? By what measure? How do I simplify it?

Slide 3

Slide 3 text

Introduction Is my code complicated? By what measure? How do I simplify it? Beginner Level Intermediate to Advanced

Slide 4

Slide 4 text

Slide 5

Slide 5 text

Lines of code

Slide 6

Slide 6 text

Fewer lines doesn’t mean less complexity

Slide 7

Slide 7 text

“ Readability counts ” — Tim Peters

Slide 8

Slide 8 text

Cyclomatic Complexity Make it a meal? Small, large or super-size? What drink? Diet or regular? Yes Large Cola Diet * Other burgers are available +1 +1 +1 +1 Big Mac please *

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

There’s wisdom in complex code

Slide 12

Slide 12 text

Functionality More Less Less More Users ! Lines of Code "Cyclomatic Complexity " Lines of Code "Cyclomatic Complexity ! Lines of Code ! Cyclomatic Complexity " Lines of Code ! Cyclomatic Complexity

Slide 13

Slide 13 text

Halstead Metrics Warning: This chapter contains math

Slide 14

Slide 14 text

Values Variables Operands ∑ ∑ Built in syntax Operators ∑ Operators and Operands

Slide 15

Slide 15 text

Length and Vocabulary operators operands Vocabulary (h) ∃! ∃! operators operands Length (N) ∑ ∑

Slide 16

Slide 16 text

def foo(): a = 1 b = a + 1 return b Halstead in the AST

Slide 17

Slide 17 text

Volume and Difficulty Better than LoC Length (N) Vocab (h) Volume (V) !"#$ operators operands Difficulty (D) ∃! 2 operands ∃! ∑ A great compliment to CC

Slide 18

Slide 18 text

Effort Volume Difficulty Effort (E) operators operands ∃! 2 operands ∃! ∑ Length (N) Vocab (h) "#$%

Slide 19

Slide 19 text

Maintainability Index 0-25 25-50 50-75 75-100 MI = 171 − 5.2 ln , − 0.23 / − 16.2 ln(2) Halstead Volume Cyclomatic Complexity LoC Unmaintainable Cause for concern Needs improvement Super-hero code

Slide 20

Slide 20 text

Radon $ pip install radon $ radon cc my_application.py -s my_application.py F 4:0 main - B (6) $ radon mi my_application.py -s my_application.py - A (87.42)

Slide 21

Slide 21 text

$ radon hal my_application.py my_application.py: h1: 3 h2: 6 N1: 3 N2: 6 vocabulary: 9 length: 9 calculated_length: 20.264662506490406 volume: 28.529325012980813 difficulty: 1.5 effort: 42.793987519471216

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Cache Revisions Operators Indexes Git Repo wily build

Slide 24

Slide 24 text

Demo of wily

Slide 25

Slide 25 text

Complexity has gravity app.py

Slide 26

Slide 26 text

Testing and refactoring •High test coverage should be a given •Test coverage is not an indicator of behavioral coverage •Fixing known bugs is hard

Slide 27

Slide 27 text

• Wily can benefit you in tracking complexity • Complexity Measures are long-term • Make sure you have good behavioral test coverage • Refactor often • Divide and conquer Talk notes

Slide 28

Slide 28 text

Thanks! realpython.com/python-refactoring [email protected] @anthonypjshaw tonybaloney.github.io