views:

319

answers:

5

By default I use string constants in my code when I have a string of text that will be output to the screen via a messagebox, label, etc. My main reason for doing so is that I do not want these strings to change at runtime and this way nobody really touches them.

My question is:

It seems that from a memory point of view, this approach is creating an object whose lifetime will be decided by the GB whereas a string literal seems more efficient and will be disposed of more quickly. I know this is a rookie question but is there a benefit (no matter how tiny) when using a literal over a constant or should a constant be used in such scenarios.

I know it will not make any noticeable difference in my apps but I am trying to improve myself as a dev and build standards that I keep a reasonable amount of the time. :o)

+8  A: 

Neither. The recommended way AFAIK is to use a resources file.

Otávio Décio
I agree. Forget literals and constants. Definitely use a resource file. That way the strings are kept externally and are language independent.
Simon P Stevens
Depending, of course, on the context. No need to SOFT_CODE: http://en.wikipedia.org/wiki/Softcoding =)
Chris Cooper
+1  A: 

Prefer string constants to string literals, but the reason is to make your code more maintainable, not to improve its performance.

Bill the Lizard
Amen. Maintainability will pay off ten times more than a micro-optimization like the one proposed by the OP.
JohnFx
+2  A: 

You should look into "string interning". Basically, all of the unique string literals (constant or inline) in any of your source files will be stored in a special table. They will never be garbage collected, and may benefit from re-use.

You might want to look into using a resources file if you will ever be changing those strings without wanting to recompile (eg for internationalisation)

Rob Fonseca-Ensor
the upshot of string interning is that you should do whatever is most maintainable
Rob Fonseca-Ensor
+1  A: 

It would depend on whether you need these strings to be localizable or not. If you need the ability to translate your software into other languages, then I would place as many strings that are displayed to an end user as you can in resource files. If you do not need localization, or if the strings in question are never displayed to an end user, I would use constants. If your strings are shared amongst multiple classes, I would create classes that wrap up common strings that are provided as public constants. Unless these strings need to be used outside of the assembly they are defined in, keep them at the lowest privilege level possible (private, protected, internal).

jrista
+1  A: 

For internationalization purposes, it's true that the accepted best practice/recommendation is a set of resource files.

I am following the same pattern of using string constants in their own class. I find it's easier to create consts that'll be used on multiple pages throughout the web app. Even a const with placeholders helps!

  //{1} for the user's full name for example
const string SOME_LABEL = "Thanks for purchasing {0} products, {1}!";
p.campbell