我们应该将格式字符串存储在资源中吗?
对于我当前正在进行的项目,我必须将特殊格式的字符串传递给第三方服务进行处理。 因此,我正在像这样构建字符串:
string someString = string.Format("{0}{1}{2}: Some message. Some percentage: {3}%", token1, token2, token3, number);
而不是对字符串进行硬编码,我正在考虑将其移动到项目资源中:
string someString = string.Format(Properties.Resources.SomeString, token1, token2, token3, number);
在我看来,第二个选项不如第一个选项可读,即阅读代码的人必须调出字符串资源才能算出最终结果应该是什么样子。
我该如何解决这个问题? 在这种情况下,硬编码格式字符串是必要的邪恶吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我确实认为这是一种必要的罪恶,我经常使用它。 我所做的一些臭事情是:
......至少在代码足够稳定之前,我可能会因为被其他人看到而感到尴尬。
I do think this is a necessary evil, one I've used frequently. Something smelly that I do, is:
..at least until the code is stable enough that I might be embarrassed having that seen by others.
您想要这样做的原因有很多,但唯一重要的原因是您要将应用程序本地化为另一种语言。
如果您使用资源字符串,则需要记住以下几点。
尽可能在您想要本地化的资源字符串集中包含格式字符串。 这将允许翻译人员重新排序格式化项目的位置,使它们更好地适应翻译文本的上下文。
避免在格式标记中使用您所用语言的字符串。 最好使用
这些是数字。 例如,消息:
“您指定的值必须介于 {0} 和 {1} 之间”
如果 {0} 和 {1} 是诸如 5 和 10 之类的数字,那就太好了。如果您使用诸如“五”和“十”之类的字符串进行格式化,这将使本地化变得困难。
您可以通过简单地命名您的资源来解决您正在谈论的可读性问题。
string someString = string.Format(Properties.Resources.IntegerRangeError, minValue, maxValue );
评估您是否在代码中以正确的抽象级别生成用户可见的字符串。 一般来说,我倾向于将所有用户可见的字符串尽可能分组在最接近用户界面的代码中。 如果某些低级文件 I/O 代码需要提供错误,则应该使用您在应用程序中处理的异常和一致的错误消息来执行此操作。 这还将合并所有需要本地化的字符串,而不是将它们散布在整个代码中。
There are several reasons that you would want to do this, but the only great reason is if you are going to localize your application into another language.
If you are using resource strings there are a couple of things to keep in mind.
Include format strings whenever possible in the set of resource strings you want localized. This will allow the translator to reorder the position of the formatted items to make them fit better in the context of the translated text.
Avoid having strings in your format tokens that are in your language. It is better to use
these for numbers. For instance, the message:
"The value you specified must be between {0} and {1}"
is great if {0} and {1} are numbers like 5 and 10. If you are formatting in strings like "five" and "ten" this is going to make localization difficult.
You can get arround the readability problem you are talking about by simply naming your resources well.
string someString = string.Format(Properties.Resources.IntegerRangeError, minValue, maxValue );
Evaluate if you are generating user visible strings at the right abstraction level in your code. In general I tend to group all the user visible strings in the code closest to the user interface as possible. If some low level file I/O code needs to provide errors, it should be doing this with exceptions which you handle in you application and consistent error messages for. This will also consolidate all of your strings that require localization instead of having them peppered throughout your code.
为了帮助添加硬编码字符串,甚至加快向资源文件添加字符串的速度,您可以做的一件事是使用 CodeRush Xpress,您可以在此处免费下载:http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/
编写字符串后,您可以访问 CodeRush 菜单并解压一步即可生成资源文件。 很不错。
Resharper 具有类似功能。
One thing you can do to help add hard coded strings or even speed up adding strings to a resource file is to use CodeRush Xpress which you can download for free here: http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/
Once you write your string you can access the CodeRush menu and extract to a resource file in a single step. Very nice.
Resharper has similar functionality.
我不明白为什么在程序中包含格式字符串是一件坏事。 与传统的未记录的幻数不同,它的作用乍一看很明显。 当然,如果您在多个位置使用格式字符串,那么它绝对应该存储在适当的只读变量中以避免冗余。
我同意将其保留在资源中是不必要的间接。 一个可能的例外是,如果您的程序需要本地化,并且您正在通过资源文件进行本地化。
I don't see why including the format string in the program is a bad thing. Unlike traditional undocumented magic numbers, it is quite obvious what it does at first glance. Of course, if you are using the format string in multiple places it should definitely be stored in an appropriate read-only variable to avoid redundancy.
I agree that keeping it in the resources is unnecessary indirection here. A possible exception would be if your program needs to be localized, and you are localizing through resource files.
是的你可以
新让我们看看
每次
我使用 ResXManager
yes you can
new lets see how
this will gave me dynamic message every time
by the way I'm useing ResXManager