Derik Whittaker

Syndication

News


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
Simple extension methods to help with Asserting values

In the past I have written about the Fail Fast principle.  This is a principle I try to live my development life by.  I would rather explicitly check for possible failure points then have a runtime error crop up.  In the past when writing these failure check we have had to resort to a few 'cavemanish' techniques.  But with the inception of Extension Methods (here or here) we can actually create something that is pretty simple as well as elegant.

Lets take a look at a few examples where we are NOT using extension methods to accomplish our failure checking.

public bool SomeMethod( string key )  
{  
	// In this case the key cannot be null or empty  
	if ( key.length == 0 ) { throw new ArgumentOutOfRangeException( "key", "Some Message Goes here" ); }
} 

The code above works, it is pretty simple and will get the job done.  But to be honest, having this sprinkled all over your code is kinda ugly.

public bool SomeMethod( SomeObject obj, Int32 someInt )  
{  
	// In this case the object cannot be null  
     	Assert.IsNotNull( obj, "Some Object" );  
  
	// In this case the int must be greater then 1  
     	Assert.IsTrue( someInt > 1, "Some Int not correct" );  
} 

This code works as well and is more concise the the previous example.  However, this code is still less readable then the example below.

Now, lets take a look at the same thing, but using Extension Methods

public bool SomeMethod( SomeObject obj, strnig someString )  
{  
	// In this case the object cannot be null  
	obj.AssertNonNull(); 
  
	// In this case the cannot be null or empty
	someString.AssertNotNullOrEmpty();	
} 

Here is the code for the actual extension methods assert

public static void AssertNonNull( this T value )
{
	// Do our null check here.
	if ( value == null ) 
	{ 
		// Could even use reflection to gather info such as calling method, method exception was
		//	found in, etc.
		throw new ArgumentNullException( "Some message here" );
	}
}

The assert above using the extension method to me is the most elegant.  It is easy to read (very important) and very concise. 

Keep in mind, all the examples above do the exact same thing, but they do in varying levels of elegance.  I would argue that the usage of extension methods here makes the code cleaner and easier to read.

Till next time,


Posted 04-10-2008 7:49 AM by Derik Whittaker

[Advertisement]

Comments

sergiopereira wrote re: Simple extension methods to help with Asserting values
on 04-10-2008 1:17 PM

My reflexes still cause me to stop at:

// In this case the object cannot be null  

obj.AssertNonNull();

and go "Ooops, I have to check if obj is null before calling one of it's methods." I wonder if there's a way to highlight extension methods differently in VS or R# or something.

Nicholas Piasecki wrote re: Simple extension methods to help with Asserting values
on 04-10-2008 8:20 PM

Same thing that sergiopereira said. Just looks freaky to me!

Paul Kinlan’s Development Blog » Development Link Goodness 11th April 2008 wrote Paul Kinlan’s Development Blog » Development Link Goodness 11th April 2008
on 04-11-2008 4:11 AM

Pingback from  Paul Kinlan’s Development Blog » Development Link Goodness 11th April 2008

Denny Voigt wrote re: Simple extension methods to help with Asserting values
on 04-21-2008 10:16 AM

A really good thing. It would be perfect if the debugger wouldn't always show the same location in the IDE when the assertion fails.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)