CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Patrick Smacchia [MVP C#]


Keep your code structure clean

 

As an architect or a lead developer responsible for the structure of a code base, you spend time creating a clean structure for your code. The challenge is to make sure that the intentions you had when creating the structure won't be violated in the future, what is often named design erosion or architecture erosion.

 

Let’s take a real-world application such as Octopus Micro Finance Suite, an open-source application developed by the Octo Technologies company. Typically, in such a 3 tiered application, the UI code shouldn’t access directly the database. Concretely, the code of the assembly Octopus.GUI shouldn’t use directly the class Octopus.Shared.DatabaseHelper.

 

As shown in the screenshot below, with NDepend you just have to right-click the cell on the Dependency Structure Matrix that represents the dependency that is forbidden, and select Generate a CQL constraints that warns if this dependency exists. The CQL rule generated basically says:

 

I’ll warn if there is an assembly named Octopus.GUI that is using directly a type named Octopus.Shared.DatabaseHelper.

 

 

As in every deny system, there is also a possibility to have some permit only CQL rules. Maybe, the architects of the Octopus project want to make sure that the assembly System.Drawing is only used by the assembly Octopus.GUI. Here, the CQL rule says that:

 

I’ll warn if there is an assembly that is not named Octopus.GUI and that is using directly an assembly named System.Drawing.

 

 

The CQL language presents some smarter usage conditions to write smarter constraints. For example, the architects of the Octopus project might want to make sure that no SQL connections are created outside the type System.Data.SqlClient.SqlConnection. Here, the CQL rule says that:

 

I’ll warn if there is a method declared outside the type Octopus.Shared.ConnectionManager that is creating an instance of the class System.Data.SqlClient.SqlConnection.

 


 

Another usage conditions is the derivation.  The following CQL rules says that:

 

I’ll warn if there is a class declared outside the assembly Octopus.GUI that derives from System.Windows.Forms.Form.

 

In the same spirit, CQL supports the Implements condition to constraint which classes implements a certain interface.

 

Finally, another very useful usage condition is the field writing one. As shown in a previous post, you might want to ensure that the only way a field is written is by calling its corresponding property setter. Doing so, it is easier to maintain and debug the field state, you don't have to put breakpoind everywhere the field is assigned.

 


 

CQL rules are stored inside the NDepend project file. You can also store CQL rules directly in the source code as shown here. All rules will be verified each time NDepend will analyze the project and if some rules are violated, they are enumerated in the report built during the analysis.

 

All resemblance with the way how xUnit tests tools are working is intended. xUnit tests test the code to prevent correctness regression. CQL rules test the structure to prevent architecture erosion.

 



Comments

Jeremy D. Miller said:

This is cool Patrick.  I'm honestly still using the old OSS version of NDepend just for CC numbers, but now you've got me thinking about getting this applied and into the build for my next project.  

# November 26, 2007 6:05 PM

Christopher Steen said:

MSBuild MSBuild Task for SharePoint - MakeCab [Via: Steve ] ASP.NET Ajax with the ASP.NET MVC Framework...

# November 27, 2007 12:16 AM

Christopher Steen said:

Link Listing - November 26, 2007

# November 27, 2007 12:16 AM

karl said:

I agree this really is cool.  I have to admit, I find NDepend a little too intimidating to use - and I bet I'm not the only one. Like Jeremy I've used the OSS version for various metrics, but haven't taken the plunge to the new version. (a) where do you recommend starting  (is there a simple sample project available with a detailed explanation of the ndepends output, that would be awesome?) and Beer do you feel that there are opportunities for you to improve the tool to reach a wider audience - specifically with respect to ease of use?  Could be some good blog posts :)

# November 28, 2007 11:20 AM

Daily Links - November 28, 2007 | GrantPalin.com said:

Pingback from  Daily Links - November 28, 2007 | GrantPalin.com

# November 28, 2007 11:21 AM

Patrick Smacchia said:

Karl, we are currently working hard to improve the UI ergonomy and make the tool usability very close to what VisualStudio propose. Actually, it is our primary goal to reach a wider audience with more ergonomy and documentation. The release v2.6 with the improved UI should be available mid dec.

Basically we are adding a start page, a project properties and a error list panels to VisualNDepend.

The current version already comes with a help panel that explains how to get started with the most common scenarios. It is still perfectible and all feedbacks are highly appreciated.

# November 28, 2007 11:32 AM

From the software development trenches said:

Time for another weekly round-up of developer news that focuses on .NET, agile and general development

# November 30, 2007 12:48 AM

Patrick Smacchia [MVP C#] said:

In my previous post Keep your code structure clean I explained how to build Code Query Language (CQL

# December 2, 2007 3:35 PM

Grant Palin said:

Very neat.  Looks to be a useful project management tool.  Do you use the paid version of the software?  Or is the trial "good enough"?

# December 4, 2007 1:57 PM

Buy And Download Music » Blog Archive » Code and Structure-Liberation (CHOKE018) said:

Pingback from  Buy And Download Music  » Blog Archive   » Code and Structure-Liberation (CHOKE018)

# January 13, 2008 10:36 AM

TechMount » Archive » Daily Friction #308 said:

Pingback from  TechMount  » Archive   » Daily Friction #308

# February 26, 2008 11:16 AM

Patrick Smacchia [MVP C#] said:

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

# May 11, 2008 4:53 PM

Community Blogs said:

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

# May 12, 2008 3:44 AM

Jersson on Geeks·ms said:

Antes de apagar la máquina (o me corten el internet pues dicen que debo dormir, lo cual no comprendo

# May 16, 2008 3:34 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Patrick Smacchia

Patrick Smacchia is a Visual C# MVP involved in software development for over 15 years. After graduating in mathematics and computer science, he has worked on software in a variety of fields including stock exchange, airline ticket reservation system as well as a satellite base station at Alcatel. He's currently a software consultant and trainer on .NET technologies as well as the lead developer of the tool NDepend which provides numerous metrics and caveats on any compiled .NET application. He is the author of Practical .NET2 and C#2, a .NET book conceived from real world experience with 647 compilable code listings. Check out Devlicio.us!