sebi
sebi

Reputation: 1841

Intellij IDEA String handling features and retriving messages from properties files

I have recently stumbled upon a neat feature in Intellij IDEA that has let me to question one of my practices. It's the String auto-complete. Basically, if I define a key-value pair in a properties file, and then begin typing a String in java code that has the save value as the key in the properties files, IDEA can auto-complete it. More, I can navigate to it with ctrl+click and can refactor it!

The practice that I was talking about is related to displaying a value from the properties file. I am currently using an enum for this, whose types have the same name as the keys in the properties file. I was doing this because I gained type checking and refactoring. But it seems that I can have the same benefits just by using strings in IDEA (well, it doesn't really give me type checking, but it's kind of close).

I was wondering if any of you are using simple String values for retrieving messages. Is this a good practice?

Upvotes: 2

Views: 328

Answers (3)

NimChimpsky
NimChimpsky

Reputation: 47300

Its probably better to use an Enum and to make everyone you work with use intellij, if they complain show them this question (and many others).

Also worth bearing in mind that some string values require compile time constants , which makes things a bit more complex.

Upvotes: 0

Denis Tulskiy
Denis Tulskiy

Reputation: 19187

A compromise would be to have an enum with properties where enum value is the property string. This way you get type safety and IDEA will recognize that enum value comes from property and let you easily navigate to it.

Upvotes: 1

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

I don't think it's a good practice. You shouldn't depend on your IDE when you're developing application. If somebody elses uses e.g. eclipse he has a chance to mess up all this.I like solution with enums more than string only because it gives compile time checks. You can even build you enum so it also reads properties file and every item in enum contains both key and value from property file.

Upvotes: 1

Related Questions