6

We have been using Django for a long time. Some old code is not being used now. How can I find which code is not being used any more and remove them.

I used coverage.py with unit tests, which works fine and shows which part of code is never used, but the test covered is very low. Is there any way to use it with WSGI server to find which code have never served any web requests?

6
  • 2
    You want to find which code is not used any more but are using coverage.py which shows which part of code is never used. So what is your question? Please read How do I ask a good question?. Commented Jul 24, 2015 at 4:12
  • 2
    A similar question Commented Jul 24, 2015 at 4:13
  • @Ajoy Sorry for confusing you, I updated my question. In the similar question, the best answer recommends coverage.py but I don't know how to use it in this case. Can you answer this question with more details or provide me any related links? Thanks! Commented Jul 24, 2015 at 4:34
  • 2
    Have a look at this. Commented Jul 24, 2015 at 4:49
  • stackoverflow.com/questions/693070/… Commented Mar 27, 2017 at 15:01

3 Answers 3

2

As other answers indicate coverage.py accurately finds out which parts of the code are never executed, but coverage requires your code to be actually run to perform the analysis. Vulture on the other hand, runs static analysis for finding dead (unused code) for Python Programs. Also, if you run vulture on both your library and test suite, you might be able to find untested code.

Vulture is a standard PyPI package and can be installed using pip:

$ pip install vulture

Run vulture using the command:

$ vulture apps/ tests/ --exclude settings

Bear in mind that due to Python's dynamic nature, there may be some false positives, but they can be dealt with by the means of "Whitelists" - Please refer to this answer by Rahul for more information on how to use Vulture with django and tackle false positives.

Sign up to request clarification or add additional context in comments.

Comments

1

It depends on what you mean by unused code.

For unreachable dead code, like functions are were never called, classes that are never instantiated, you can use a pure static code analyzer to find them. Pylint is a good option. Bear in mind that this is not 100% accurate, false positive is possible:

# static analysis can't detect methods called this way
func = getattr(obj, "func_name")
func() 

For code that are reachable, but never reached. You have to rely on tools like coverage.py, and improve your test coverage.

3 Comments

As a web site, when a function is registered as web view, it's the first call, but if this view is never visited by any user, that's what 'never reached' means in my case.
In that case, you need to analyze dead links, urls that are never visited. Python code analyzer cannot help you there.
I know, my idea is using some tool to wrap the wsgi app and running online for a period, and then manually check the unreached URLs.
1

On a well tested project, coverage would be ideal but with some untested legacy code I don't think there is a magical tool.

You could write a big test loading all the pages and run coverage to get some indication.


Cowboy style:

If it's not some critical code and you're fairly sure it's unused (i.e. not handling payments, etc.). Comment it out, check that the tests pass, deploy and wait a week or so before removing it definitely (or putting it back if you got a notification).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.