0

Testing tools, unitary or functional, are running certain portions of code. So we can define an idea of code coverage, which is the part of the code that is actually being tested by the test tooling.

But are there any test tool based features or plugins that can help you know if some parts of the code are never executed in the application, i.e. dead code?

3
  • You mean never executed under any circumstances (like when IDE reportrs that a variable is never used)? Because there are the parts of the code that might or might not be executed depending on the program input. – Alexey R. Feb 24 at 15:57
  • @AlexeyR. That's exactly why I think it could be a job for test tooling, and not an IDE. Writing every relevant test cases, running them, getting a report with the location of never executed code. – Alexis Dufrenoy Feb 24 at 17:21
  • 1
    I think such the tool is hardly possible taking into account that modern programming patterns imply decoupling of the code. Say you have a DI framework and you have several implementations of some interface in your code base. You will never know which of that implementation be used and which is not or probably both won't be because how they would couple would depend on the DI configuration of the particular run. However I might be mistaken. – Alexey R. Feb 24 at 17:30
2

You are looking for a dynamic, runtime, solution because like you wrote a static analyzer can only go so far.

A good solution, but not 100% accurate (*), could be mutation testing combined with a dynamic code coverage tool. Descartes: A Mutation Engine for PIT is such a solution but it is Java only and might not work well in high performance systems.

(*) What you will be doing is randomly exercising the system, there is a non zero chance that you will miss a state and it will be falsely declared as dead code.

0

When you say "dead code" you are meaning unreachable code or simply code not covered by your automated checks?

For the first case, your compiler usually already indicates such blocks of code. Most text editors do it on the fly.

For the second case, most code coverage analysis tools indicate uncovered code by highlighting it. Since most tools also provide machine-readable reports, you can scrap it out to report specifically uncovered code in a way that is better for you.

enter image description here

2
  • 1
    I mean unreachable code. I put a quick reminder of what code coverage is in my question, so I obviously know what it is, don't you think? And no, the IDE is not able to detect unreachable code beyond some obvious cases (never called methods, if statements containing an obvious false...). For example: you have an if statement on a value coming from a database. If that test is always false, the IDE won't be able to detect it. With a good test coverage, it should be possible to detect part of the code that are never used during any of the test. – Alexis Dufrenoy Feb 24 at 14:40
  • 1
    A DB is an external detail of your system, not part of your program itself. That's why technically the aforementioned case is not in the scope of static unreachable code detection. It would be the case for dynamic code coverage. There are tools, like PyWatch, that support running your automated checks at every file change thus giving you the feedback about uncovered blocks of code. You can then use this information in your text editor (Vim & Emacs have plugins for that). What context/stack are you in? Most major programming languages already have support for code coverage reporting. – João Farias Feb 24 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.