A Better Razor IsNullOrEmpty Statement

Tuesday, June 28, 2011

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?


Comments
gravatar Abc Tuesday, June 28, 2011
better? worse? hmm... maybe...
gravatar Al Gonzalez Tuesday, June 28, 2011
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.
Elmar de Groot Tuesday, June 28, 2011
That's a bit too much abstraction for my taste ;)
gravatar gerry lowry Tuesday, June 28, 2011
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 Jamie Tuesday, June 28, 2011
Why not just IfNotNull or IfExists? I think that conveys the intent pretty clearly.
gravatar Kyralessa Tuesday, June 28, 2011
The method is better, but the name could use some work. IfExists would be good.
gravatar Andrew Robinson Tuesday, June 28, 2011
You could call it HasValue() and possibly apply it to any ref/nullable type. Or add a second method as such.
Bunter Tuesday, June 28, 2011
@Html.If(User.Identity.Name.NullOrEmpty, <span>Anonymous</span>)

would be my approach. this would require standard string extension and helper extension
gravatar James Culbertson Tuesday, June 28, 2011
I like it but recommend a change in the naming, such as .IfHasValue().
gravatar Eber Irigoyen Tuesday, June 28, 2011
better but Maybe is a bad name, perhaps .WhenValue, or WhenNotEmpty, something like that
gravatar bennyb Tuesday, June 28, 2011
.IfHasValue()

+1
Make more sense.
gravatar Kim Tranjan Tuesday, June 28, 2011
Hey people, no matter the function name but what it does.
gravatar James Culbertson Tuesday, June 28, 2011
The name adds to the readability/maintainability of the code and that's important too.
James Tuesday, June 28, 2011
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.
Phil Wednesday, June 29, 2011
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.
Nick Wednesday, June 29, 2011
Html.IfNotNullOrEmpty(item,value)
gravatar Kim Tranjan Thursday, June 30, 2011
Exactly James,
That's why doesn't matter the function name. You set the name that looks better for you and your team.
gravatar Jeremy Gray Friday, July 1, 2011
It will be easier to unit test the Razor extension.
E Rolnicki Friday, July 8, 2011
I would argue that perhaps the view should not need to do this at all, but rather the controller would instead.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!