The Perils of Format

The Perils of Format

Format is good. Format is bad.

The good is that you can create very nice translatable strings which can deal with reordered parameters, and that perform fairly well.

if Language = Yoda

then fmt := ‘%1:s apples, %0:d you took.’

else fmt := ‘you took %0:d %1:s apples.’; // English

Writeln(Format(fmt, [5, ‘green’]));

So, what is the pain?  All the checks are done at run time.

Let me repeat that: … at run time.  

Once again, during a hotfix, I blew it on the parameter type, mistakenly passing a Double into the position where an Integer was expected.

I really wish that there was compiler magic that could validate the type and position of the Format parameters at compile time.  Until there is, I will be careful to send all parameters as strings, and convert to string in the Format argument list – at least for the quick fixes.

 

13 thoughts on “The Perils of Format


  1. Or just ignore the format… In mORMot we use plain % instead of %d or %s or %g and it is sufficient in almost all cases. And also safer (less runtime error) and faster (no need to manually convert to string).


  2. A. Bouchez How is % interpreted – does it change dynamically at runtime depending on the type that’s actually passed? How does handle (for example) float formatting flags?


  3. I always wondered, why format doesn’t determine the parameter type and simply converts it to string. It already does the checks to be able to generate a meaningful error message, so why not go all the way? I even started to implement this at one time but got sidetracked somehow. Maybe I still have got the sources of that attempt.


  4. FWIW it’s something I added in DWS & is experimental in the Smart JS codegen: constant format strings are type-checked at compile-time.


    It’s also something I added to the in-house translation utility, to ensure format strings translations are correct.


    A. Bouchez You need to specify at least the argument index, otherwise it’s not translation-friendly.


    Thomas Mueller Jeroen Wiert Pluimers you need all the parameters in the format string for a truly translation-friendly format string, as they may have to be adjusted to locale or specific preferences…


  5. I changed all to %s a decade ago.


    But anyway it will blowup when a translator will think it is clever and can shorten the phrase. Obviously with little % characters.


    So I have a function to compare the number of % character occurrences that I must run always after I receive translation.

Leave a Reply