From: Burrows on
A new college at work use the following

if(true==x)
{
.........
}

now I have through all my programming years and through various
languages have always used

if(x==true)
{
.........
}

Now yes we get the same result in checking for a true value but he's a
smug sod so don't wish to question his methods. So should I tell him
to change to my method or adopt his as it's better????
From: Jeff Johnson on
"Burrows" <burrows.stephen(a)gmail.com> wrote in message
news:c375543f-1651-4a8f-9ce8-0c767eb420a3(a)x21g2000yqa.googlegroups.com...

>A new college at work use the following
>
> if(true==x)
> {
> ........
> }
>
> now I have through all my programming years and through various
> languages have always used
>
> if(x==true)
> {
> ........
> }
>
> Now yes we get the same result in checking for a true value but he's a
> smug sod so don't wish to question his methods. So should I tell him
> to change to my method or adopt his as it's better????

I personally find that putting the constant first is horrifically ugly, but
ultimately it's the same either way to the compiler. I think it's harder to
read, like talking in reverse (much the same reason I despise top posting,
but that's a flamewar for another day).

I like my conditionals to read "If <the thing I'm interested in> <compares
to> <a certain value>"; it just flows better.


From: Jeff Johnson on
"Jeff Johnson" <i.get(a)enough.spam> wrote in message
news:hvtqqp$o62$1(a)news.eternal-september.org...

>> if(true==x)

>> if(x==true)

I forgot to comment on the obvious: explcitly comparing to true is the REAL
ugliness here. Just do

if (x)
{
...
}

But I think everyone gets the gist of what you're saying.


From: Arne Vajhøj on
On 23-06-2010 16:22, Burrows wrote:
> A new college at work use the following
>
> if(true==x)
> {
> ........
> }
>
> now I have through all my programming years and through various
> languages have always used
>
> if(x==true)
> {
> ........
> }
>
> Now yes we get the same result in checking for a true value but he's a
> smug sod so don't wish to question his methods. So should I tell him
> to change to my method or adopt his as it's better????

You should use:

if(x)
{
...
}

The reason behind the if(constant==variable) instead of the
more natural if(variable==constant) is to prevent writing
if(variable=constant) by accident.

Not a particular good reason in my opinion, but there are
some people with a C/C++ background that use it.

Arne

From: Peter Duniho on
Arne Vajh�j wrote:
> On 23-06-2010 16:22, Burrows wrote:
>> A new college at work use the following
>>
>> if(true==x)
>> {
>> ........
>> }
>>
>> now I have through all my programming years and through various
>> languages have always used
>>
>> if(x==true)
>> {
>> ........
>> }
>>
>> Now yes we get the same result in checking for a true value but he's a
>> smug sod so don't wish to question his methods. So should I tell him
>> to change to my method or adopt his as it's better????
>
> You should use:
>
> if(x)
> {
> ...
> }
>
> The reason behind the if(constant==variable) instead of the
> more natural if(variable==constant) is to prevent writing
> if(variable=constant) by accident.

Note that doing so is not really an issue in C#, because types are not
generally implicitly convertible to Boolean, and so an unintended
assignment in an "if" statement will cause a compile-time error.

The one obvious exception is of course some along the lines of "if (x =
true)", but as stated by both Jeff and Arne, that's a silly thing to
write in any case, whether you want = or ==.

> Not a particular good reason in my opinion, but there are
> some people with a C/C++ background that use it.

I never did like the idiom in C/C++, especially since compilers
generally emit a warning on an assignment in an "if" statement that
isn't then compared to something else, and so if you treat warnings as
errors, it's not really likely to cause a problem to do it the other way.

But because of the lack of a real error in C/C++ I do see why some
people prefer it.

To me, it's just hard to read. I don't really even know why, since
equality is symmetric. But for some reason, I don't like putting the
literal first.

Pete