Sunday, June 8, 2008

Evaluate your code by Code Analysis

We may write lines and lines of code but never check our code to see whether we follow up the best practice for coding. Sometimes, we make big mistakes in our codes it may not appear now but in future when change requests come. It reveals our code was not that much good we thought it was. So how can a programmer be confident about his code? The only way is that you need to review your code but in reality being overloaded we never have this chance. Nevertheless, we need code checking to be automatic and Code Analysis is a tool absolutely free with Visual studio .Net helps us to check our code in terms of security, design, maintainability, reliability and a lot more.

How to configure Code Analysis?

Open your project in visual studio then right click on your project and select properties. Then select code analysis tab. And you need to check "Enable Code Analysis". I highly recommend taking a look at different Rules that you can check. For example if you have different naming convention you can unselect that.

Then all you need in solution explorer right click on your project and select Run Code Analysis. It will show you some warning for your code.

Take a look at this picture

For simple code that I wrote code analysis could find some issues with my code. One of them says for enum value you need to have a zero value which is part of Microsoft best practice.


Nahid said...

I don't have 'Code Analysis' tab in my Visual Studio 2005, I think it should be checked during the installation, shouldn't it? But I checked it on Visual Studio 2008 and surprisingly got lots of warnings on my project!!!
Many Thanks

Anonymous said...

hi nahid,
you are right. you have this as long as you have VS team edition

Mehdi said...

سلام آقای یزدان پناه
چهار سال پیش در کلاس وب سی شارپ خیلی چیزها ازتون یاد گرفتم.
خوشحالم که وبلاگ شما را اینجا دیدم. من پارسال هم در دو تا از کلاس های موسسه آرینا شرکت کرده بودم و جویای احوال شما بودم