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
Avoid API breaking changes
 

If you are developing a framework, the last thing you want to happen when releasing a new version of your product is to break the code of your clients because of an API change. For example, you want to make sure that all public methods you had in previous versions are here in the next version, unless you tagged some of them with System.ObsoleteAttribute.

There is one major company in the .NET sphere that publishes a big framework and that has this need: Microsoft. To make sure that the .NET framework public API doesn’t contain breaking changes, Microsoft developed the tool LibCheck that detects automatically API breaking change.

The tool NDepend can also be used to detect API breaking changes thanks to some astute CQL rules. The following CQL rule warns if a public method of the previous version is not public anymore or has been removed in the new version.

WARN IF Count > 0 IN SELECT METHODS WHERE
IsInOlderBuild AND IsPublic AND (VisibilityWasChanged OR WasRemoved)

 

The condition IsInOlderBuild needs a little explanation. When comparing 2 versions of a code base, NDepend stores in memory the 2 code base structures, the older one and the newer one. This is why the comparison is immediate, everything is in-memory. Also, in this context, NDepend has to choose on which structure the query will be run. The condition IsInOlderBuild forces NDepend to run the query on the older build. Actually we don’t need the condition IsInOlderBuild here because when a query contains the condition WasRemoved, NDepend automatically runs the query against the older build. However, I estimate that adding IsInOlderBuild makes the query easier to read and understand.

The query can be read: Warn if there are methods that used to be public and for which visibility was changed or have been removed. Interestingly enough, we notice that if the visibility used to be public and has changed, it means that the visibility is not public anymore, hence the API breaking issue. This is also a good example of mixing ortogonal CQL features (here comparison and visibility) in the same query to obtain smarter queries. I will blog more on this in the future, this will be an important direction for the product in 2008.


Eventually, we could also add the CQL condition IsObsolete to make sure that the query doesn’t match obsolete methods that have been removed.

WARN IF Count > 0 IN SELECT METHODS WHERE
IsInOlderBuild AND IsPublic AND !IsObsolete AND (VisibilityWasChanged OR WasRemoved)


In the same spirit, it is easy to write the following CQL rule that detects public types of the previous version that are not public anymore or that has been removed in the new version:

WARN IF Count > 0 IN SELECT TYPES WHERE
IsInOlderBuild AND IsPublic AND (VisibilityWasChanged OR WasRemoved)
 

Notice that in this previous post I explained how to configure an NDepend project to define the previous version of your code base to compare with during an analysis. Basically you can choose between:

  • Compare with the last analysis available.
  • Compare with the analysis made N days ago.
  • Compare with the particular analysis made on MM/DD/YYYY hh:mm 

Of course, you can also still use the VisualNDepend GUI to compare any 2 previous analysis.



Posted Sun, Jan 20 2008 12:11 PM by Patrick Smacchia
Filed under:

[Advertisement]

Comments

» Daily Bits - January 20, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats. wrote » Daily Bits - January 20, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.
on Sun, Jan 20 2008 10:09 AM

Pingback from  » Daily Bits - January 20, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.

Christopher Steen wrote Link Listing - January 20, 2008
on Mon, Jan 21 2008 12:09 AM

Link Listing - January 20, 2008

Christopher Steen wrote Link Listing - January 20, 2008
on Mon, Jan 21 2008 12:09 AM

ASP.NET Forms Authentication and path= in the Tag [Via: Rick Strahl ] Sharepoint WSS List DataSource...

Bernardo Heynemann wrote re: Avoid API breaking changes
on Tue, Jan 22 2008 1:59 PM

Hi Patrick,

We have a plug-in in StormwindProject that does just that.

You can check more details in www.stormwindproject.org/index.php/CCNet-Plugins/Home.html">www.stormwindproject.org/.../Home.html.

Hope it helps!

Bernardo Heynemann

Stormwind Project Committer

www.stormwindproject.org

Reflective Perspective - Chris Alcock » The Morning Brew #18 wrote Reflective Perspective - Chris Alcock » The Morning Brew #18
on Fri, Jan 25 2008 2:44 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #18

DotNetKicks.com wrote Avoid API breaking changes
on Wed, Mar 26 2008 10:53 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Patrick Smacchia [MVP C#] wrote Write Active Conventions on your Code Base
on Sun, May 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 Mon, May 12 2008 3:45 AM

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

on Thu, Jul 17 2008 11:06 AM

Louisiana class action lawsuits effexor. Effexor. Effexor and weight gain.

Chris Johnson wrote re: Avoid API breaking changes
on Fri, May 15 2009 3:41 AM

Yes, that sounds good, but does it work for native code? if not I'm not really interested in it right now.

I've been writing my own tools at work using an API that ships with some software we purchased call 'Understand 4 c++. It a great source code analysis tool, which I find indispensable.

Patrick Smacchia [MVP C#] wrote On Managing API
on Mon, Jul 13 2009 12:19 PM

Normal 0 21 false false false FR X-NONE X-NONE Recently, Jb Evain , the creator of Mono.Cecil , asked

Add a Comment

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