Groups | Blog | Home
all groups > c# > march 2004 >

c# : Is this a bug in ?: Operator, the addition does not perform as expected


proware NO[at]SPAM del2.vsnl.net.in
3/6/2004 11:31:10 PM
I am posting the code snippet here. It seems to me that the ?:
Operator is not behaving properly. It seems like it is a bug:

TimeSpan ts1, ts2;
float dayHours = 0;

ts1 = (some_datetime_variable_1);

ts2 = (some_datetime_variable_2);

// ****************** Working Code *************************
// This computes dayHours correctly
if(ts1.TotalHours > 0)
dayHours = (float)ts1.TotalHours;

if(ts2.TotalHours > 0)
dayHours += (float)ts2.TotalHours;
// ****************** Working Code *************************

// ****************** Non-Working Code *************************
// This does not add the portion after + sign
dayHours = (float)(ts1.TotalHours > 0 ? ts1.TotalHours : 0 +
ts2.TotalHours > 0 ? ts2.TotalHours : 0);
// ****************** Non-Working Code *************************

As commented above, I am trying to condense the working code to one
line using ? operator but the one line non-working code dows not add
the variable after + sign. For example, if ts1.TotalHours is 4 and
ts2.TotalHours is 3, the dayHours is computed as 4. Though, I can use
the working code, which takes four lines but it is something I am
doing wrong here or it is a bug in .NET Framework?

Thank you for your time for looking at my post and would appreciate
your responses.

proware NO[at]SPAM del2.vsnl.net.in
3/7/2004 7:27:38 AM
Thanks Jon and C# Learner,

Bracketing saved the day for me. I thought about using bracket earlier
but thought that ? operator would take precedence. Obviously that was
not the case. I agree with your advice on readability but there are
times that the code file becomes so big, I become more inclined to
using condensed code like above. But that only helps reduce the file
size and takes so much time and effort while working through. Thanks
guys.

Jon Skeet [C# MVP]
3/7/2004 7:51:10 AM
[quoted text, click to view]

I don't think there's a bug in .NET, I think there's a problem with
your code in terms of lack of bracketting. I believe your code is being
compiled as:

dayHours = (float)(ts1.TotalHours > 0 ? ts1.TotalHours :
(0 + (ts2.TotalHours > 0 ? ts2.TotalHours : 0));

Instead, you want:

dayHours = (float)((ts1.TotalHours > 0 ? ts1.TotalHours : 0) +
(ts2.TotalHours > 0 ? ts2.TotalHours : 0));

However, this just demonstrates that the version of the code using the
conditional operator is harder to read than the longer version. Code
which takes four lines instead of one isn't necessarily bad - in fact,
it will often be better, as it's likely to be more readable (as in this
case).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
C# Learner
3/7/2004 8:12:38 AM
[quoted text, click to view]

As Jon said, it's probably best to write out the fuller, more-readable
lines.

Personally, I might make a method called something along the lines of
GetDayHours() or CalculateDayHours(), which contains these lines. It is
then just a case of:

dayHours = GetDayHours(param1, param2, ..., paramN);

Alternatively, you could make a general-purpose static method called
EnsureNonNegative(), e.g.:

private static int EnsureNonNegative(int val)
{
return val >= 0 ? val : 0;
}

....and then say:

dayHours = (float)(EnsureNonNegative(ts1.TotalHours) +
EnsureNonNegative(ts2.TotalHours));

Jay B. Harlow [MVP - Outlook]
3/7/2004 2:31:13 PM
R. Kaushik,
In addition to Jon's comments writing readable code.

Martin Fowler's book "Refactoring - Improving the Design of Existing Code"
from Addison Wesley http://www.refactoring.com/ provides a number of
Refactorings on how to improve the design of existing code, plus the reasons
you would want to.

Hope this helps
Jay

[quoted text, click to view]

Jon Skeet [C# MVP]
3/7/2004 4:14:56 PM
[quoted text, click to view]

I've never come across a situation where source file size is actually
more important than readability. I would *always* use the expanded
version in this case. There are times when it's worth using the
conditional operator, but I can't remember ever using it twice in the
same statement - readability just goes out the window.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
AddThis Social Bookmark Button