Every now and then, you run into a piece of code that makes you wonder how it ever got into existence. I personally have a bit of a fascination for these things. I read TheDailyWTF regularly for amusement, but I always try to understand what caused these usually inventive but nevertheless misguided pieces of work. When I run into something within my own circle, understanding may also help me prevent problems in the future.
Often the root cause of a "WTF" is an incomplete understanding of the technology being used, which is relatively easily fixed provided someone is willing to learn. In this case however, I'm perplexed. I can't for the life of me imagine why the following code was ever written, by a fairly knowledgeable .NET developer no less:
using System;
namespace types
{
///
/// Boolean type.
///
public class Boolean : System.Object, IComparable, IFormattable
{
private bool? aValue;
public Boolean()
{
}
public Boolean(System.Boolean aValue)
{
this.v = aValue;
}
public bool? v
{
get { return aValue; }
set
{
aValue = value;
}
}
public override string ToString()
{
string to = Convert.ToString(aValue);
return to;
}
#region IComparable Members
public int CompareTo(object obj)
{
if (obj is bool)
{
return this.v.Value.CompareTo(obj);
}
else if (obj is types.Boolean)
{
return this.v.Value.
CompareTo((obj as types.Boolean).v.Value);
}
else
{
throw new InvalidCastException(
"Argument cannot be compared to types.Boolean");
}
}
#endregion
#region IFormattable Members
public string ToString(string format, IFormatProvider provider)
{
return this.ToString();
}
#endregion
}
}
Given the general knowledge level of the person who wrote this, I was almost certain that there was some subtlety to it, however small. I cannot find any sane reason why this class exists. Sure, it wraps a value type into a reference type, but that's what boxing is for. I'm pretty sure the person who wrote this knows about boxing and unboxing. Also this class was used all over the place, where using reference types was completely unnecessary.
To make it worse (or is it better?), similar classes called Integer, Long, Float, Double and DateTime also existed. No full marks for consistency however, because the Decimal type was missing.