Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if(false)

 
naga eswar
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all....


In my application, I observed at Java script code if(false).

Can you please explain where and when this condition uses...
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java or JavaScript? Both are different.
if(false) would always evaluate the else{} and would never evaluate anything in if{}
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:if(false) would always evaluate the else{} and would never evaluate anything in if{}

Either way, it seems a bit pointless...in Java or Javascript; but maybe it's some nifty 'evaluator' mechanism I've never come across
...or possibly auto-gen'd code?

Winston
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or perhaps some temporary piece of code that was never removed.
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A decent IDE will mark that as dead code. If it were in Java code, I believe the compiler would remove it, i.e., it won't produce byte code for the block. In JavaScript, well, I guess it would be easier to flip false to true in something like FireBug, than it would be to comment/uncomment code. I agree with Rob that it sounds like temporary code, and it shouldn't go to production that way.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure about javascript. but I come across this idiom in java. and i find the answer when i read effective java(not sure though...) . suppose you want to comment bunch of statement, and you use /* */. and it wont work if the statements itself has /* */ . i.e, nested block comment wont work, produce compilation error. however, it is easy to identify in modern IDE(easy to use // ). So some people comment the code by using if(false) block rather than /**/. you know its all about style

<edit>corrected typo</edit>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is still nothing that should be in production-ready code. In either language.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, it is explicitly allowed in the JLS, which may be giving some people it's a good idea. But it' a case where Java's creators were trying to emulate a then-common idiom in C, and there's not really any need for it now.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Of course, it is explicitly allowed in the JLS, which may be giving some people it's a good idea.

Exactly. Another example of "just because you can do something, doesn't mean that you should".
 
Campbell Ritchie
Sheriff
Pie
Posts: 49392
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote: . . . Java's creators were trying to emulate a then-common idiom in C . . .
I wish Java’s creators had realised they were creating a new language rather than a new version of C.
 
naga eswar
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly guys .... the code is from java script.

But either in java or javascript this if block will not execute, we can say its alternate to /* */
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naga eswar wrote:Exactly guys .... the code is from java script.
But either in java or javascript this if block will not execute, we can say its alternate to /* */

Or useless. Take your pick. In my experience, "historical documentation" is almost invariably a waste of space.

Winston
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with @bear that it (if (false)) has no place in production quality code, but I use it a lot while debugging.
Specifically to remove a big wad of code for testing and problem isolation.

It is equivalent to /* .... */
but when the block of code you are trying go comment out has comments, something the pairing doesn't work as you want, so you
can't just put a /* */ around a big wad of code and comments.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:Specifically to remove a big wad of code for testing and problem isolation.

Debugging, possibly; but testing? How can you say you've tested something if you just removed a chunk of code?

Winston
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Debugging, possibly; but testing? How can you say you've tested something if you just removed a chunk of code?


Easy, problem isolation is part of testing. Removing code makes that easier. Sometimes.

 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:Easy, problem isolation is part of testing. Removing code makes that easier. Sometimes.

Hmmm. Sounds like changing the test to me. Surely isolation is down to your test suite?

Winston
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: Sounds like changing the test to me. Surely isolation is down to your test suite?


Of course it is.

Sometimes real world debugging is not as easy as the folks pushing JUnit or other Test First methodologies suggest.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat Farrell wrote:Sometimes real world debugging is not as easy as the folks pushing JUnit or other Test First methodologies suggest.

Actually, good testing, JUnit or otherwise, often reveals flaws in the code that developers didn't think of; and it's often revealed in the things you can't test.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic