Sunday, November 01, 2009

Shorten Your If’s By Avoiding ‘Or’

I’m not suggesting there’s necessarily a better language syntax or that we need one, but I find this annoying;

if (value == MyEnum.EnumValue1 || value == MyEnum.EnumValue3 || value == MyEnum.EnumValue4)


Why can’t I just type this and have the compiler know what I mean;



if (value == MyEnum.EnumValue1 || MyEnum.EnumValue3 || MyEnum.EnumValue4)


Now I know what you’re thinking, there’s multiple ways the compiler could interpret my use of or (||) in that scenario, but the point is this is a common type of statement to write and it’s unnecessarily verbose – especially if you have meaningful variable and type names which is something that should be encouraged.



Some of you are probably also thinking I should have used switch, which is fine some of the time but still takes up quite a lot room on the screen and isn’t really appropriate when your conditional has only the ‘true’ condition for one set of values.



What we really need is something equivalent to the ‘In’ keyword in t-sql, i.e



if (value in(MyEnum.EnumValue1, MyEnum.EnumValue3, MyEnum.EnumValue4))


Well, the good news is you can build one yourself… here’s some of mine;



#region StringInList Overloads
/// <summary>
/// Returns a boolean value indicating whether or not the specified search string appears in the specified list of values.
/// </summary>
/// <param name="searchValue">The string to search for.</param>
/// <param name="ignoreCase">A boolean indicating whether or not the search is case sensitive.</param>
/// <param name="listValues">A list of string values to search in.</param>
/// <returns>A boolean value, true if the searchValue parameter value appears in the listValues parameter set.</returns>
public static bool StringInList(string searchValue, bool ignoreCase, params string[] listValues)
{
    return StringInList(searchValue, ignoreCase, (IEnumerable<string>)listValues);
}
/// <summary>
/// Returns a boolean value indicating whether or not the specified search string appears in the specified list of values.
/// </summary>
/// <param name="searchValue">The string to search for.</param>
/// <param name="ignoreCase">A boolean indicating whether or not the search is case sensitive.</param>
/// <param name="listValues">An enumerable list of string values to search in.</param>
/// <returns>A boolean value, true if the searchValue parameter value appears in the listValues parameter set.</returns>
public static bool StringInList(string searchValue, bool ignoreCase, IEnumerable<string> listValues)
{
    bool retVal = false;
    foreach (string testString in listValues)
    {
        if (String.Compare(searchValue, testString, ignoreCase) == 0)
        {
            retVal = true;
            break;
        }
    }
    return retVal;
}

#endregion

 

And if you’re not dealing with strings;

#region ValueInList Overloads
/// <summary>
/// Returns a boolean indicating whether the specified item is in the list of values provided (true).
/// </summary>
/// <typeparam name="T">The type of value being tested.</typeparam>
/// <param name="value">The value to search the list for.</param>
/// <param name="list">A list of values to search in.</param>
/// <returns>A boolean, true if 'value' is one of the values provided in the list argument.</returns>
/// <exception cref="System.ArgumentNullException">Occurs if the value parameter is null</exception>
public static bool ValueInList<T>(T value, params T[] list)
{
    return ValueInList<T>(value, (IEnumerable<T>)list);
}
/// <summary>
/// Returns a boolean indicating whether the specified item is in the list of values provided (true).
/// </summary>
/// <typeparam name="T">The type of value being tested.</typeparam>
/// <param name="value">The value to search the list for.</param>
/// <param name="list">An enumerable set of values to be searched.</param>
/// <returns>A boolean, true if 'value' is one of the values contained in the list argument.</returns>
/// <exception cref="System.ArgumentNullException">Occurs if the value parameter is null</exception>
public static bool ValueInList<T>(T value, IEnumerable<T> list)
{
    if (value == null) throw new ArgumentNullException("value");
    bool retVal = false;
    foreach (T item in list)
    {
        if (item.Equals(value))
        {
            retVal = true;
            break;
        }
    }
    return retVal;
}
#endregion


If you’re working in .Net 3.5 and have access to Lambda Expressions and the Extension Methods built for Linq, you can make an even simpler version;



public static bool In<T>(this T e, params T[] list)
{
  return list.Any(l => e.Equals(l));
}









Technorati Tags: ,,,,,,,

No comments:

Post a Comment