473,513 Members | 2,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.

R. Kaushik
Nov 15 '05 #1
5 1242
R. Kaushik <pr*****@del2.vsnl.net.in> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
R. Kaushik wrote:
[...]
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.
[...]


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));

Just my £0.02.
Nov 15 '05 #3
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.

R. Kaushik
Nov 15 '05 #4
R. Kaushik <pr*****@del2.vsnl.net.in> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
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

"R. Kaushik" <pr*****@del2.vsnl.net.in> wrote in message
news:87**************************@posting.google.c om...
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.

R. Kaushik

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2075
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
4
1802
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
4
1358
by: Peter | last post by:
The Operator overloading in C# has one limitation which I have not seen stated explicitly anywhere. Take the following example public class A { int m_A; public A(int a) {
13
1770
by: Jon Cosby | last post by:
VB .Net does support operator overloading, doesn't it? It seems like this should overload binary addition, but VB doesn't recognize "Operator" Public Shared Operator +(ByVal c1 as cnum, ByVal c2...
3
1124
by: Roger Garrett | last post by:
I'm having a problem with a derived class. When a copy is made of the object it does indeed copy the additional member variables of the object but it does not seem to be copying the members of the...
7
7293
by: vbgunz | last post by:
function operator(a, b) { return a + b; } without the use of eval, how would you change the + operator into say * ? If you cannot pass operators in any form what would be the best thing to do...
4
3348
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
4
1938
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
1
6700
by: Stuart Brockman | last post by:
Hi, I don't quite get what is going on in this code example: --------------------------- #include <iostream> using namespace std; class Base{ public:
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.