• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if(false)

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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{}
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or perhaps some temporary piece of code that was never removed.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is still nothing that should be in production-ready code. In either language.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic