我应该抑制 CA2204:文字应该拼写正确吗?

发布于 2024-09-02 03:58:18 字数 712 浏览 2 评论 0原文

我最近将我的项目从 Visual Studio 2008 升级到 Visual Studio 2010。

通过启用代码分析,我收到了很多导致规则 CA2204 的警告:文字应该拼写正确。

编辑

假设我有一个名为 GetResult() 的方法,并且由于某种原因我想在其中抛出异常。我希望异常显示“GetResult() 由于某种原因失败”。这会给我警告,因为 GetResult 不是一个单词。仅当我将方法名称 GetResult() 放入字符串中时,我才不会收到警告。这是因为 Get 和 Result 是合法的词。

我不认为编写 GetResult() 由于某种原因失败 是解决方案。

编辑:在MSDN中,它说:

该规则解析文字字符串 转化为单词,标记复合词, 并检查每个的拼写 单词/标记。

这是否意味着 GetResult 应该检查为两个词:“Get”和“Result”?

我应该抑制 CA2204 吗?

I've recently upgraded my project from Visual Studio 2008 to Visual Studio 2010.

By enabling Code Analysis, I'm getting a lot of warnings resulted in rule CA2204: Literals should be spelled correctly.

EDIT:

Let's say I have a method called GetResult(), and in it I want to throw an exception for some reason. I want the exception to say "GetResult() has failed for some reason". This will give me the warning since GetResult isn't a word. I won't get a warning on the method name GetResult(), only if I put it in a string. This is because Get and Result are legal words.

I don't believe that writing GetResult() has failed for some reason is the solution.

EDIT: In MSDN it says:

This rule parses the literal string
into words, tokenizing compound words,
and checks the spelling of each
word/token.

Doesn't that mean that GetResult should be checked as two words: "Get" and "Result"?

Should I suppress CA2204?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

守望孤独 2024-09-09 03:58:19

CA2204 与拼写有关。不幸的是,即使在 VS2012 中,它仍然有一个令人烦恼的旧错误,无法标记复合词: http: //connect.microsoft.com/visualstudio/feedback/details/521030

这导致我在代码分析规则集中关闭了此规则。

CA2204 is all about spelling. Unfortunately even in VS2012 it still has an old annoying bug where it fails to tokenize compound words: http://connect.microsoft.com/visualstudio/feedback/details/521030

This caused me to switch off this rule in our code analysis rule set.

雪化雨蝶 2024-09-09 03:58:18

“无法初始化 MyClass” 对于开发人员来说,在代码中引入并不是一个好消息。它很少有助于调试,而且如果它被显示的话只会让最终用户感到困惑。

一般来说,我想说不要隐藏该消息,因为拼写错误会让人们看起来比实际情况愚蠢得多,而且这不是您想要通过应用程序传达的消息。

在这个特定实例中,这实际上是一个错误消息的警告——要么告诉用户如何纠正它,自动纠正它,要么在错误日志中包含它没有初始化的实际原因。

编辑:包括OP的编辑
您可以从此警告中了解到,您不应该在错误消息中透露代码详细信息(主要是因为当您记录异常时它们将包含在调用堆栈中)。

GetResult() 由于某种原因失败
假设“某种原因”是权限。消息内容如下:

您无权查看这些结果。

无需提及失败的具体方法,因为可以自动记录堆栈跟踪。

"Can't initialize MyClass" is not a good message for a developer to introduce into code. It rarely aids in debugging and it only confuses the end user if it's ever displayed.

In general, I would say don't suppress the message because spelling error makes one look much dumber than they really are and that's not a message you want to convey with your app.

In this specific instance, it's really a warning of a poor error message -- either tell the user how to correct it, correct it automatically, or include the actual reason it isn't initializing in your error log.

EDIT: Including OP's edits
Something you can take from this warning is that you shouldn't be revealing code details as part of an error message (primarily because they will be included in the call stack when you log the exception).

GetResult() has failed for some reason
Let's say "some reason" is permissions. The message could read:

You don't have permission to view these results.

There's no need to mention the specific method that failed because the stack trace can be logged automatically.

太阳哥哥 2024-09-09 03:58:18

解决此问题的一种方法是不直接将类型名称添加到字符串中。而是将其作为参数传递。例如,

var msg = String.Format("Can't initialize {0}", typeof(MyClass).Name);

这样做的好处是既可以绕过 FxCop 规则,又可以安全地进行重构。

One way to fix this is to not add the type name into the string directly. Instead pass it as a parameter. For example

var msg = String.Format("Can't initialize {0}", typeof(MyClass).Name);

This has the benefit of both getting around the FxCop rule and being safe for refactoring.

柠檬心 2024-09-09 03:58:18

也许你不应该将类名放入文字中?如何使用或定义可以像这样抛出的异常:

throw new CantInitializeClassException(innerException, typeof(MyClass);

我的想法是将更多信息从更一般的异常转移到更具体的异常。我建议使用上面的示例,而不是 throw new ApplicationException("Cantinitialize MyClass");

May be you should not put class name into literal? What about using or defining exception that can be thrown like this:

throw new CantInitializeClassException(innerException, typeof(MyClass);

My idea was to move more information to more specific exceptions from more general ones. I suggest using the sample above instead of throw new ApplicationException("Cant initialize MyClass");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文