我们应该在资源中存储格式字符串吗? [英] Should we store format strings in resources?

查看:115
本文介绍了我们应该在资源中存储格式字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我当前正在进行的项目,我必须将特殊格式的字符串传递给第三方服务进行处理.因此,我正在像这样构建字符串:

For the project that I'm currently on, I have to deliver specially formatted strings to a 3rd party service for processing. And so I'm building up the strings like so:

string someString = string.Format("{0}{1}{2}: Some message. Some percentage: {3}%", token1, token2, token3, number); 

不是硬编码字符串,而是考虑将其移入项目资源:

Rather then hardcode the string, I was thinking of moving it into the project resources:

string someString = string.Format(Properties.Resources.SomeString, token1, token2, token3, number); 

我认为第二个选项不如第一个可读性高,即,阅读代码的人将不得不拉起字符串资源来确定最终结果应该是什么样.

The second option is in my opinion, not as readable as the first one i.e. the person reading the code would have to pull up the string resources to work out what the final result should look like.

我该如何解决?在这种情况下,硬编码格式字符串是否必不可少?

How do I get around this? Is the hardcoded format string a necessary evil in this case?

推荐答案

我确实认为这是一种必不可少的邪恶,我经常使用它.我做的有臭味的是:

I do think this is a necessary evil, one I've used frequently. Something smelly that I do, is:

// "{0}{1}{2}: Some message. Some percentage: {3}%"
string someString = string.Format(Properties.Resources.SomeString
                                  ,token1, token2, token3, number);

..至少要等到代码足够稳定为止,否则我可能会被别人看到的情况感到尴尬.

..at least until the code is stable enough that I might be embarrassed having that seen by others.

这篇关于我们应该在资源中存储格式字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆