Tool that finds unused code.

Started by
9 comments, last by Rydinare 15 years, 10 months ago
Hi. I'm currently sitting on a fairly large code base, about 40,000 lines of code. The code has been built upon for a while and there are some functions in here that are not used anymore. Rather then combing through the code manually, Is there a tool out there that can find functions, classes, and variables that are no longer referenced anywhere else. Essentially obsolete code that was not deleted way back when. I know code like this is frowned upon. Apparently, maintenance of code is not a big priority here. Also, out of curiosity, would a compiler automatically remove code that is not longer used anywhere else? I don't really know where to put this post so if a moderator feels that this doesn't belong in here, could you please move it? Thanks in advance.
------------Anything prior to 9am should be illegal.
Advertisement
Tried profiling the code using gprof? See what functions get called (and what classes through constructors) and pare out anything that's not shown. Make sure you run through all possible test cases, though.

If that doesn't help, then I'm out of ideas. :P
You can use code analysis tools (i.e. PC-Lint, commercial) that will analyze your code and give feedback on things like that and general badness in your code. Although those tools do cost a fair chunk of change. I find the best way (at least for my projects) is to turn up the warning levels to the highest setting which will warn against uninitialized/referenced variables, etc...If you're using Visual Studio, you could always parse the project (it's XML-ish) diff'ing files in the project versus the tree for inconsistencies. Hope this helps.
I'm more concerned about free-floating functions that are in my code, just not referenced. I combed through the warnings of uninitialized/unused variables and got rid of them. I think all classes are being used though, so its just a matter of finding these stray functions.
------------Anything prior to 9am should be illegal.
Depending on your linker you may have a linker option to discard unused function definitions. For example, most recent versions of MSVC have this functionality with the /OPT:REF switch.
You can also do a good old edit->find and see if your functions are actually referenced anywhere (not the most efficient solution, but at least free), or even write your own program that basically does just that (fishes function declarations and sees if they are called anywhere).
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Understand for C++ $495

Graphviz CPL

C/C++ Depedency Graphing Perl

Dependency-Grapher for C++-Projects Visual Studio .NET Addins

The Netron diagramming library GPL

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
What I'd suggest doing is compiling and linking the program inn release mode (but without function inlining) and generating a map file. Any functions that aren't mentioned in the map file will have been optimized away by the compiler / linker as unused. If you're lucky the compiler will have an option to tell you what it optimized away.

If you're using gcc look here for the parameters you need to use for it to tell you what's unused.
Most tools I've used that have done this have been on embedded systems, so they're probably not of much use.

It brings me to my word of caution, though. Unused code isn't necessarily bad. If it's reusable and has the potential of being used in the future, wrap it up nicely in a library, hopefully put it in a test harness, and keep it around for the next time you need it. Of course, if it's code that duplicates another way that things are now done, and the unused portion is inferior and unnecessary, by all means, it's probably time for it to die. [smile]
Quote:Original post by Rydinare
It brings me to my word of caution, though. Unused code isn't necessarily bad. If it's reusable and has the potential of being used in the future, wrap it up nicely in a library


The thing is that we did a major refactor on the code, that spanned almost a year. so the functionality that was once there is old and useless. Its just burried.

ALl these examples are nice but are part of larger applications that do graphing of dependencies. Is there just a straight tool that only shows unused code?
------------Anything prior to 9am should be illegal.

This topic is closed to new replies.

Advertisement