Is there a quick way to detect classes in my application that are never used? I have just taken over a project and I am trying to do some cleanup.
I do have ReSharper if that helps.
I don't recommend deleting old code on a new-to-you project. That's really asking for trouble. In the best case, it might tidy things up for you, but isn't likely to help the compiler or your customer much. In all but the best case, something will break.
That said, I realize it doesn't really answer your question. For that, I point you to this related question:
Is there a custom FxCop rule that will detect unused PUBLIC methods?
NDepend
Resharper 4.5 (4.0 merely detects unused private members)
Build your own code quality unit-tests with Mono.Cecil (some samples could be found in the Lokad.Quality the this open source project)
Review the code carefully before you do this. Check for any uses of reflection, as classes can be loaded and methods can be dynamically invoked at runtime without knowing at compile time which ones they are.
It seems that this is one of the features proposed features for the next version of Resharper. That doesn't help yet, but hopefully the EAP is just around the corner.
Be careful with this - it is possible that you may remove things that are not needed in the immediate vicinity of the code you are working on but you run the risk of deleting interface members that other applications may rely on without your knowledge.
Related
I am using a huge open source library in C#, but I'm only using it for a handful of specific functions. Is there a tool that I can use to automatically clean out all unused code?
I tried going about it manually using resharper but the code is so deeply nested that it is impossible...
Try something like this: include a project containing code that calls the methods you need. Maybe this should be a unit test project. Use solution-wide analysis to remove code that is completely unused.
Then start making the public classes private. Revert this as soon as you "see red".
Also be sure to remove unused references.
If you have Ultimate, you can use a dependency diagram to give you. Visual clue about what's not used.
As far as I know there is no simple way to do this. In order to isolate specific methods you'll need to understand all of their dependencies and make sure those remain intact. From what you've said - which isn't enough for me to really know, this is pure speculation - you're going to have to take a lot or take nothing at all.
As recommended by DJ KRAZE, if you really only want a small subset of the functionality, make it yourself.
This question already has an answer here:
Detect duplicate code in Visual Studio 2010
(1 answer)
Closed 5 years ago.
Is there any tool or way I can check how can I optimize my code? removing redundancy? I am using VS 2010
Thanx
I don't know about removing redundancy, but ReSharper has some nice code analysis features that can help to identify unused code blocks. It can also make suggestions for cleaner code, but it's not always 100% accurate.
Such tools, even if they existed, wouldn't be reliable. The best would be to perform a code review by a good developer or architect.
No tool can replace experience and expertise. There are a number of productivity tools that can help, a popular one being ReSharper for example, but it's not going to fix everything for you. At some point you just have to rely on your abilities and the abilities of your team members. Learning how to code well takes time.
It often helps to step back and look at your code with the mindset of certain design principles. S.O.L.I.D. can be a great place to start. Some other questions you can ask yourself are:
Are your classes and types properly encapsulated?
Is your code test-driven or behavior-driven in any way?
Do your tests define discrete unit of behavior, or are they just tailored to the implementation that's being tested?
To specifically address redundancy, quite simply, do you have copied/pasted code doing the same thing in two places?
A profiler will give you a good idea of where your application spends most of its time. From knowing what to how to optimize, though, requires experience as well as knowledge of both the code base in general and the problem domain.
What you want is Code Coverage tools. These keep a record of which lines of code are executing. In order for this to be effective, a complete test suite, or manual test run, is required. This will show up the lines of code that are never used, and will help you make decisions.
Static analysis can also help you with code paths and give you information about how and where your code is called.
A couple of good question sabout code coverage:
What can I use for good quality Code Coverage for C#/.NET?
C# Code Coverage metrics
Also look at Microsoft's FxCop for static analysis:
http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx
There is http://clonedetectivevs.codeplex.com/ which is a VS plugin. It uses http://conqat.cs.tum.edu/ under the hood. I've not really used it but does what you asked. Couple that with code reviews and might help.
There is a (commercial, 249€) solution checking for duplicate code, even in large projects.
http://www.solidsourceit.com/products/SolidSDD-code-duplication-cloning-analysis.html
For that purpose we use the build-in Duplicated Finder in our TeamCity build server.
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.
ReSharper does the job.
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.
Eclipse does it automatically for Java, not sure if you can have same thing for C#.
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.
I believe lots of us have been presented with a challenge to check manually through hundreds of string constants to check which ones are used and which are not (orphans). Although I do not have that many yet in my current project, I still find it difficult already to do a code review and check for dropped constants manually.
Is there any way to automate this? Maybe some secret hidden feature in VS?
I have an ASP.NET MVC project with VS 2008. So I'd like to check for all possible references to resources in both .cs and .aspx files.
My guess is that would not be possible. Still, maybe there is something to help me with this task?
CodeRush from Developer Express provides all sorts of orphan checks. They just released a free version - CodeRush Express - although I'm not entirely sure what features are in the free version. Would seem plausible that a feature like this would have been left in, though.
It's a pretty powerful tool, so I'd recommend it anyway.
Also Resharper is the best tool for VS IDE,it does all that for you plus many other options.
hope this helps.
NDepend supports searching for unused public methods / properties etc. Also, Resharper 4.5 now highlights all unused non-private members if you turn on solution wide analysis.
recently I was given the task to discover a C# solution I have never seen before, and give suggestions on refactoring it. I think I will use NDepend (for the first time ever) to see the overall picture, and also to check a lot of code metrics to figure out what could be refactored. NDepend is pretty good at showing the structure of a project, I think.
My question is a more general one: what do you think is the best way to discover code that you are seeing the first time, and need to understand it's structure?
(Unfortunately there is no logical design documentation and the code is poorly commented.)
Code Discovery is much more easy with NDepend. This tool provides a top-down approach on dependencies and layering between assemblies, namespaces and classes. This is done with some graph and depednencies matrix generated from the code.
You'll also get dependencies on tier code assemblies, which is really useful to know which part of the code does what.
Also, some graphical representation of volume metrics such as # lines of code helps a lot to get a clear idea of where the effort where done on the code.
I frequently use Reflector to study third-party assemblies as well as .NET assemblies. Not so much for a bird's-eye view of the relations between classes, but a more for close-up detail of exactly what is going on.