Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Anthony Shaw - Wily Python: Writing simpler and...

Anthony Shaw - Wily Python: Writing simpler and more maintainable Python

Everyone starts with the best intentions with their Python projects, "this time it's going to be clean, simple and maintainable". But code evolves over time, requirements change and codebases can get messy and complicated quickly.

In this talk, you will learn how to use wily to measure and graph how complicated your Python code is and a series of practical techniques to simplify it. wily will show you which parts of your projects are becoming or have become hard to maintain and need a refactor. Once you know where the skeletons are, you will learn practical techniques for refactoring "complex" code and some resources to use to take your refactoring to the next level.

https://us.pycon.org/2019/schedule/presentation/162/

PyCon 2019

May 03, 2019
Tweet

More Decks by PyCon 2019

Other Decks in Programming

Transcript

  1. Introduction Is my code complicated? By what measure? How do

    I simplify it? Beginner Level Intermediate to Advanced
  2. 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 *
  3. 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
  4. def foo(): a = 1 b = a + 1

    return b Halstead in the AST
  5. Volume and Difficulty Better than LoC Length (N) Vocab (h)

    Volume (V) !"#$ operators operands Difficulty (D) ∃! 2 operands ∃! ∑ A great compliment to CC
  6. 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
  7. 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)
  8. $ 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
  9. Testing and refactoring •High test coverage should be a given

    •Test coverage is not an indicator of behavioral coverage •Fixing known bugs is hard
  10. • 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