The following extension method is being used in our code base:
public static bool ToBool(this object src)
{
return src != null && ((string) src).ToBool(false);
}
It leverages off another extension method:
public static bool ToBool(this string src, bool defaultValue)
{
if (src.IsEmpty())
return defaultValue;
if (src.IsNumeric())
return src.ToInt() != 0;
var ret = false;
if (bool.TryParse(src, out ret))
return ret;
return defaultValue;
}
(where the ToInt and all the Is* methods are wrappers around the appropriate objects IsNullEmpty / Parse method)
This does not 'feel' right but I cannot explain it to the author. Besides for the obvious problem of passing an object that cannot be cast to a string what other reasons are there to justify this as bad code?
Are there any other concrete reasons why this code may be good or bad?