Using Vulture to improve used-to-unused code ratio of your Python Programs
Rahul Jha (~RJ722) |
Maintaining a high level of code quality is important for any serious project. One aspect of this is ensuring that all code is actually used. There are many reasons for dead code ending up in a project. The most common is refactoring, but another is misspellings, which are only detected at runtime for dynamic languages. Finding and removing dead code allows to keep the code base clean and reduces bugs.
This talk is focused on how we can use Vulture to find dead code. It helps you find unused code in Python programs and it is useful for cleaning up and finding errors in large code bases. If you run Vulture on both your library and test suite you can find untested code. Due to Python's dynamic nature, static code analyzers like Vulture are likely to miss some dead code. Also, code that is only called implicitly may be reported as unused. Nonetheless, Vulture can be a very helpful tool for higher code quality.
One part of this talk is to discuss how to automate testing for dead code with Vulture. There are quite a few options available:
- Adding vulture to your continuous integration testing.
- A script using the Vulture API for custom tests.
- VultureBear: Integration with coala - a static code analysis tool.
- Integration with automatic analysis tools like Gitmate, etc. for automatic code-reviews with native support for GitHub and GitLab.
Please also note that a talk was given on the same subject during PyCon India 2017 by the same speaker, but since then, several features have been added to Vulture and it's API.
The only prerequisite is willingness to do the hard work required for making your codebase maintainable.
If someone in audience wants to follow along, please install Python > 2.6 and pip on your computer.
- The slides accompanying the talk, along with all the examples may be found at RJ722/reducing-dead-code
- RJ722/example-vulture displays an example on how we can integrate vulture with CI tests.
There is also this blog post - Why use coverage to find which Python code is run, but as you can guess from the title, it isn't dedicated entirely to vulture, but still serves as a nice introduction for the workflow involved with using Vulture.
Rahul is a final year Electronics Engineering student at Aligarh Muslim University.
He is the co-author of, and currently maintains, Vulture. He loves programming robots. Currently, he is working with the AUV ZHCET Team on an Autonomous Underwater Vehicle, Safina-é-Aabdoz.
Other than programming, he has interest in Urdu Shayari, history and politics. He sometimes write about this stuff at rj722.github.io.
The best way to reach me is through e-mail: email@example.com, firstname.lastname@example.org.
You may also find me lurking in #dgplug, #qutebrowser and #python-india channels on freenode. (IRC Nick: RJ722)