Now that we have seen how static tools can be used to report a wide range of errors and issues in our Python code, let us do a simple exercise of refactoring our code. We will take our poorly written metric test module as the use case (the first version of it), and perform a few refactoring steps.
Here are the rough guidelines we will follow when doing the refactoring:
Fix complex code first: This will get a lot of code out of the way, as typically, when a complex piece of code is refactored, we end up reducing the number of lines of code. This overall improves the code quality, and reduces code smells. You may be creating new functions or classes here, so it always helps to perform this step first.
Do an analysis of the code now: It is a good idea to run the complexity checkers at this step, and see how the overall complexity of the code—class/module or functions—has been reduced. If not, iterate again.
Fix code smells next: Fix any issue with code smells—class, function...