2

for example, sometimes I need to define a hardcoded string with some variable parts, I often have trouble to choose the style:

style 1 : reuse every characters when possible

showMessage(num){
  let strArr=['Mon','Tues','Wednes','Thurs','Fri','Satur','Sun'];
  alert('Today is '+strArr[num]+'day');
}

style 2: Keep the varible part as a whole world when possible

showMessage(num){
  let strArr=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
  alert('Today is '+strArr[num]);
}

style 3: each individual case have their string even if they have some similar part:

showMessage(num){
  let strArr=[
    'Today is Monday',
    'Today is Tuesday',
    'Today is Wednesday',
    'Today is Thursday',
    'Today is Friday',
    'Today is Saturday',
    'Today is Sunday',
  ];
  alert(strArr[num]);
}

which style should I use?

  • 1
    The first one looks strange to me, I probably wouldn't join parts of words even if they're all the same. The last is clear but requires more work if you want to change the first part of the string (which wouldn't matter for such a small array, but would if it was much larger). I'd go with style 2. – Turksarama Nov 1 '18 at 3:46
  • 2
    @Turksarama Agreed, style 2 is also more robust towards translation into other languages. – Omegastick Nov 1 '18 at 8:23
  • @Omegastick, third option is more translation friendly, because different languages can have different phrase structures based on the variable value. – Fabio Nov 7 '18 at 20:20
  • @Fabio Yeah, I guess it's a trade-off with 1 being the most concise, 3 being the most robust, and 2 somewhere in the middle. – Omegastick Nov 8 '18 at 1:22
  • style 4: Remove "Today is" and display only name of the weekday. I bet the fact that it is today will be obvious from the surrounding context where you displaying the text. Then your code will look simpler a more translation friendly ;) – Fabio Nov 8 '18 at 2:13
3

As we are living in a more and more global world, you should really consider how easy it is to translate your texts to other languages. This almost automatically disqualifies your first option, as there will almost certainly be an (important) language that does not have a common part between all possibilities of the dynamic part.

The second option (literally with string concatenation) is also problematic for translations, as different languages will have different word orders and different positions within the text to place the dynamic part.

The third option is reasonable, but not very friendly for maintenance due to the repetitions of the common parts.

A fourth option that is both maintenance friendly and translation friendly is to use string replacements.

showMessage(num){
  let strArr=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
  alert('Today is {0}'.format(strArr[num]));
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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