Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Refactoring if(variable == CONSTANT) into if(CONSTANT == variable)
Refactoring if(variable == CONSTANT) into if(CONSTANT == variable) [message #3805] Thu, 24 April 2003 04:51 Go to next message
Eclipse UserFriend
Hi,

I am new to eclipse jdt. Exploring I chanced upon the Refactoring wizards.

They are great, like rename a class and by refactoring apply the change
through out the project (even in comments/ java docs). Thats really superb.

I have a specific refactoring requirement. Is it possible?

Normally in Java in "if" stmts we write:
if(varaible_name == IConstants.CONSTANT_VALUE) { ...

CONSTANT_VALUE is declared as a public static final in IConstants.java

However becuase of a typo if "==" is typed as "=" (single = sign)
The java compiler will not crib as it is syntactically valid.
But debuging such typo's in complex code is a big head ache.

My personal style in such case's is to put the constant on the left hand
side.
Like:
if(IConstants.CONSTANT_VALUE == varaible_name) { ...

But now given the fantastic Reafactoring wizards in eclipse will it be
able to refactor
if(varaible_name == IConstants.CONSTANT_VALUE) { ...
into
if(IConstants.CONSTANT_VALUE == varaible_name) { ...

of course this does not apply if I am not using constants in my if stmt.

Any comments ?

rgds
g1
Re: Refactoring if(variable == CONSTANT) into if(CONSTANT == variable) [message #3812 is a reply to message #3805] Thu, 24 April 2003 05:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: adam_kiezun.ch.ibm.spam.protection.com

it sounds like a feature request for QuickAssist
(the only preconditions that could maybe be checked would be the absence of side-effects - but that we never do anywhere else.)

please enter a requrest to bugzilla (JDT UI)

a.
--
eclipse.org
Re: Refactoring if(variable == CONSTANT) into if(CONSTANT == variable) [message #3839 is a reply to message #3805] Thu, 24 April 2003 09:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: muelleimer.usenet.katzien.de

* Jeevan <jeevan_sunkersett@satyam.com> wrote:
> However becuase of a typo if "==" is typed as "=" (single = sign)
> The java compiler will not crib as it is syntactically valid.
> But debuging such typo's in complex code is a big head ache.

I'm not sure, but I think I found a new compiler warning about this
problem in one of the new build notes.

Jan
Re: Refactoring if(variable == CONSTANT) into if(CONSTANT == variable) [message #3851 is a reply to message #3839] Thu, 24 April 2003 10:35 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: bob.objfac.com

This is only a problem in Java if the variable is boolean. It's not like
C/C++, where darn near any expression had a boolean interpretation.

Bob

"Jan Schulz" <muelleimer@usenet.katzien.de> wrote in message
news:slrnbafquo.jit.muelleimer@snoopy.katzien.de...
> * Jeevan <jeevan_sunkersett@satyam.com> wrote:
> > However becuase of a typo if "==" is typed as "=" (single = sign)
> > The java compiler will not crib as it is syntactically valid.
> > But debuging such typo's in complex code is a big head ache.
>
> I'm not sure, but I think I found a new compiler warning about this
> problem in one of the new build notes.
>
> Jan
Re: Refactoring if(variable == CONSTANT) into if(CONSTANT == variable) [message #3858 is a reply to message #3805] Thu, 24 April 2003 10:43 Go to previous messageGo to next message
Eclipse UserFriend
Jeevan <jeevan_sunkersett@satyam.com> wrote:

<snip>

> My personal style in such case's is to put the constant on the left hand
> side.

Might I ask why, in Java? It makes sense in C, where a missed = sign
could have damaging side effects, but apart from the very rare case of
comparing two booleans, it's not a problem in Java.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Re: Refactoring if(variable == CONSTANT) into if(CONSTANT == variable) [message #5369 is a reply to message #3858] Fri, 25 April 2003 01:42 Go to previous messageGo to next message
Eclipse UserFriend
<snip>
Jon Skeet wrote:
> > My personal style in such case's is to put the constant on the left hand
> > side.

> Might I ask why, in Java? It makes sense in C, where a missed = sign
> could have damaging side effects, but apart from the very rare case of
> comparing two booleans, it's not a problem in Java.
> --

Yes,

I picked up the style from IBM written C/C++ code.
It did not occur to me that in Java it is applicable only in the very rare
case of comparing two booleans.
Thank you all for the update.

g1
Re: Refactoring if(variable == CONSTANT) into if(CONSTANT == variable) [message #5549 is a reply to message #3839] Fri, 25 April 2003 05:43 Go to previous messageGo to next message
Eclipse UserFriend
Indeed, we just released a new optional warning for this very case (in
JDT/Core build notes):

a.. Added new compiler optional problem to signal cases where a boolean
variable is assigned in a condition expression. It is likely an accidental
situation, where a comparison was actually meant.
* COMPILER / Reporting Possible Accidental Boolean Assignment
* When enabled, the compiler will issue an error or a warning if a
boolean assignment is acting as the condition
* of a control statement (where it probably was meant to be a boolean
comparison).
* - option id:
" org.eclipse.jdt.core.compiler.problem.possibleAccidentalBool eanAssignment "
* - possible values: { "error", "warning", "ignore" }
* - default: "warning"
..NOTE: until some UI is added to change its severity, this diagnosis will
remain a warning.."Jan Schulz" <muelleimer@usenet.katzien.de> wrote in
message news:slrnbafquo.jit.muelleimer@snoopy.katzien.de...
> * Jeevan <jeevan_sunkersett@satyam.com> wrote:
> > However becuase of a typo if "==" is typed as "=" (single = sign)
> > The java compiler will not crib as it is syntactically valid.
> > But debuging such typo's in complex code is a big head ache.
>
> I'm not sure, but I think I found a new compiler warning about this
> problem in one of the new build notes.
>
> Jan
Re: Refactoring if(variable == CONSTANT) into if(CONSTANT == variable) [message #14801 is a reply to message #3858] Tue, 06 May 2003 05:53 Go to previous message
Eclipse UserFriend
"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.19120872d63dc4159897d8@news.eclipse.org...
> Jeevan <jeevan_sunkersett@satyam.com> wrote:

> > My personal style in such case's is to put the constant on the left hand
> > side.
>
> Might I ask why, in Java? It makes sense in C, where a missed = sign
> could have damaging side effects, but apart from the very rare case of
> comparing two booleans, it's not a problem in Java.

Another place constants on the left can be useful in Java is
String comparison:

Compare:

"fred".equals(name);

....with...

name.equals("fred");

....and see which is more likely to produce exceptions.
--
__________
|im |yler http://timtyler.org/ tim@tt1.org
Previous Topic:Deploying a servlet in Eclipse(WSAD)
Next Topic:java editor formatting problem
Goto Forum:
  


Current Time: Wed Jun 04 02:41:13 EDT 2025

Powered by FUDForum. Page generated in 0.05582 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top