• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification

 
Priya Jothi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

plz have a look at the following comparisons.

1) if(a==null)

(or)

2) if(null==a)


Both comparisons are doing the same job.But,is there any difference between these two in terms of efficiency?.If so what is the reason?.plz let me know..

Thanx in advance,

Regards,
Priya.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not relevant to SCJP, moving to Performance...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No difference.
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Priya Jothi:

plz have a look at the following comparisons.

1) if(a==null)

(or)

2) if(null==a)


Both comparisons are doing the same job.But,is there any difference between these two in terms of efficiency?.If so what is the reason?.plz let me know..

The answer has nothing to do with performance or effeciency.

There is a school of thought that one should be in the habit of always using the latter form because it allows the compiler to catch typos like

if(a = true)

This will *always* return true and the compiler won't complain a bit (the typo is a single "=" instead of "==").

In this school, they point out that had you typed

if(true = a)

the compiler would have complained (you can't set true to anything ).

I personally find that it makes the code hard to read, but that's just me.

Mark
[ July 26, 2005: Message edited by: Mark Wuest ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Wuest:

if(a == true)


Of course the best way to write this is

if (a)

the "== true" is totally redundant.

if (a = null)

won't compile in Java. In C(++) it would, and probably be a hard to find bug. That's where the advice originally is coming from.
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Of course the best way to write this is

if (a)

the "== true" is totally redundant.

Of course, "totally" is in the eye of the beholder. Many folks find it more readable and less cryptic, especially comparing (a == false) to (!a). Many of these same folks also hate that ternary thing.
Originally posted by Ilja Preuss:
if (a = null)

won't compile in Java. In C(++) it would, and probably be a hard to find bug. That's where the advice originally is coming from.

Yes - it does originate in C/C++, though the same bug can still occur in Java not just with booleans. For example, it is common in Java to see if an object reference is null...

Mark
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Wuest:
Of course, "totally" is in the eye of the beholder. Many folks find it more readable and less cryptic, especially comparing (a == false) to (!a).


So why not

if (((a == false) == true) != false)

??? Where do you stop?

People who find

if (!a)

too cryptic should perhaps simply give "a" a better name. Is there anything cryptic about

if (!buttonPressed)

???

Yes - it does originate in C/C++, though the same bug can still occur in Java not just with booleans. For example, it is common in Java to see if an object reference is null...


No, that bug cannot occur in Java, because if you use "=" instead of "==" with non-boolean values, it's quite unlikely to compile. If you have some code that does, I'd like to see it...
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
if (!buttonPressed)

I occasionally just plain do not see that little tiny, itty-bitty ! in there when looking at others' code. It's hard to miss "== false" though. Maybe I need new glasses. Or bigger monitors. I still like "== false" better.

Believe it or not, even when I write C/C++, I use

if(x == 0)

instead of

if(!x)

just because of missing a few of those little buggers.
Originally posted by Ilja Preuss:
No, that bug cannot occur in Java, because if you use "=" instead of "==" with non-boolean values, it's quite unlikely to compile. If you have some code that does, I'd like to see it...

Heh - that's what I get for trying to defend a point I disagree with anyway.

I still remember my first time working on some person's code who did the

if(<some const> == <some variable>)

trick, cursing the person (and the author of whichever book it was he read that told him to do that), and getting sorely tempted to do an emacs exc-x-query-replace-regex to "fix" them all. No, I didn't do it.

Mark
[ July 26, 2005: Message edited by: Mark Wuest ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Wuest:

if (true == a)
I personally find that it makes the code hard to read, but that's just me.


No, not just you, it's me too.


Ilja Preuss
sheriff and author

People who find

if (!a)

too cryptic should perhaps simply give "a" a better name. Is there anything cryptic about

if (!buttonPressed)


People should:
- use a better coding style:

- buy bigger monitors
- increase fontsize
- lern VB (no flamewar to be expected here, isn't it?)

I guess good c/ c++ - compilers should (and do) warn, if they find an assignment inside an condition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic