1

EDIT: Apparently what I'm talking about is not called a tombstone. I got that term from this video: https://www.youtube.com/watch?v=29UXzfQWOhQ

I'm working on a complex django webapp, and recently I started to think about adding tombstones to detect old and unused code.

Has there been a python library for creating and managing tombstones? If not, what suggestions do you have for me on building such library?

megapctr
  • 931
  • 1
  • 7
  • 19
  • Sounds like you should be looking for a code coverage tool to detect unused code. The idea of tombstones is to detect access to resources after they have been disposed of, not to find unused code. – Matthew Strawbridge Jun 28 '14 at 09:07
  • 1
    "tombstone" is not such a well-known word, perhaps you could provide a link describing what you mean? – Ned Batchelder Jun 28 '14 at 11:00
  • Right, sorry. I'm updating the question now. – megapctr Jun 28 '14 at 11:15
  • Some IDEs can detect unused variables and imports. Proper versioning and using a repository usually seems helpful to me in these matters! – user2963623 Jun 28 '14 at 11:28

2 Answers2

1

It sounds like a dead code problem. You've got two options:

Code coverage tools

This involves using a tool that can detect when a particular piece of code is actually run. This is known as code instrumentation.

For this to work, you must run the code through an extensive set of tests, ensuring that every part of the code that could be run in a real deployment scenario is actually run. The code coverage tool will then tell you what's "left over".

Coverage.py is one I've used before.

The problem here is that if you don't already have that extensive test suite, you're going to have to right one.

Static Analysis

Static analysis involves inspecting of the source code in an attempt to infer runtime problems that might occur.

The issue with static analysis in a dynamic language like python, that it is very hard to determine what code is being run/used just from the source code. Some things are easy to spot statically (undefined variables for example, although there are caveats to even that) but some are less so. Take the following example:

class A:
    value = 1
class B:
    value = 2

l = [A(), B()]

print l[0].value

Without actually running the code, the static analyser is going to have a hard time working out that B.value is never used, and impossible is the list index came from user input.

In python, there are not many options for static analysis, but there are one or two tools out there. Vulture is one example, but as is say, it's not going to be very correct.

Jamie Cockburn
  • 7,379
  • 1
  • 24
  • 37
-1

I recommend Pycharm Community Edition: http://www.jetbrains.com/pycharm/download/
It's free and powerfull IDE with code inspections and much more.

szymanskilukasz
  • 463
  • 3
  • 13