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
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 breakpoint everywhere the field is assigned.


Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

WARN IF Count > 0 IN SELECT METHODS WHERE

  IsDirectlyWritingField  "Octopus.CoreDomain.ClientGroup.leader" AND

  !FullNameIs "Octopus.CoreDomain.ClientGroup.set_Leader(Person)"

 

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.

 


Posted 11-26-2007 8:16 PM by Patrick Smacchia

[Advertisement]

Comments

Jeremy D. Miller wrote re: Keep your code structure clean
on 11-26-2007 6:05 PM

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.  

Christopher Steen wrote Link Listing - November 26, 2007
on 11-27-2007 12:16 AM

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

Christopher Steen wrote Link Listing - November 26, 2007
on 11-27-2007 12:16 AM

Link Listing - November 26, 2007

karl wrote re: Keep your code structure clean
on 11-28-2007 11:20 AM

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 :)

Daily Links - November 28, 2007 | GrantPalin.com wrote Daily Links - November 28, 2007 | GrantPalin.com
on 11-28-2007 11:21 AM

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

Patrick Smacchia wrote re: Keep your code structure clean
on 11-28-2007 11:32 AM

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.

on 11-30-2007 12:48 AM

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

Patrick Smacchia [MVP C#] wrote Deconstructing Software
on 12-02-2007 3:35 PM

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

Grant Palin wrote re: Keep your code structure clean
on 12-04-2007 1:57 PM

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"?

Buy And Download Music » Blog Archive » Code and Structure-Liberation (CHOKE018) wrote Buy And Download Music » Blog Archive » Code and Structure-Liberation (CHOKE018)
on 01-13-2008 10:36 AM

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

TechMount » Archive » Daily Friction #308 wrote TechMount » Archive » Daily Friction #308
on 02-26-2008 11:16 AM

Pingback from  TechMount  » Archive   » Daily Friction #308

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:44 AM

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

Jersson on Geeks·ms wrote WF Designer + Performance Tool + VB Tips + Todo!
on 05-16-2008 3:34 AM

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

Add a Comment

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