Replace string with constants

We had some field names hard coded in multiple places. I used introduce constant. Idea created the constants for me and replaced the one usage that I had selected. Later I moved them to an interface, or rather idea did for me:)

My Question: Is there a way to get idea to search my project and find all the hard coded values and replace them with my new constant?

12 comments
Comment actions Permalink

There is currently no such feature.

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"

"charles decroes" <spam@decroes.com> wrote in message
news:3437654.1054296159863.JavaMail.javamailuser@localhost...

We had some field names hard coded in multiple places. I used introduce

constant. Idea created the constants for me and replaced the one usage that
I had selected. Later I moved them to an interface, or rather idea did for
me:)
>

My Question: Is there a way to get idea to search my project and find all

the hard coded values and replace them with my new constant?


0
Comment actions Permalink

Does anyone think that would be a good idea? Maybe in the analyze code if it would look at your string constants and see if those same constants are in an interface that you are currently implementing?

0
Comment actions Permalink

"charles decroes" <spam@decroes.com> wrote in message
news:14227847.1054317343397.JavaMail.jrun@is.intellij.net...

Does anyone think that would be a good idea? Maybe in the
analyze code if it would look at your string constants and
see if those same constants are in an interface that you
are currently implementing?


Is this close to what you're looking for?

http://www.intellij.net/tracker/idea/viewSCR?publicId=7356


Erik


0
Comment actions Permalink

I don't think that's a good idea. Actually, I don't think that implementing
interfaces in order to make constants available is a good idea, so I'd
rather have IDEA suggesting me not to do that instead of offering to do it
more for me.

What's wrong with a search and replace, anyway?

Andrei

"charles decroes" <spam@decroes.com> wrote in message
news:14227847.1054317343397.JavaMail.jrun@is.intellij.net...

Does anyone think that would be a good idea? Maybe in the analyze code if

it would look at your string constants and see if those same constants are
in an interface that you are currently implementing?


0
Comment actions Permalink

so how do you do constants? Until 1.5 I can't statically import them. Don't tell me you hard code values all over the code :(

For instance we have keys into our datamodel. The entries are put into the model on the server and then passed to the client. The client must use the same keys to get individual elements out of the datamodel. I guess I could make public static variables in a class but that is basically the same thing. I would want intellij to tell me when i've hard coded a string when there is a constant available

0
Comment actions Permalink

sorry i forgot to answer the question about search and replace. The only problem I have with that is that sometimes I may be unaware that a constant exists. Search and replace works if I remember to do that. I already do inspections before I check in so I was thinking if it warned me it would be nice.

0
Comment actions Permalink

so how do you do constants? Until 1.5 I can't
statically import them. Don't tell me you hard code
values all over the code :(


Use a class, not an interface. Make sure that the class has a private default constructor to prevent instantiation. See item 17 in Effective Java... :)

For instance we have keys into our datamodel. The
entries are put into the model on the server and then
passed to the client. The client must use the same
keys to get individual elements out of the datamodel.
I guess I could make public static variables in a
class but that is basically the same thing. I would
want intellij to tell me when i've hard coded a
string when there is a constant available


It's not the same thing if you use interfaces... I guess it all depends wether your classes actually implements the interface to access the constants or access them via static members. In any cases, interfaces should only be used to define types.

Best,
Chris

0
Comment actions Permalink

ok well i'm fine with that. Both ways what i'm looking for is places where a constant should be used but is not. I usually do not implement interfaces I just access them via Constants.MY_CONST; but public static final variables in a unconstructable class works too.


On a side note does anyone know what the difference is in the byte code? If I put the variables in an interface vs static final variables in a class?

0
Comment actions Permalink

ok well i'm fine with that. Both ways what i'm looking for is places

where a constant should be used but is not. I usually do not implement
interfaces I just access them via Constants.MY_CONST; but public static
final variables in a unconstructable class works too.

In that case, an inspection that tells you there is a constant defined for a
string you hardcoded somewhere in the code might come in handy. It was the
part with the implemented interface I didn't quite like (and the thread
makes it clear why). If the constants were detected anywhere in the codebase
that would be nice.

Andrei


0
Comment actions Permalink

In that case, an inspection that tells you there is a
constant defined for a
string you hardcoded somewhere in the code might come
in handy. It was the
part with the implemented interface I didn't quite
like (and the thread
makes it clear why). If the constants were detected
anywhere in the codebase
that would be nice.


I agree. I meant to comment on that as well. Being able to identify spots where a constant could be used instead of a hardcoded value could be nice. On the other hand, it might not be that easy to figure out for the tool... Think about int constants for example, especially if you have several different constants with the same int value.

On the other hand, you shouldn't use int for constants if you can help it but rather a typesafe enum (which 1.5 will make even easier)... See Item 21 of Effective Java! (_);

0
Comment actions Permalink

"Chris Laprun" <chris.laprun@nist.gov> wrote in message
news:31656023.1054330530915.JavaMail.jrun@is.intellij.net...

Being able to identify spots where a constant could be used
instead of a hardcoded value could be nice. On the other
hand, it might not be that easy to figure out for the tool...
Think about int constants for example, especially if you have
several different constants with the same int value.


IDEA could just show you a list. Of course, the list might be pretty long
for "0"...

On the other hand, you shouldn't use int for constants if you
can help it but rather a typesafe enum (which 1.5 will make
even easier)... See Item 21 of Effective Java! (_);


Maybe a new refactoring: "replace constants with typesafe enum", or "replace
constants with Java 1.5 enum".


Erik Hanson


0
Comment actions Permalink

IDEA could just show you a list. Of course, the list
might be pretty long
for "0"...


Yep...

Maybe a new refactoring: "replace constants with
typesafe enum", or "replace
constants with Java 1.5 enum".


Good idea.

Best,
Chris

0

Please sign in to leave a comment.