Sign in to follow this  

Tool that finds unused code.

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this