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?

Kane Blueriver
  • 4,170
  • 4
  • 29
  • 48
  • 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?](http://stackoverflow.com/help/how-to-ask). – Ajoy Jul 24 '15 at 04:12
  • 2
    [A similar question](http://stackoverflow.com/q/9524873/1761793) – Ajoy Jul 24 '15 at 04: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! – Kane Blueriver Jul 24 '15 at 04:34
  • 2
    Have a look at [this](http://stackoverflow.com/q/19025336/1761793). – Ajoy Jul 24 '15 at 04:49
  • http://stackoverflow.com/questions/693070/how-can-you-find-unused-functions-in-python-code – Ciro Santilli OurBigBook.com Mar 27 '17 at 15:01
  • Possible duplicate of [Finding dead code in large python project](https://stackoverflow.com/questions/9524873/finding-dead-code-in-large-python-project) – 0 _ Oct 07 '17 at 20:55

3 Answers3

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.

NeoWang
  • 17,361
  • 24
  • 78
  • 126
  • 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. – Kane Blueriver Jul 24 '15 at 06:04
  • In that case, you need to analyze dead links, urls that are never visited. Python code analyzer cannot help you there. – NeoWang Jul 24 '15 at 08:47
  • 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. – Kane Blueriver Jul 24 '15 at 08:51
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).

François Constant
  • 5,531
  • 1
  • 33
  • 39
1

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.

Rahul Jha
  • 107
  • 1
  • 9