Tony Hoare has called null references his “billion dollar mistake”. Dealing with nulls and their consequences have created a large number of bugs, and eaten a lot of developer time. It’s certainly bad enough when you understand nulls and why they exist, but Benjamin Soddy inherited code from someone who absolutely didn’t.

First, there’s our new type, the ByteBool:

    public enum ByteBool
    {
        IsFalse,
        IsTrue,
        IsNull
    }

Not quite FileNotFound territory. Based on this code alone, you might think that this is some legacy .NET 1.1 code, back before .NET had nullable value types, and thus there was no way to set a boolean to null.

You’d be wrong, however. Because as a sibling to ByteBool, we have the class NullHelper:

public class NullHelper
    {
        /// <summary>
        /// Like the Helper.GetSafeInteger but returns a negative one instead of zero which may be a valid number
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static int ConvertNullableInt(int? value)
        {
            if (value.HasValue)
            {
                return value.Value;
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// Like the Helper.GetSafeInteger but returns a negative one instead of zero which may be a valid number
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static int? ConvertBackToNullableInt(int value)
        {
            if (value != -1)
            {
                return (int?)value;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Converts the null bool to byte bool.
        /// </summary>
        /// <param name="boolean">The boolean.</param>
        /// <returns></returns>
        public static ByteBool ConvertNullBoolToByteBool(bool? boolean)
        {
            if (boolean.HasValue)
            {
                return (boolean.Value ? ByteBool.IsTrue : ByteBool.IsFalse);
            }
            else
            {
                return ByteBool.IsNull;
            }
        }

        /// <summary>
        /// Converts the byte bool to null bool to.
        /// </summary>
        /// <param name="byteBool">The byte bool.</param>
        /// <returns></returns>
        public static bool? ConvertByteBoolToNullBoolTo(ByteBool byteBool)
        {
            switch (byteBool)
                {
                        case ByteBool.IsFalse:
                    return false;
                break;
                case ByteBool.IsTrue:
                    return true;
                break;
                case ByteBool.IsNull:
                    return null;
                break;
                default:
                    return false;
                break;
                }
        }
    }

I’m no expert on the subject, but the comments alone read to me like poetry.

Like the Helper.GetSafeInteger,
but returns a negative one instead of zero which may be a valid number,
the value,
Like the Helper.GetSafeInteger,
but returns a negative one instead of zero which may be a valid number,
the value,
Converts the null bool to byte bool,
the boolean,
Converts the byte bool to null bool to,
the byte bool

Benjamin junked this code, but ByteBool is still a legend around his office. “Are you sure you aren’t ByteBooling this?” is the polite way of saying “your code is bad and you should feel bad” during code-reviews. “I found a ByteBool,” is called out when someone trawls through legacy code, in the same tone of voice as a lifeguard finding a “floater” in the pool.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!