Refactoring
Dr. Kristian Rother
Goal of the tutorial:
As a participant, I want to refactor a piece of unknown Python code, so that you I apply the same techniques to a larger codebase later.
Motivation:
When you are working on your first real-world Python project, the codebase is typically much larger than any textbook or course example. Over time, software entropy kicks: functions grow longer and longer, the same code gets copy-pasted to multiple places and slowly mutates there, and code that seemed a brilliant idea a few weeks back is now incomprehensible.
Prerequisites:
You should be well fluent in the basic Python data structures, in particular lists and dictionaries. You should also have written functions on your own. It helps if you know how a class looks like and how to recognize a generator function. If you have tried writing any of the above and are not happy about the outcome, you have come to the right place.
The tutorial works with any Python installation >= 3.6. It requires a code editor like PyCharm, VSCode or Spyder (no notebooks). We will use the pytest library.
The tutorial focuses on programming strategies, so you don't need any advanced elements of the Python language. In particular, we will ignore type hints, decorators, asyncio, Dataclasses, ABC's, metaclasses or pattern matching even if these are great and would help. You do not need any deep knowledge of particular Python packages.
Structure:
- What is refactoring?
- Clone or download the space travel game and make sure it runs
- Run the tests
- Identify problematic pieces of code using a checklist of "code smells"
- Split a long function into multiple shorter ones (apply one of the most fundamental refactoring techniques for warming up)
- Extract data structures (2-3 separate examples)
- Encapsulate behavior into a class (this will take longer, because we need to discuss pros and cons of different alternatives)
- Run the tests again (and emphasize that points 3.+8. are the most important ones on the list; still thinking about special effects here)
- Discuss common strategies (functional, class-based and hybrid paradigms, design patterns, testability.
- Closing remarks
The tutorial will be broken down in elementary steps that work both in onsite and online formats. The complete materials will be available under an Open Source License
Dr. Kristian Rother
Affiliation: Freelance
I am a professional Python trainer, developer and author based in Berlin. During the past 4 years I have trained 400 Data Scientists in one of the largest boot camps in Germany. I have translated several well-known O'Reilly books on Python and authored two Python books on my own. I believe everybody can learn programming.