简体   繁体   English

“数组初始化程序中的冗余逗号”为什么应该修复它

[英]“redundant comma in array initializer” why it should be fixed

With ReSharper 6.0 I've received a new warning: 使用ReSharper 6.0,我收到了一条新警告:

redundant comma in array initializer

for the case when I have a list of array initializer parameters with comma in the end. 对于我最后有逗号的数组初始化参数列表的情况。

What bad in that? 那有什么不好的? Why I like "my" approach is that I often need to comment the latest value and don't want to bother with fixing those 'leading' commas: 为什么我喜欢“我的”方法是我经常需要评论最新的价值,并且不想打扰那些“领先”的逗号:

    private readonly string[] _tables
        = new[]
              {
                  "users",
                  "user_account",
                  "user_budget_type",
                  //"user_budget"
              };

Please advise what is good in following that suggestion. 请告知遵循该建议有什么好处。

Thank you. 谢谢。

I agree that it shouldn't matter that there is a comma at the end, however, if you write your arrays like so, you don't have this problem. 我同意最后有一个逗号并不重要,但是,如果你这样编写你的数组,你就没有这个问题。

private readonly string[] _tables
    = new[]
          {
              "users"
              ,"user_account"
              ,"user_budget_type"
              //,"user_budget"
          };

ReSharper is full of suggestions. ReSharper充满了建议。 Just like any other productivity tool, in the end it's up to you whether to act upon those suggestions. 就像任何其他生产力工具一样,最终由您来决定是否采取这些建议。

For example, while the C# language allows a trailing comma or an empty control statement bodies (placing a ; for a loop body), or even typing duplicate ;; 例如,虽然C#语言允许尾随逗号或空控制语句体(放置一个;用于循环体),或者甚至键入重复;; at the end of the statement. 在声明的最后。 For all those things ReSharper will suggest you removing redundancies, because it's what it does - heuristically detects redundancies. 对于所有这些事情,ReSharper会建议你删除冗余,因为它是它的作用 - 启发式检测冗余。 If you know better, there are several ways you could "silence" ReSharper from suggesting it: 如果你知道的更好,有几种方法你可以“沉默”ReSharper建议它:

  1. Temp. 温度。 suppress the warning using special comments (choose the "Suppress inspection ... with comment" quickfix): 使用特殊注释抑制警告(选择“抑制检查...带注释”quickfix):

     private readonly string[] _tables = new[] { "users", "user_account", // ReSharper disable RedundantCommaInArrayInitializer "user_budget_type", // ReSharper restore RedundantCommaInArrayInitializer //"user_budget" }; 

or 2. Suppress this warning completely by selecting "Inspection options for ...", and choosing "Do not show" in the options. 或2.通过选择“检查选项...”并在选项中选择“不显示”来完全取消此警告。

It's all about choice and personal preference - you can configure just about anything you want in the tool. 这完全取决于选择和个人偏好 - 您可以在工具中配置您想要的任何内容。

The clue is in the text of the message: 线索在消息的文本中:

redundant comma in array initializer 数组初始值设定项中的冗余逗号

What does it mean by that? 这是什么意思? Well, your code: 那你的代码:

private readonly string[] _tables
    = new[]
          {
              "users",
              "user_account",
              "user_budget_type",
              //"user_budget"
          };

and the code with the inspection acted upon: 并且检查的代码采取了行动:

private readonly string[] _tables
    = new[]
          {
              "users",
              "user_account",
              "user_budget_type"
              //"user_budget"
          };

are semantically equivalent . 语义上是等价的 That is, the CIL produced in each case is identical - there is no difference in behaviour. 也就是说,在每种情况下产生的CIL是相同的 - 行为没有差异。 So the comma changes nothing and is therefore redundant. 所以逗号没有任何改变,因此是多余的。

Now, as to your question: 现在,关于你的问题:

What bad in that? 那有什么不好的?

That's largely up to you. 这在很大程度上取决于你。 R# merely has this redundancy as an inspection , but it is you (through your acceptance of the default settings) that has it categorised as a warning . R#仅作为检查具有此冗余,但是您(通过接受默认设置)将其归类为警告

If this construct is acceptable to your style, then by all means go ahead and change the Inspection Severity in R# options; 如果这个结构对你的风格是可以接受的,那么一定要继续并改变R#选项中的Inspection Severity; but in general , the defaults for R# options are those that produce pretty close to the minimum amount of code necessary. 一般来说 ,R#选项的默认值是那些产生非常接近所需最小代码量的选项。

你可以压制那个警告,Resharper只是说,以防万一你在乎:)

@Budda, Unlike other have said, this redundant comma is eval! @Budda,与其他人说的不同,这个冗余的逗号 eval! In some browsers, for example IE8, this could cause the browser to misunderstand you and mis-create the object. 在某些浏览器中,例如IE8,这可能会导致浏览器误解您并错误地创建对象。

See this answer for example, where the trailing comma caused the Highcharts library to stop working. 例如, 请参阅此答案 ,其中尾随逗号导致Highcharts库停止工作。

Keeping your code clean is not only a matter of easier reading and debugging. 保持代码清洁不仅是一个更容易阅读和调试的问题。 Even if you see your code working with this comma, it doesn't mean that it will be working the same with all browsers. 即使您看到您的代码使用此逗号,也不意味着它将与所有浏览器一样工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在集合初始化器的末尾有逗号? - Why can you have a comma at the end of a collection initializer? 无法理解为什么无法初始化对象初始化程序中的数组初始化程序 - Can't understand why array initializer in object initializer cannot compiled 为什么将Resharper的“冗余字段初始化程序”的默认检查严重性设置为“警告” - Why is Resharper's default inspection severity for “redundant field initializer” set to “warning” 为什么数组初始化器不能在局部变量上工作? - Why does an array initializer not work on a local variable? MSIL:为何数组初始化程序使用dup - MSIL: Why does Array initializer use dup 为什么我不能在没有初始化器的情况下声明隐式类型的数组? - Why can't I declare an implicitly typed array without an initializer? 为什么我不能将数组初始值设定项与隐式类型变量一起使用? - Why can't I use the array initializer with an implicitly typed variable? 为什么这是多余的? - Why is this cast redundant? 为什么“Assert”类有这么多看似多余的方法? 什么时候应该使用? - Why does the “Assert” class have so many seemingly redundant methods? When should each be used? 嵌套数组初始化器是预期的 - nested array initializer is expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM