Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This is a question about preferences.

Assume a programming language offers these two options to make a string with some variables:

  1. "Hello, my name is ". name ." and I'm ". age ." years old."
  2. StringFormat("Hello, my name is $0 and I'm $1 years old.", name, age)

Which do you prefer and why?

I have found myself using both without any clear reason to pick either. Considering micro-optimizations is not within the scope of this question.

Localization has been mentioned as a reason to go with option #2 and I think it's a very valid reason and deserves to be mentioned here.

However, would opinions differ based on aesthetic viewpoints?

share|improve this question

closed as primarily opinion-based by Matthew Flynn, gnat, GlenH7, Michael Kohne, Dan Pichelman Nov 1 '13 at 16:46

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

When the question was generic, there were valid technical reasons why to prefer one over the other, making the question on-topic. But aesthetic reasons are by definition purely subjective and as such the question is off-topic on this site. Upvote retracted. –  Jan Hudec Nov 4 '13 at 14:16

1 Answer 1

Formatting, because of localization.

If you write anything for which there is any risk it might be published, it is likely you'll have to localize (translate) it. Than it will pay off if you write the strings in localizable format from the start, because it will be pain to update otherwise.

Basically if you write

"Hello, my name is" . name . " and I'm " . age . " years old"

than the translator will see these 3 strings:

"Hello, my name is"
"and I'm"
"years old."

and they

  1. Make absolutely no sense to the translator, who often does not know the program well. Most of the time they never see it, especially when commercial software is translated by translation agency.
  2. There is some language somewhere where the word order has to be different. In example where there is a string before, between and after all values as above, the words can be reordered. Provided the translator actually knows the bits go together, which they don't. But if there is a value at the beginning or end, you're in trouble.

For more detailed discussion and somewhat better examples, see GNU Gettext manual.

Note: When I write quick private helper scripts and such I also mix the forms, usually depending on which one is more convenient in the language I am using at the moment. But in anything that might get distributed localization is important topic and some of the problems are often non-obvious to native English speakers. So I'd really recommend at least skimming the Gettext manual to all programmers.

share|improve this answer
This is a very valid reason and one I hadn't considered yet. I don't come across localization often during my work so for me that's not a big issue. –  Davio Nov 1 '13 at 12:53
@Davio: Localization issues are often non-obvious to native English speakers and even to many other programmers who never tried to translate anything. We currently have 22 translations in our software and I am about to do a large refactoring of string formatting that will include fixing large number of strings that were not using formatting properly, because even some here won't do it right even though we are not native English speakers and we had three languages right from the start. –  Jan Hudec Nov 1 '13 at 13:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.