CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Compare guard code styles

310 Views
Copy Code Show/Hide Line Numbers
public static void Copy(string sourceDirName, string destDirName, bool excludeSubdirectories)
{
    // Style #1: fluent
    Guard.MethodArgument("sourceDirName").IsNotNullOrEmpty(sourceDirName);
    Guard.MethodArgument("destDirName").IsNotNullOrEmpty(destDirName, "The destination directory is required!");
 
    // Style #2: lambdas and expressions
    Guard.Argument.IsNotNullOrWhiteSpace(()=>sourceDirName);
    Guard.Argument.IsNotNullOrWhiteSpace(()=>destDirName, "The destination directory is required!");
 
    Guard.Against<IOException>(!System.IO.Directory.Exists(sourceDirName), "Source directory was not found.");
 
    // TODO: ...
}
by Al Gonzalez
  May 24, 2010 @ 8:58pm
Tags:
Description:
Going with a slight variation on Style #1:
Guard.Argument("sourceDirName").IsNotNullOrEmpty(sourceDirName);

The reason for this is that using Expressions to extract the argument name works in C# but not in VB.NET and I'd rather not rely on a compiler feature for such a minimal (IMO) gain.


by Al Gonzalez    May 25, 2010 @ 9:48pm

Going with a slight variation on Style #1:
Guard.Argument("sourceDirName").IsNotNullOrEmpty(sourceDirName);

The reason for this is that using Expressions to extract the argument name works in C# but not in VB.NET and I rather not rely on a compiler feature for such a minimal (IMO) gain.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate