I am looking at some C/C++ code that appears to have functions that are defined, but never used. This is a pretty tedious process to trace through the code and verify. I’ve done some surveys and there are a number of tools that can do this type of analysis.
Can anyone share any experiences or tips on which tools and techniques are best or alternative methods of verifying this code is in fact not used?
1
There are some tools out there which can find ‘dead code’ in your programs. you can read about them on Stack Overflow threads here and here.
a small summary:
use the gcc compiler flags -Wunused and -Wunreachable-code, and then use a tool like lcov to find the unused methods.
1
Trace? Why? Just comment them out and run through the compiler. It will quickly tell you if you commented out some function which was referenced somewhere else.
3
You can use Cppcheck for this purpose:
$ cppcheck --enable=unusedFunction .
Checking foo.c...
1/2 files checked 0% done
Checking main.c...
2/2 files checked 0% done
[foo.c:1]: (style) The function 'foo' is never used.
If you are actively looking for unused code, use something Jake223 suggested.
But, if you just stumble upon some code that seems to be unused, you can just do a search for the method’s name in your codebase. If it’s a private method, your job is easy, search only the current class. If it’s a public method, search everything. If it’s a whole class, search for it’s name in all your code.
No hit? Perfect. Delete the code and run tests. You do have tests, don’t you? Than commit your changes to your document version system. You use one, don’t you? That way if you discover, ever, that you need it back, you just revert a change.
Deleting code should be something you do every day. Maybe you refactor some code and delete the old one. Or find old, unused code and delete it. And even if you don’t have backups, how hard it is to rewrite a function or two? And the second time you will certainly write them better than before.
2
My IDE of choice is Eclipse, and while it’s clunky and a bit of a pain to set up at first, it’s well worth the effort considering all of the tools I get. One such tool (and I’ve no idea what it’s official name is) informs you about unused code, such as classes, functions, variables, etc. The IDE simply displays a yellow line underneath the declaration. I’m not 100% sure if this works with multi-file projects, but you could always give it a shot!
Unless you’re looking for trouble (ie; you know they should be called) why bother. If they are genuinely uncalled, they will probably be removed by the linker, and in any case the wasted space is not significant. On the other hand, if you remove them and later find they were needed (perhaps in some compiler configuration you were not aware of) you will have more work to do to recover them. This is especially difficult if the removal and rediscovery are separated by a lot of time.
3
IDEs usually have this feature well implemented and it works by default (it can usually be disabled).
Whatever method you pick from the other answers to find dead code, always be aware of reflection issues. Some methods/fields may be accessed through reflection alone. Removing those methods will not trigger any alarm, except on runtime.
If you have (good) tests written for your projects, they will be of great value in this situation.
1