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

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



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,



Comments

sergiopereira said:

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.

# April 10, 2008 1:17 PM

Nicholas Piasecki said:

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

# April 10, 2008 8:20 PM

Paul Kinlan’s Development Blog » Development Link Goodness 11th April 2008 said:

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

# April 11, 2008 4:11 AM

Denny Voigt said:

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.

# April 21, 2008 10:16 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News