Whats wrong with this code???
-
I was assigned a QA bug today and I found this horror code written by my colleague :mad:...
private static double ConvertToDouble(object obj, out bool good)
{
if (obj is double || obj is int)
{
good = true;
return (double)obj;
}good = false; return double.NaN; }
This code exploded in to
Exception type: InvalidCastException
Message: Specified cast is not valid.
[System.InvalidCastException] -
I was assigned a QA bug today and I found this horror code written by my colleague :mad:...
private static double ConvertToDouble(object obj, out bool good)
{
if (obj is double || obj is int)
{
good = true;
return (double)obj;
}good = false; return double.NaN; }
This code exploded in to
Exception type: InvalidCastException
Message: Specified cast is not valid.
[System.InvalidCastException] -
I was assigned a QA bug today and I found this horror code written by my colleague :mad:...
private static double ConvertToDouble(object obj, out bool good)
{
if (obj is double || obj is int)
{
good = true;
return (double)obj;
}good = false; return double.NaN; }
This code exploded in to
Exception type: InvalidCastException
Message: Specified cast is not valid.
[System.InvalidCastException]And
float
anduint
are also really bad... -
Yes!! Actually the question was raised by the same colleague when I told him its wrong.... :-D
-
And
float
anduint
are also really bad...Well in theory not... but our middleware doesn't support uint and float so for us (practically) they are bad.
-
I was assigned a QA bug today and I found this horror code written by my colleague :mad:...
private static double ConvertToDouble(object obj, out bool good)
{
if (obj is double || obj is int)
{
good = true;
return (double)obj;
}good = false; return double.NaN; }
This code exploded in to
Exception type: InvalidCastException
Message: Specified cast is not valid.
[System.InvalidCastException]That's what I call good quality, well tested code! :doh: It's obvious it should have been:
return (double)(int)obj;
:laugh:
The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.
-
That's what I call good quality, well tested code! :doh: It's obvious it should have been:
return (double)(int)obj;
:laugh:
The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.
This would fail if obj is double... :-\
-
This would fail if obj is double... :-\
In which case you make it
return (double)(int)(double)obj;
:laugh:
The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.
-
I was assigned a QA bug today and I found this horror code written by my colleague :mad:...
private static double ConvertToDouble(object obj, out bool good)
{
if (obj is double || obj is int)
{
good = true;
return (double)obj;
}good = false; return double.NaN; }
This code exploded in to
Exception type: InvalidCastException
Message: Specified cast is not valid.
[System.InvalidCastException]He likes reinventing the well or doesn't he know Convert.ToDouble()[^]?
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"
-
I was assigned a QA bug today and I found this horror code written by my colleague :mad:...
private static double ConvertToDouble(object obj, out bool good)
{
if (obj is double || obj is int)
{
good = true;
return (double)obj;
}good = false; return double.NaN; }
This code exploded in to
Exception type: InvalidCastException
Message: Specified cast is not valid.
[System.InvalidCastException] -
It should be called TryConvertToDouble with the signature and semantics following the TryXXX functions like TryGetValue(), that is: It should return bool true on successful conversion, false otherwise and the converted value in the out parameter.
It shouldn't exist at all.
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"
-
I was assigned a QA bug today and I found this horror code written by my colleague :mad:...
private static double ConvertToDouble(object obj, out bool good)
{
if (obj is double || obj is int)
{
good = true;
return (double)obj;
}good = false; return double.NaN; }
This code exploded in to
Exception type: InvalidCastException
Message: Specified cast is not valid.
[System.InvalidCastException] -
I was assigned a QA bug today and I found this horror code written by my colleague :mad:...
private static double ConvertToDouble(object obj, out bool good)
{
if (obj is double || obj is int)
{
good = true;
return (double)obj;
}good = false; return double.NaN; }
This code exploded in to
Exception type: InvalidCastException
Message: Specified cast is not valid.
[System.InvalidCastException]Perhaps an effect of not using http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx[^]
Vasudevan Deepak Kumar Personal Homepage BRAINWAVE/1.0 Status-Code: 404 Status-Text: The requested brain could not be found. It may have been deleted or never installed.
--Brisingr Aerowing -
Perhaps an effect of not using http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx[^]
Vasudevan Deepak Kumar Personal Homepage BRAINWAVE/1.0 Status-Code: 404 Status-Text: The requested brain could not be found. It may have been deleted or never installed.
--Brisingr AerowingActually, that's designed to convert a string to double, the code in question is designed to convert objects to double - including explicitly ints. You're not suggesting the way to convert an integer to a double is as follows are you? 1. Convert the int to a string. 2. Convert the string to a double.
"If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.