开发者

Way to check redundant code in C# [duplicate]

This question already has an answer here: Detect duplicate code in Visual Studio 2010 (1 answer) Closed 4 years ago.

Is there any tool o开发者_开发知识库r 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜