Bools reloaded

xs0 xs0 at xs0.com
Mon Mar 6 04:45:01 PST 2006


Ivan Senji wrote:
> xs0 wrote:

>> I used to think so too, until I was forced to code in Java :) 
> 
> I also coded in Java and never found this to be a problem :)

Sure, it has an easy "workaround", but like I said, I don't think the 
added verbosity helps, rather the opposite.


>> Even though I don't mind typing something as simple as
>>
>> if (a!=null)
>>
>> the verbosity of longer expressions really annoys the heck out of me. 
>> Now, I prefer
>>
>>  > if (a && b && c && d) // check for nulls
> 
> But what is going on behind the scene if a,b,c,d are ints or ponters,
> conversions to bools? I still don't get it how logic && operator can be 
> aplied to integers. What is 5 && 3? (Except two numbers: 5,3.) It has no 
> value regarding truth. It is neither true nor false.

Well, I believe that is just a matter of education/documentation. As 
much as you need to know that && will short-circuit, you also need to 
know that it will evaluate the operands as booleans. And, while 3 or 5 
are definitely neither true nor false in themselves, one can certainly 
define a conversion between them and booleans..

It's somewhat like Java's evaluation of "abc"+123. It's implied that 123 
will be converted to a string form, and no-one seems to mind, even 
though there is no implicit relationship between numbers and strings 
(no, there isn't; 123 as a string could just as easily be "321"(*) or 
"123.0000000000000", "onetwothree" or "WARNING: auto-conversion of 123 
of type int to java.lang.String").. Would you also prefer

"abc"+String.valueOf(123)

or even

// hey, who ever heard of a sum of strings?
StringBuffer sb=new StringBuffer();
sb.append("abc");
sb.append(123);
return sb.toString();

?

I mean, compared to almost everything else in programming, is it really 
so hard to grasp/learn/remember/know/whatever that 0 and null evaluate 
to false, while everything else evaluates to true?


xs0

*) this could be called a little-endian string :)



More information about the Digitalmars-d mailing list