C# - StyleCop - SA1121:UseBuiltInTypeAlias - 可读性规则

发布于 2024-11-06 20:08:48 字数 512 浏览 1 评论 0原文

在 StyleCop 帮助手册、SO 和 Google 上没有找到它,所以在这里;)

在使用 StyleCop 期间,我收到一条警告:

SA1121 - 使用BuiltInTypeAlias - 可读性规则

该代码使用基本的 C# 之一 类型,但不使用内置的 类型的别名。

而不是使用类型名称或 完全限定类型名称, 这些类型的内置别名 应始终使用:bool、byte、 字符、十进制、双精度、短整型、整数、 长整型、对象、sbyte、浮点数、字符串、 ushort、uint、ulong。

所以 String.Empty 是错误的(取决于上述规则),而 string.Empty 是好的。

为什么使用内置别名更好?可以字符串。 Int32、Int64(等等)在特殊情况下会使代码中的某些内容变得复杂?

Not found it in StyleCop Help Manual, on SO and Google so here it is ;)

During StyleCop use I have a warning:

SA1121 - UseBuiltInTypeAlias -
Readability Rules

The code uses one of the basic C#
types, but does not use the built-in
alias for the type.

Rather than using the type name or the
fully-qualified type name, the
built-in aliases for these types
should always be used: bool, byte,
char, decimal, double, short, int,
long, object, sbyte, float, string,
ushort, uint, ulong.

so String.Empty is wrong (depend on above rules) and string.Empty is good.

Why using built-in aliases is better? Can String. Int32, Int64 (etc.) complicate something in the code on special scenarios?

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

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

发布评论

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

评论(6

樱&纷飞 2024-11-13 20:08:48

只是澄清一下:并不是每个人都同意 StyleCop 作者的观点。 Win32 和 .NET 大师 Jeffrey Richter 在他的优秀著作 CLR via C# 中写道:

C# 语言规范指出:“就风格而言,使用关键字优于使用关键字
使用完整的系统类型名称。”我不同意语言规范;我更喜欢
使用 FCL 类型名称并完全避免使用原始类型名称。事实上,我希望
编译器甚至不提供原始类型名称并迫使开发人员使用 FCL
请输入名称。以下是我的理由:

  • 我见过许多开发人员感到困惑,不知道是否使用字符串
    或代码中的字符串。因为在 C# 中 string (关键字)完全映射到
    System.String(FCL类型),没有区别,都可以使用。相似地,
    我听到一些开发人员说 int 代表一个 32 位整数,当应用程序
    运行在 32 位操作系统上,并且当应用程序运行时它表示一个 64 位整数
    在 64 位操作系统上运行。这个说法绝对是错误的:在 C# 中,int 总是映射
    System.Int32,因此它表示一个 32 位整数,与操作系统无关
    代码正在运行。如果程序员在他们的代码中使用Int32,那么这种潜力
    混乱也消除了。

  • 在 C# 中,long 映射到 System.Int64,但在不同的编程语言中,long
    可以映射到 Int16Int32。事实上,C++/CLI 确实将 long 视为 Int32
    用一种语言阅读源代码的人很容易误解代码的含义
    他或她是否习惯于使用不同的编程语言进行编程。
    事实上,大多数语言甚至不会将 long 视为关键字,也不会编译代码
    使用它。

  • FCL 有许多方法,这些方法将类型名称作为其方法名称的一部分。为了
    例如,BinaryReader类型提供了ReadBooleanReadInt32等方法,
    ReadSingle 等,System.Convert 类型提供了诸如
    ToBooleanToInt32ToSingle 等。尽管编写以下内容是合法的
    代码中,带有 float 的行对我来说感觉很不自然,并且该行并不明显
    正确:

    BinaryReader br = new BinaryReader(...);
    浮点数 = br.ReadSingle(); // 好的,但是感觉不自然
    Single val = br.ReadSingle(); // 好的,感觉很好
    
  • 许多专门使用 C# 的程序员往往会忘记其他编程
    语言可以用来对抗 CLR,正因为如此,C# 主义逐渐渗透到了 CLR 中。
    类库代码。例如,微软的 FCL 几乎完全是用 C# 编写的,
    FCL 团队的开发人员现已将方法引入到库中,例如
    ArrayGetLongLength,返回一个 Int64 值,该值在 C# 中是 long,但不是
    其他语言(如 C++/CLI)。另一个例子是System.Linq.Enumerable
    LongCount 方法。

Just to clarify: not everyone agrees with the authors of StyleCop. Win32 and .NET guru Jeffrey Richter writes in his excellent book CLR via C#:

The C# language specification states, “As a matter of style, use of the keyword is favored over
use of the complete system type name.” I disagree with the language specification; I prefer
to use the FCL type names and completely avoid the primitive type names. In fact, I wish that
compilers didn’t even offer the primitive type names and forced developers to use the FCL
type names instead. Here are my reasons:

  • I’ve seen a number of developers confused, not knowing whether to use string
    or String in their code. Because in C# string (a keyword) maps exactly to
    System.String (an FCL type), there is no difference and either can be used. Similarly,
    I’ve heard some developers say that int represents a 32-bit integer when the application
    is running on a 32-bit OS and that it represents a 64-bit integer when the application
    is running on a 64-bit OS. This statement is absolutely false: in C#, an int always maps
    to System.Int32, and therefore it represents a 32-bit integer regardless of the OS the
    code is running on. If programmers would use Int32 in their code, then this potential
    confusion is also eliminated.

  • In C#, long maps to System.Int64, but in a different programming language, long
    could map to an Int16 or Int32. In fact, C++/CLI does treat long as an Int32.
    Someone reading source code in one language could easily misinterpret the code’s
    intention if he or she were used to programming in a different programming language.
    In fact, most languages won’t even treat long as a keyword and won’t compile code
    that uses it.

  • The FCL has many methods that have type names as part of their method names. For
    example, the BinaryReader type offers methods such as ReadBoolean, ReadInt32,
    ReadSingle, and so on, and the System.Convert type offers methods such as
    ToBoolean, ToInt32, ToSingle, and so on. Although it’s legal to write the following
    code, the line with float feels very unnatural to me, and it’s not obvious that the line is
    correct:

    BinaryReader br = new BinaryReader(...);
    float val = br.ReadSingle(); // OK, but feels unnatural
    Single val = br.ReadSingle(); // OK and feels good
    
  • Many programmers that use C# exclusively tend to forget that other programming
    languages can be used against the CLR, and because of this, C#-isms creep into the
    class library code. For example, Microsoft’s FCL is almost exclusively written in C# and
    developers on the FCL team have now introduced methods into the library such as
    Array’s GetLongLength, which returns an Int64 value that is a long in C# but not
    in other languages (like C++/CLI). Another example is System.Linq.Enumerable’s
    LongCount method.

柏林苍穹下 2024-11-13 20:08:48

如果您有自己的 StringInt32 等类型,这些类型最终可能会被用来代替 System.* -请不要这样做!

最终这是个人喜好。我在任何地方都使用别名,但我知道有些人(例如 Jeffrey Richter)建议永远不要使用它们。保持一致可能是个好主意,仅此而已。如果您不喜欢 StyleCop 规则,请将其禁用。

请注意,方法等的名称应使用框架名称而不是别名,以便与语言无关。这对于私有/内部成员来说并不那么重要,但是您也可以对私有方法采用与公共方法相同的规则。

It would only really complicate the code if you had your own String, Int32 etc types which might end up being used instead of System.* - and please don't do that!

Ultimately it's a personal preference. I use the aliases everywhere, but I know some people (e.g. Jeffrey Richter) advise never using them. It's probably a good idea to be consistent, that's all. If you don't like that StyleCop rule, disable it.

Note that names of methods etc should use the framework name rather than the alias, so as to be language-neutral. This isn't so important for private / internal members, but you might as well have the same rules for private methods as public ones.

幻梦 2024-11-13 20:08:48

因为内置别名是用该语言表达概念的更自然的方式。

有些文化说足球,有些文化说足球。哪一种更合适取决于上下文。

Because the built in alias is a more natural way to express the concept in that language.

Some cultures say soccer, others say football. Which one is more appropriate depends on the context.

晌融 2024-11-13 20:08:48

此 StyleCop 规则假设使用别名可以减少所谓的“普通语言用户”的困惑。例如,它知道“long”类型,但不知何故害怕“System.Int64”类型,并且在看到它时感到困惑。就我个人而言,我认为保持代码风格一致很重要,不可能让所有人满意

This StyleCop rule supposes that using aliases introduces less confusion to the so-believed-to-exist "average language user". Which knows, for example, 'long' type, but somehow scary of 'System.Int64' type and gets confused then sees it. Personally I think it's importatnt just to be consistent in your code style, it's impossible to satisfy everyone.

玩世 2024-11-13 20:08:48

不那么混乱?对于我来说,对于传统上只是一个值的基本数据类型来说,包含静态函数似乎非常尴尬。我知道如果您只是存储一个值,则使用等效的基本数据类型,但是对于访问类的成员,在基本类型名称后面放置一个 .(点)似乎非常尴尬。

Less confusing? It seems very akward to me for a base data type, which traditionally is just a value, to include static functions. I understand using the base data type equivalent if you're just storing a value, but for accessing members of the class, it seems very akward to put a .(dot) after the base type name.

公布 2024-11-13 20:08:48

一个类比可能会有所帮助:字符串System.String的关系就像步枪与步枪一样。 字符串是一种古老语言的遗物,是为老程序员提供的。 C# 没有“内置”数据类型,这些别名是为无法理解此概念的一代“C”程序员提供的。

An analogy might help: string is to System.String as musket is to rifle. string is a relic of an old language and is provided for old programmers. C# has no "built-in" datatypes and these aliases are provided for a generation of 'C' programmers who have trouble with this concept.

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