File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes if(false) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "if(false)" Watch "if(false)" New topic
Author

if(false)

naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
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

Joined: Sep 08, 2007
Posts: 3099
    
  33

Java or JavaScript? Both are different.
if(false) would always evaluate the else{} and would never evaluate anything in if{}


Mohamed Sanaulla | My Blog
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

Or perhaps some temporary piece of code that was never removed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 10, 2002
Posts: 61772
    
  67

It is still nothing that should be in production-ready code. In either language.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Jan 10, 2002
Posts: 61772
    
  67

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

Joined: Oct 13, 2005
Posts: 40061
    
  28
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

Joined: Jan 25, 2012
Posts: 120
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if(false)