Advice needed on SmartBool, SmartInt16, SmartInt32, SmartInt64, etc.Advice needed on SmartBool, SmartInt16, SmartInt32, SmartInt64, etc.
Old forum URL: forums.lhotka.net/forums/t/1649.aspx
david.wendelken posted on Tuesday, October 31, 2006
I've got working copies of SmartInt16, SmartInt32 and SmartInt64 ready to go, plus a subclass of SafeDataReader that recognizes them. (Have sent Rocky an email so I can add them to CSLA Contrib).
I'm working on SmartBool and ran into something I'm not sure about.
Basically, there are some Add and Subtract functions plus + and - operator overloading.
Should I just remove them? Or implement the functionality such that:
True + True = True
True + False = False
False + False = ??
True + Empty = ??
False + Empty = ??
Empty + Empty = Empty
Which option makes more sense to you and why?
david.wendelken replied on Tuesday, October 31, 2006
Forgot to add tgthe following facts:
- My subclass of SafeDataReader required a single modification of SafeDataReader. The _dataReader variable was changed from internal to protected so that it could be referenced as base._dataReader. Anyone know a better way?
William replied on Tuesday, October 31, 2006
For me, I think I will implement the operator behaviors following boolean algebra and SQL rules for NULL (equivalent to Empty). Thus, in boolean algebra, + means OR, * means AND,
True + True = True
True + False = True
False + True = True
False + False = False
True + Empty = Empty
Empty + True = Empty
False + Empty = Empty
Empty + False = Empty
Empty + Empty = Empty
Regards,
William
RRorije replied on Wednesday, November 01, 2006
This was also my first idea.
But after thinking it over one time more, it creates a problem.
I do not know whether this is available in C#, but in VB you have the keyword "OrElse" which is a lazy version of "Or". Which means, when you have the following code:
if x orelse y then
...
end if
Then y will only be evaluated when x is False. When x is True then x or y is also True, so y does not have to be evaluated.
In the case you gave above, this is not possible anymore. The fifth member: True + Empty = Empty implies that both have to be evaluated before you know what the value is. I do not know whether this is a big deal, but it is something to think about.
Regards,
Ruben
Bayu replied on Wednesday, November 01, 2006
RRorije:This was also my first idea.
I do not know whether this is available in C#, but in VB you have the keyword "OrElse" which is a lazy version of "Or". Which means, when you have the following code:
Actually it's called a short-circuiting operator. But that's just for the books. ;-)
Why can't you just use the 'Or' operator? This one does not 'short-circuit' (is this a valid verb?), but will always evaluate both operands.
On the topic of this thread: I think that operators are very C++-ish, and should only be used when you are writing software for math or logic reasoning purposes. In these contexts the operators are well-defined and intuitive in use. I think that having these operators on a datareader will only cause you to forget what the actual implementation was again and make you look it up in your code time and time again.
Bayu
William replied on Wednesday, November 01, 2006
I am not familar with VB.NET but in C# I think this will do:
public static SmartBool operator+(SmartBool x, SmartBool y)
{
if (x.IsEmpty || y.IsEmpty)
return SmartBool.Empty;
else
return new SmartBool(x.Value || y.Value);
}
Regards,
William
Bayu replied on Wednesday, November 01, 2006
William:I am not familar with VB.NET but in C# I think this will do:
public static SmartBool operator+(SmartBool x, SmartBool y)
{
if (x.IsEmpty || y.IsEmpty)
return SmartBool.Empty;
else
return new SmartBool(x.Value || y.Value);
}
Regards,
William
In VB It would like this:
Public Shared Operator +(ByVal x As SmartBool, ByVal y As SmartBool) As SmartBool
If x.IsEmpty Or y.IsEmpty Then
Return SmartBool.Empty
Else
Return New SmartBool(x.Value Or y.Value)
End If
End Operator
So that's should work fine.
I just wonder if other developers will ever get an intuition as to what it means to 'add two SmartBools', especially in the context potential Empty values. I think everybody will be looking up the actual implementation after all.
Bayu
ajj3085 replied on Wednesday, November 01, 2006
The VB would be OrElse, because in C# the && and || always short circuit.
Makes sense; if the first parameter is true, the whole expression will be true, regardless of what the second expression would be. Likewise for and... if the first expression is false, the second expression doesn't matter, because the whole expression will be false.
It was done in the C days for peformance reasons. I'm glad they kept it though, because it doesn't force you to make two if blocks when the second might blow up (if there's a null reference in the second expression).
I have to agree though; adding and subtracting bools doesn't seem very intiutive to begin with, and I think people will be looking at the docs alot. If you need this for some reason, fine, but I wouldn't implement it unless you really need to.
Andy
david.wendelken replied on Wednesday, November 01, 2006
ajj3085:
I have to agree though; adding and subtracting bools doesn't seem very intiutive to begin with, and I think people will be looking at the docs alot. If you need this for some reason, fine, but I wouldn't implement it unless you really need to.
Andy
It wasn't very intuitive to me either (but that hardly qualifies as defining the issue as non-intuitive).
The capability is optional in that the compiler isn't insisting upon it. ;)
I will leave it out until given a compelling reason to put it in.
Copyright (c) Marimer LLC