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

Patrick Smacchia [MVP C#]


Optimal Encapsulation

 

There is a very simple thing to do to rationalize a code base: making sure that every methods, fields and types have an optimal visibility. For example, if a method is declared internal but is not used outside its declaring type, it should be declared private. For such method the private visibility is its optimal one. Rationalizing a code base with optimal visibility help protecting from potential flaw. For example having optimal visibility can avoid improper coupling with a class that should have been declared internal as soon as it was developed.

 

Since the early days of NDepend, the tool was able to tell which code elements do not have optimal visibility. We now moved this feature into Code Query Language v1.5 with the following CQL conditions:

 

CouldBeInternal, CoulBeInternalProtected,  CouldBeProtected, CouldBePrivate and ShouldBePublic.

 

The default set of CQL constraints comes with a new Optimal Encapsulation group that contains constraints such as…

 

SELECT METHODS WHERE CouldBePrivate

 

…and that you can adapt to suit the needs of your own code base…

 

SELECT METHODS OUT OF NAMESPACES "MyNamespace1", "MyNamespace2" WHERE CouldBePrivate AND !NameLike "MyMethodsThatIDontWantToBePrivate"…

 

Of course, if your code base is a framework, there are chances that the query…

 

SELECT METHODS WHERE CouldBeInternal

 

…returns a lot of public methods that are not used by your code base, but that should remain public anyway because some external client code relies on it. In this case, just discard this query.

 

 

What about the condition ShouldBePublic? This condition matches types, methods and fields that are declared as internal but that are used by code declared in other assemblies thanks to the attribute System.Compilers.Services.InternalsVisibleToAttribute. In general, having code elements that are considered as ShouldBePublic is not a problem because it is made on purpose. However, it can be interesting to list them.

The condition CoulBeInternal can also apply to namespaces although .NET languages (C#, VB.NET etc) don’t allow visibility modifier on namespaces. A namespace is considered as CoulBeInternal if it contains only types that are used internally and if at least one of its types is declared as public.

 

SELECT NAMESPACES WHERE CouldBeInternal

 

While developing the optimal visibility feature, we notice an interesting thing. When you declare an internal class without  an explicit constructor, the default constructor automatically provided by the C# compiler is public. NDepend will tell you to declare explicitly an internal constructor but beware here: having a default internal constructor can sometime lead to serialization issues.

 



Comments

Jimmy Bogard said:

Wow this is weird, we were talking about this very thing in our M. Feathers book club today.  "Gee I wish there was a tool that would tell us if our visibilities were appropriate", then boom here it is.  Now to start the 5 month purchase approval process...

# October 31, 2007 4:30 PM

Tales from the SharpSide said:

Links from the Sharpside Super Spooky Halloween Edition

# October 31, 2007 10:20 PM

Ed Kirwan said:

Hi, Patrick,

The may seem a little odd, but I was playing around with some mathematics a while back and derived a methematical law proving optimum encapsulation for small family of systems. You may, or may not, fancy the read here:

www.edmundkirwan.com/.../intro.html

But if that's too much, the second law states that the encapsulation which minimises a family of systems' complexity is when the number of name spaces equals the square root of the number of classes divided by the number of public classes per name space.

Ed

# November 24, 2007 5:34 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:45 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!