Finding "dead code" in a large C++ legacy application

C++Visual C++Legacy

C++ Problem Overview


I'm currently working on a large and old C++ application that has had many developers before me. There is a lot of "dead code" in the project, classes and functions that aren't used by anyone anymore.

What tools are available for C++ to make a analysis of large code base to detect and refactor dead code? Note: I'm not talking about test coverage tool like gcov.

How do you find dead code in your project?

C++ Solutions


Solution 1 - C++

You'll want to use a static analysis tool

The main gotcha I've run into is that you have to be careful that any libraries aren't used from somewhere that you don't control/have. If you delete a function from a class that gets used by referencing a library in your project you can break something that you didn't know used the code.

Solution 2 - C++

You can use Cppcheck for this purpose:

$ cppcheck --enable=unusedFunction .
Checking 2380153.c...
1/2 files checked 0% done
Checking main.c...
2/2 files checked 0% done
[2380153.c:1]: (style) The function '2380153' is never used.

Solution 3 - C++

Caolán McNamara's callcatcher is very effectively used within the LibreOffice project (~6 MLOC) to find dead code.

Solution 4 - C++

I think your best bet would probably be a coverage tool. There're plenty for both *nix and windows. If you have unit tests, it's easy - if you have a low test coverage, then the uncovered code is either dead or not tested yet (you want both pieces of this info anyway). If you don't have unit tests, build your app with instrumentation provided by one of those tools, run it through some (should be all ideally) execution paths, and see the report. You get the same info as with unit tests, it will only require a lot more work.

Since you're using VisualStudio, I could provide you couple of links which you could consider using:

Neither of them is free, not even cheap, but the outcome is usually worth it.

On *nix-like platforms gcov coupled with tools like zcov or lcov is a really great choice.

Solution 5 - C++

One approach is to use "Find All References" context menu item on class and function names. If a class/function is only referenced in itself, it is almost certainly dead code.

Another approach, based on the same idea, is to remove(comment out) files/functions from project and see what error messages you will get.

Solution 6 - C++

Nothing beats familiarity with the code. Except perhaps rigourous pruning as one goes along.

Sometimes what looks like deadwood is used as scaffolding for unit tests etc, or it appears to be alive simply because legacy unit tests exercise it, but it is never exercised outside of the tests. A short while ago I removed over 1000 LOC which was supporting external CAD model translators, we had tests invoking those external translators but those translators had been unsupported for 8+ years and there was no way that a user of the application even if they wanted to could ever invoke them.

Unless one is rigourous in getting rid of the dead wood, one will find your team maintaining the stuff for years.

Solution 7 - C++

See our http://www.semanticdesigns.com/Products/TestCoverage/CppTestCoverage.html">SD C++ Test Coverage.

You need to do a lot of dynamic testing to exercise the code, to make sure you hit the maximum amount of coverage. Code "not covered" may or may not be dead; perhaps you simply didn't have a test case to exercise it.

Solution 8 - C++

Although not specifically for dead code, I found the Source Navigator

<http://sourcenav.berlios.de/>

quite useful, although cumbersome to set up and a bit buggy. That was a year ago on Linux (Fedora).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongudnithorView Question on Stackoverflow
Solution 1 - C++Alan JacksonView Answer on Stackoverflow
Solution 2 - C++user6307369View Answer on Stackoverflow
Solution 3 - C++phwView Answer on Stackoverflow
Solution 4 - C++DmitryView Answer on Stackoverflow
Solution 5 - C++hongliangView Answer on Stackoverflow
Solution 6 - C++lilburneView Answer on Stackoverflow
Solution 7 - C++Ira BaxterView Answer on Stackoverflow
Solution 8 - C++SchwanritterView Answer on Stackoverflow