A smooth sea never made a skilled sailor. - Franklin D. Roosevelt
Hi Pythonistas, a new week, a new 'bite' of Python coding. Wow challenge 30 already! This week we let you hone your refactoring skills. Learn, code, PR submit (don't be shy) and above all have fun!
The challenge is pretty simple:
Go through you code and identify something you think can be improved.
Write some tests to verify it does the right thing. This could be with Pytest, unittest or just adding a simple assert, just have a safeguard in place before starting to touch the code. Version control is your other friend here.
Iterate over various small improvements, running the tests after each step to verify nothing breaks.
When done submit a PR for our weekly review - this week we will be extra picky on PEP8 compliance! ;)
Update: somebody asked for scripts to refactor. Excellent point. As refactoring is such an important skill we will do a part II sometime soon where we will give you a couple of PyBites scripts to refactor!
Here are some resources you could use:
- The Importance of Refactoring Code
- From Script to Project - Packaging Your Code in Python
- Code Challenge Pilot - code review (our code challenges actually started out with this refactoring post)
- Learning from Python mistakes
- 5 min guide to PEP8
- Errors should never pass silently
- Beautiful, idiomatic Python
Worth checking out!
- A nice article we caught in last week's Import Python newsletter: Refactoring with tests in Python: a practical example
- PyCon 2016 Brett Slatkin talk: Refactoring Python: Why and how to restructure your code.
- Dive Into Python 3 - Chapter 10 - Refactoring
- Martin Fowler's Refactoring
- Uncle Bob's Clean Code
- Sig's Building Maintainable Software (Bob's summary/ review here)
- A classic: Design Patterns
This doc also provides you with instructions how you can submit your code to our community branch via a Pull Request (PR). We will feature your PRs in our end-of-the-week challenge review (previous editions).
Last but not least: there is no best solution, only learning more and better Python. Good luck!
Keep Calm and Code in Python!
-- Bob and Julian