Advertisements
Posted by: jsonmez | June 18, 2010

The Power of Func<>

I remember why I love C#.

After spending the last two years or so writing mainly Java code, getting back into Visual Studio felt a little awkward and painful.

Where did all my keyboard shortcuts go?  Why can’t I navigate to members in a class?  Oh, yes Resharper… ah that’s better.

But then it happened.

I was minding my own business writing some code… la la la

public bool IsYummy()
{
    if(!areOverridesLoaded)
    {
        LoadOverrides();  // loads the override from the database into this.theOverride
    }

    if(this.theOverride != null)
    {
        return theOverride.IsYummy.GetValueOrDefault(food.IsYummy);
    }

    return food.IsYummy;
}

public bool IsFruit()
{
    if(!areOverridesLoaded)
    {
        LoadOverrides();
    }

    if(this.theOverride != null)
    {
        return theOverride.IsFruit.GetValueOrDefault(food.IsFruit);
    }

    return food.IsFruit;
}

public bool IsVegetable()
{
    if(!areOverridesLoaded)
    {
        LoadOverrides();
    }

    if(this.theOverride != null)
    {
        return theOverride.IsVegetable.GetValueOrDefault(food.IsVegetable);
    }

    return food.IsVegetable;
}

Oh my, repeated code!  What ever shall I do?

Hmm… It varies by property names only.

Surely I can refactor out the override loading code.  Can’t put it in the constructor, because the goal is to lazy load the overrides from the database.

Func<> to the rescue!

This is the power of Func.  Of delegates really.  You can’t do this in Java folks… Hold on to your chair.  Here we go…. Whee…..

private bool ReturnValueOrDefault(Func<Food, bool?> FoodProperty, bool defaultValue)
{
    if (!areOverridesLoaded)
    {
        LoadOverrides();
    }

    if (this.theOverride != null)
    {
        return foodProperty(this.theOverride).GetValueOrDefault(defaultValue);
    }

    return defaultValue;
}

public bool IsYummy()
{
    ReturnValueOrDefault(o => o.IsYummy, food.IsYummy);
}

public bool IsFruit()
{
    ReturnValueOrDefault(o => o.IsFruit, food.IsFruit);
}

public bool IsVegetable()
{
   ReturnValueOrDefault(o => o.IsVegetable, food.IsVegetable);
}

Good has won…

Evil has been defeated…

Code has been deleted

Thank you Func<>

Don’t forget the Super Combo!

Also, check out the new page I created, a compilation of useful posts I have written about building software in an Agile way.

As always, you can subscribe to this RSS feed to follow my posts on Making the Complex Simple.  Feel free to check out ElegantCode.com where I post about the topic of writing elegant code about once a week.  Also, you can follow me on twitter here.

Advertisements

Responses

  1. You can’t possibly be serious. Java tooling isn’t anywhere near as good as vanilla Visual Studio. You definitely don’t need resharper to have better tooling than the ones that exist in Java.

    • I’m partially kidding, but to be completely honest, Eclipse is what I call a big ugly beast.

      It is extremely powerful. You can do anything at all in Eclipse. Honestly, there is either a plugin or a setting to do absolutely everything including formating your code, and automatically organizing it and refactoring it on saving the file.

      Eclipse is also very fast. You can navigate to an find a method with a short series of keystrokes.

      But, it is ugly. It is the kitchen-sink and more which causes it to have a huge learning curve. When I first started using Eclipse, I thought “this sucks, Visual Studio is so much better.” After a while of using it and switching back to Visual Studio, I realized that Eclipse had some pretty nice features for navigating large projects that Visual Studio lacks.

      All in all, I prefer Visual Studio. I can’t lie, but I’m not a fanboy of any technology (except C#, C# is freaking awesome), so I’ll be honest about both IDEs.

    • What about IntelliJ IDEA (http://www.jetbrains.com/idea)? Surely that’s a pretty strong contender…

      • I never used it much myself, but I do hear it is pretty good.

  2. […] The Power of Func<> (John Sonmez) […]

  3. […] The Power of Func<> […]

  4. I really like to read your articles, they are clear and contain humor.

    • Thank you!

  5. very nice explanation jsomez…Hats off to you.
    Thank you.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: