views:

149

answers:

4

Are there any tools that can find any private functions without any references? (Redundant functions)

Reason being, that a function may have been created and called from a couple of areas, but as the project expands and grows, these two calls may have been removed and swapped with a better alternative. But the method may still remain. I was wondering if there were any handy tools that would look through the code, spotting private functions and checking if they have any references, if not, inform the user of the situation.

It wouldn't be too tricky to create one myself, but I was wondering if there were any accessible apps that could do this with the files containing the code?

My code is in c#, but I can imagine that this question covers a variety of coding languages.

Thanks

+2  A: 

If your code has Unit Tests (it does, right? ;-) then running NCover will allow you to identify methods that aren't being called from anywhere. If you haven't got any unit tests then it's a good excuse to use for starting to build them.

In the general case, I'd suspect that code coverage tools are a good fit in most languages.

Rob
Would agree with you on this, but I also have recently started on a project with 0 tests, but also wish to remove such 'dead code'. Doing so would reduce the amount of code I have to start writing tests for!
MPritch
@Martin - No tests; ouchies! If you start writing unit tests for public methods that'll probably make the private methods that aren't called a small enough set that you can use Visual Studio's Search to check for them being called. That said, I know *exactly* where you're coming from with this one! :)
Rob
I have no Unit tests in my project either, could be because it's SharePoint though :-D.
Colin
Downside is that if the private method/function is a result of some earlier cycle of behavior-/test-driven development, there may be a test covering the now unused private method through another method which isn't private or is a corner case for example a null check which in the software's current incarnation will never be executed. Just looking at code coverage won't help you there.
Esko
@Esko - good point! =)
Rob
A: 

Eclipse does it automatically for Java, not sure if you can have same thing for C#.

Ivan
A: 

Another question might even be "Does the c# compiler remove private methods that aren't actually used?".

My guess would be no, but you never know!

EDIT:

Actually, I think it might be hard to tell where a method is used. It might be private, but it can still be used as Event Handlers. Not impossible to check, but I'm sure that aspect would make it a little more difficult.

SkippyFire
It'd be a "bad thing"(tm) for the compiler to remove methods that aren't used, private or otherwise, as they can still be accessed and called via Reflection. In fact, I believe that the VS Unit Test framework generates code that does exactly that to allow you to unit test private/internal methods.
Rob
+3  A: 

ReSharper does the job.

The Chairman
Would be helpful if you tell people how
MPritch
ReSharper marks any private function that is not called:* Yellow mark at the right side of the VS code window.* Method name greyed.* Red light bulb if cursor is on function name.ALT + RETURN offers "Remove unused method". That's it
The Chairman