A Better Razor IsNullOrEmpty Statement

This post is a play on Phil's "A Better Razor Foreach Loop".  If you aren't familiar with templated delegates in Razor, you can follow the links in Phil's post.

Something I don't like to see in a view is the if null or empty check.

@if (!String.IsNullOrEmpty(User.Identity.Name)) { 
    <span>Logged in as: @User.Identity.Name</span>
}

There are a number of ways to remove the check, but the knowledge in Phil's post give you one more option -  the HTML helper option.

public static class RazorExtensions
{    
    public static HelperResult Maybe(this string @string,
                                     Func<object, HelperResult> template)
    {
        return new HelperResult(writer =>
        {
            if (!String.IsNullOrEmpty(@string))
            {
                template(@string).WriteTo(writer);
            }
        });
    }
}

Now the view code can look like this:

@User.Identity.Name.Maybe(@<span>Logged in as: @item</span>)

Better? Or Worse?

Print | posted @ Tuesday, June 28, 2011 9:12 AM

Comments on this entry:

Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Abc at 6/28/2011 9:27 AM

better? worse? hmm... maybe...
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Al Gonzalez at 6/28/2011 9:45 AM

I like the helper function but not the name Maybe. It seems too fuzzy, like it could test almost anything -- maybe if the user is logged in. I'd have to give some thought to what would work for me.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Elmar de Groot at 6/28/2011 9:57 AM

That's a bit too much abstraction for my taste ;)
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by gerry lowry at 6/28/2011 9:58 AM

worse ... lacks clarity; you're simply wrapping .IsNullOrEmpty with an unclear alias .Maybe

better would be to call .Maybe .IsNullOrEmpty instead; this would give your code familiarity and some elegance imho.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Jamie at 6/28/2011 10:25 AM

Why not just IfNotNull or IfExists? I think that conveys the intent pretty clearly.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Kyralessa at 6/28/2011 10:49 AM

The method is better, but the name could use some work. IfExists would be good.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Andrew Robinson at 6/28/2011 11:32 AM

You could call it HasValue() and possibly apply it to any ref/nullable type. Or add a second method as such.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Bunter at 6/28/2011 12:11 PM

@Html.If(User.Identity.Name.NullOrEmpty, <span>Anonymous</span>)

would be my approach. this would require standard string extension and helper extension
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by James Culbertson at 6/28/2011 1:22 PM

I like it but recommend a change in the naming, such as .IfHasValue().
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Eber Irigoyen at 6/28/2011 2:05 PM

better but Maybe is a bad name, perhaps .WhenValue, or WhenNotEmpty, something like that
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by bennyb at 6/28/2011 2:08 PM

.IfHasValue()

+1
Make more sense.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Kim Tranjan at 6/28/2011 6:06 PM

Hey people, no matter the function name but what it does.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by James Culbertson at 6/28/2011 10:54 PM

The name adds to the readability/maintainability of the code and that's important too.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by James at 6/28/2011 11:39 PM

It's not ~bad~, but I would agree that it's an abstraction that's probably not necessary. The IsNullOrEmpty() check is visually familiar and shows intent.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Phil at 6/29/2011 3:31 AM

The arguments about the chosen naming convention seem somewhat moot. The point is that this post gives a clear example of how to implement a level of abstraction that may (or may not) benfit the developer's working style.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Nick at 6/29/2011 8:46 AM

Html.IfNotNullOrEmpty(item,value)
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Kim Tranjan at 6/30/2011 7:43 PM

Exactly James,
That's why doesn't matter the function name. You set the name that looks better for you and your team.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by Jeremy Gray at 7/1/2011 9:03 AM

It will be easier to unit test the Razor extension.
  
Gravatar # re: A Better Razor IsNullOrEmpty Statement
by E Rolnicki at 7/8/2011 7:11 AM

I would argue that perhaps the view should not need to do this at all, but rather the controller would instead.
  
Comments have been closed on this topic.
Scott Allen
Posts - 869
Comments - 4493
Stories - 14