This may not be the correct place for this question, if not feel free to move it. I tagged as Delphi/Pascal because it’s what I am working in atm, but this could apply to all programming I guess.
Anyway I am doing some code cleanup and thinking of moving all the strings in my program to a separate single .pas file. Are there any pros and cons to doing this? Is it even worth doing?
To clarify: I mean that I will be creating a separate file, Strings.pas in it I will make all my text string variables.
Ex
Current Code
Messages.Add('The voucher was NOT sent to ' + sName+
' because the application is in TEST MODE.');
Messages.Add('Voucher Saved to ' + sFullPath);
Messages.Add('----------------------------------------------------------');
New Code would be something like:
Messages.Add(sMsgText1 + '' + sName + '' + sMsgText2 + '' + sFullPath)
The Strings.pas file would hold all the string data. Hope that makes better sense
Moving your strings to a separate file is a good idea! It keeps them together and will let you easily change them if required. Your question doesn’t say you want to be able to translate them, but centralizing will help that to.
But, code like:
is not better than code like:
You’ve turned a messy but readable function call into a messy and un-readable function call. With the old code (the second snippet just above), you can read the code and see roughly what the message is going to say, because a lot of it is there in text. With the new code, you can’t.
Second, the reason for moving the strings to to keep related items together and make it easier to change them. What if you want to change the above message so that instead of saying “The file ‘foo’ in path ‘bar’…” it is phrased “The file bar\foo is…”? You can’t: the way the messages are built is still fixed and scattered throughout your code. If you want to change several messages to be formatted the same way, you will need to change lots of individual places.
This will be even more of a problem if your goal is to translate your messages, since often translation requires rephrasing a message not just translating the components. (You need to change the order of subitems included in your messages, for example – you can’t just assume each language is a phrase-for-phrase in order substitution.)
Refactor one step further
I’d suggest instead a more aggressive refactoring of your message code. You’re definitely on the right track when you suggest moving your messages to a separate file. But don’t just move the strings: move the functions as well. Instead of a large number of
Messages.Add('...')scattered through your code, find the common subset of messages you create. Many will be very similar. Create a family of functions you can call, so that all similar messages are implemented with a single function, and if you need to change the phrasing for them, you can do it in a single spot.For example, instead of:
have a single function:
You get:
ItemNotFount(item, path), which leads toSounds good to me 🙂