Patrick Smacchia [MVP C#]

Sponsors

The Lounge

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Ensure the quality of the code that will be developed this year

 

Do a favor to yourself and your team: Make sure that the code you will develop in 2008 will abide by common quality principles.

 

The classic blame on quality tools I hear again and again is: When the tool analyzes my code base, it yields literally thousands of warnings. I don’t have the time to go through all these problems.

 

Actually, this blame is similar to the one that is made against automatic tests. I don’t have the time to stop my development for months just to code unit tests on existing code. There is one simple answer to this situation apparently widely accepted amongst the agile experts: from now, make sure that all the code you are doing or refactoring will be unit tested.

 

I propose the same answer for code quality: from now, make sure that all the code you are doing or refactoring will abide by common quality principles. And now comes the tricky part: how can I specify to my quality tool which part of my code base is new or has been refactored?

 

NDepend and the language CQL are able to compare 2 versions of a code base. The idea is then to regularly check that the quality will be respected on the code that has been changed since today. Concretely, you just have to write:

 

WARN IF Count> 0 IN SELECT METHODS WHERE
(
CodeWasChanged OR WasAdded) AND CyclomaticComplexity > 15

 

The conditions CodeWasChanged OR WasAdded can be understood as Code that has been refactored or new code.

 

The NDepend project can be easily configured to define with which previous analysis the comparison will be made against. The following screenshot shows how to specify a particular analysis, made on the 27th March 2007. You can also compare against an analysis made N days ago or the last analysis done.

 

 

 

 

 

 

Here are 10 essential quality rules I would suggest. The great news is that this tips can be readily applied to any CQL rules!

 

// <Name>Methods too complex (CyclomaticComplexity)</Name>
// A method should not be too big.

WARN
IF Count> 0 IN SELECT METHODS WHERE
(
CodeWasChanged OR WasAdded) AND NbLinesOfCode > 25

 

 

// <Name>Methods too complex (CyclomaticComplexity)</Name>
// A method should not be too complex.

WARN IF Count> 0 IN SELECT METHODS WHERE
(
CodeWasChanged OR WasAdded) AND CyclomaticComplexity > 15

 

 

// <Name>Methods poorly commented</Name>
// A method with at least 10 lines should be commented.

WARN IF Count> 0 IN SELECT METHODS WHERE
(
CodeWasChanged OR WasAdded) AND
PercentageComment < 20 AND NbLinesOfCode > 10      

 

 

// <Name>Methods with too many parameters</Name>
// Methods with many parameters are hard to maintain and are bad for perf.

WARN IF Count> 0 IN SELECT METHODS WHERE
(
CodeWasChanged OR WasAdded) AND NbParameters > 5     

 

 

// <Name>Methods with too many local variables</Name>
// Methods with many local variables are hard to maintain.

WARN IF Count> 0 IN SELECT METHODS WHERE
(
CodeWasChanged OR WasAdded) AND NbVariables > 7    

 

 

// <Name>Inheritance tree too deep</Name>
// Branches too long in the derivation should be avoided.

WARN IF Count > 0 IN TYPES WHERE
(
CodeWasChanged OR WasAdded) AND DepthOfInheritance > 5    

 

 

// <Name>Types with too many fields</Name>
// Types with too many fields should be avoided.

WARN IF Count > 0 IN TYPES WHERE
(
CodeWasChanged OR WasAdded) AND NbFields > 10

 

 

// <Name>Methods that use boxing</Name>
// Boxing\Unboxing should be avoided for performance reasons.

WARN IF Count> 0 IN SELECT TYPES WHERE
(
CodeWasChanged OR WasAdded) AND
(
IsUsingBoxing OR IsUsingUnboxing)

 

 

// <Name>Methods that could be better encapsulated</Name>
WARN IF Count> 0 IN SELECT METHODS WHERE
(
CodeWasChanged OR WasAdded) AND
(
CouldBePrivate OR CouldBeInternal OR CouldBeProtected OR CouldBeInternalProtected)

 

 

// <Name>Types that could be better encapsulated</Name>
WARN IF Count> 0 IN SELECT TYPES WHERE
(
CodeWasChanged OR WasAdded) AND
(
CouldBePrivate OR CouldBeInternal OR CouldBeProtected OR CouldBeInternalProtected)

 

 


Posted 01-01-2008 1:38 PM by Patrick Smacchia

[Advertisement]

Comments

Trumpi's blog wrote Our daily link (2008-01-01)
on 01-01-2008 2:39 PM

Happy new year! And no, I didn&#39;t get an MVP award. Podcasts .NET Rocks! #303 - Ken Levy on Visual

» Daily Bits - January 2, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats. wrote &raquo; Daily Bits - January 2, 2008 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.
on 01-02-2008 8:25 AM

Pingback from  &raquo; Daily Bits - January 2, 2008 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.

Patrick Smacchia [MVP C#] wrote Avoid API breaking changes
on 01-20-2008 6:33 AM

If you are developing a framework, the last thing you want to happen when releasing a new version of

Patrick Smacchia [MVP C#] wrote Write Active Conventions on your Code Base
on 05-11-2008 4:53 PM

Recently, both Glenn Block and Ayende wrote about how to define some sort of active conventions about

Community Blogs wrote Write Active Conventions on your Code Base
on 05-12-2008 3:45 AM

Recently, both Glenn Block and Ayende wrote about how to define some sort of active conventions about

Patrick Smacchia [MVP C#] wrote Getting rid of spaghetti code in the real-world: a Case Study
on 09-23-2008 12:42 PM

A part of my job as lead developer of the tool NDepend , is to make sure that the product fits real-world

Community Blogs wrote Getting rid of spaghetti code in the real-world: a Case Study
on 09-23-2008 1:19 PM

A part of my job as lead developer of the tool NDepend , is to make sure that the product fits real-world

Mirrored Blogs wrote Getting rid of spaghetti code in the real-world: a Case Study
on 09-23-2008 1:27 PM

A part of my job as lead developer of the tool NDepend , is to make sure that the product fits real-world

Patrick Smacchia [MVP C#] wrote Development Psychology, Technical debt and The next feature syndrome
on 02-28-2009 9:46 PM

I would like to describe here a harmful scenario I have seen in many different occasions. Typically,

Community Blogs wrote Development Psychology, Technical debt and The next feature syndrome
on 02-28-2009 10:28 PM

I would like to describe here a harmful scenario I have seen in many different occasions. Typically,

Patrick Smacchia [MVP C#] wrote My 100th blog post: Top 5 development practices you should care for
on 03-25-2009 3:48 AM

This is a modest number but I am happy to have reached it, especially taking account that I spend weekly

Community Blogs wrote My 100th blog post: Top 5 development practices you should care for
on 03-25-2009 3:59 AM

This is a modest number but I am happy to have reached it, especially taking account that I spend weekly

Add a Comment

(required)  
(optional)
(required)  
Remember Me?