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?
Please sign in to leave a comment.
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...
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:)
>
the hard coded values and replace them with my new constant?
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?
"charles decroes" <spam@decroes.com> wrote in message
news:14227847.1054317343397.JavaMail.jrun@is.intellij.net...
Is this close to what you're looking for?
http://www.intellij.net/tracker/idea/viewSCR?publicId=7356
Erik
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...
it would look at your string constants and see if those same constants are
in an interface that you are currently implementing?
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
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.
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... :)
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
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?
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
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! (_);
"Chris Laprun" <chris.laprun@nist.gov> wrote in message
news:31656023.1054330530915.JavaMail.jrun@is.intellij.net...
IDEA could just show you a list. Of course, the list might be pretty long
for "0"...
Maybe a new refactoring: "replace constants with typesafe enum", or "replace
constants with Java 1.5 enum".
Erik Hanson
Yep...
Good idea.
Best,
Chris