简体   繁体   English

C#关于结束{}的评论

[英]C# comments on closing {}

I've been working a little with DevExpress CodeRush and Refactor! 我一直在使用DevExpress CodeRush和Refactor! Pro this week, and I picked up a commentor plug-in that will automatically generate comments as you type code. 在本周的Pro中,我选择了一个注释程序插件,该插件将在您键入代码时自动生成注释。

I don't want to go into how good a job it does of picking out basic meaning (pretty good, actually) but it's default implementation does raise a question. 我不想谈谈挑选基本含义(实际上很好)的工作有多么出色,但是它的默认实现确实引发了一个问题。

By default, typing a } character to close a block will result in the plugin adding a comment like the following... 默认情况下,输入}字符以关闭块将导致插件添加如下注释。

using(MyType myType = new MyType())
{
    myType.doWork();
} // using

(ie adding a comment to the closing brace labelling where it was opened.) (即,在打开括号的地方添加注释。)

While I can see that there are instances where this behaviour may be of great use, I feel that the resultant code looks very untidy with all the additional commenting. 虽然我可以看到在某些情况下此行为可能很有用,但我觉得结果代码看起来很杂乱,并带有所有其他注释。

I was wondering what other people;'s take on this kind of comment was. 我想知道其他人对这种评论的看法。 Not just from an academic standpoint, but if I get a good number of negative comments about them I can decide whether to inflict them upon my co-workers or strip them out. 不仅从学术的角度来看,而且如果我对它们有很多负面评论,我可以决定是将其强加给我的同事还是将其剔除。

I think comments like that are useless, unless of course the code is awful. 我认为像这样的注释是没有用的,除非代码很糟糕。 With proper formatting of code it's not difficult to see where a block starts and where a block ends because usually those blocks are indented. 使用正确的代码格式,不难发现块在哪里开始以及块在哪里结束,因为通常这些块是缩进的。

Edit: If a procedure is so big that is not readily apparent what block of code is being closed by a brace, then there should already be more descriptive comments describing the procedure anyways and these comments would just be clutter. 编辑:如果一个过程太大,以至于不知道哪个代码块被大括号关闭,那么无论如何应该已经有更多描述性的注释来描述该过程,而这些注释只会很混乱。

I find the idea of a plugin that genrates comments from code rather useless. 我发现一个从代码生成注释的插件的想法相当无用。 If it can be inferred by the machine then it can also be inferred by anybody reading it. 如果机器可以推断出它,那么任何阅读它的人也可以推断出它。 The comments are extremely likely to be totally redundant. 这些评论极有可能是完全多余的。

I feel that those closing brace comment is messy, it gives information that is better provided directly by the IDE if the individual wants it. 我觉得这些大括号的注释是乱七八糟的,如果个人需要,它可以提供更好地由IDE直接提供的信息。

IMO every comment that is describing what the code is already telling you is unnecessary. IMO不需要任何描述代码已经告诉您的注释。

If you really have code blocks that are so long that you have to scroll a lot to see there beginning you have done something wrong and may consider to split the code up. 如果您确实有太长的代码块,那么您必须滚动很多才能看到那里的代码,然后开始做错了什么,因此可以考虑拆分代码。

Bad bad comment style - it introduces a maintainance overhead in the codebase. 不良的不良注释样式-它在代码库中引入了维护开销。

I've known ex-VB coders who found trails of } s in C-syntax code to be confusing, but in this scenario the real fix is to refactor your code to prevent deep nesting and excessively long functions and/or code blocks. 我认识过一些前VB编码人员,他们发现C语法代码中}的痕迹令人困惑,但是在这种情况下,真正的解决方法是重构代码,以防止深度嵌套和过长的功能和/或代码块。

Maybe useful if the using block extends over a page in the IDE, but then you've got other problems to worry about. 如果using块扩展到IDE中的页面上,可能会很有用,但是您还有其他问题需要担心。 In this case I get by with proper indenting and an IDE that highlights the matching brace when I select one. 在这种情况下,我可以通过适当的缩进来实现,并且选择一个IDE时,IDE会突出显示匹配的花括号。

I give it a thumbs down in general, but with potential use when you can't avoid a long block otherwise. 我通常会对此表示反对,但是如果您不能避免长时间使用,则可以使用。

Sometimes you will get very large code blocks, or a lot of nested blocks closing together. 有时您会得到非常大的代码块,或者将许多嵌套的块放在一起。 I sometimes use this style in cases like this, but definitely not all the time. 在这种情况下,有时我会使用这种风格,但绝对不是一直如此。 I don't restrict it to code either: HTML can benefit greatly from this style of "close commenting": 我也不将其限制为代码:HTML可以从这种“关闭注释”样式中受益匪浅:

<div id="content">
    <div id="columns">
        <div class="column">

            <!-- .. snip a lot of lines .. -->

        </div> <!-- .column -->
    </div> <!-- #columns -->
</div> <!-- #content -->

This sort of comments are only usefull on very long blocks of code where you have many nested blocks. 此类注释仅对具有许多嵌套块的非常长的代码块有用。 But that said this should not be the case in the first place as many nested blocks and long methods call for refactoring. 但这并不是说,首先应该是这样,因为许多嵌套块和长方法都需要进行重构。 So I don't like this at all, as the reader obviously can see what block of code it is. 所以我根本不喜欢这样,因为读者显然可以看到它是什么代码块。

I think more useful than comments would be an IDE feature to not only highlight matching pairs of braces, but also display the openining line on a tooltip, so that if you hovered over the closing brace in your example it would come up with "using(MyType myType = new MyType())" in a tooltip. 我认为,IDE功能比注释更有用,它不仅可以突出显示匹配的花括号对,还可以在工具提示上显示打开行,因此,如果在示例中将鼠标悬停在右花括号上,它将出现“工具提示中的MyType myType = new MyType())”。

This would enable you to easily make sense of complex nested braces for large functions without providing constant visual clutter. 这将使您能够轻松理解大型功能的复杂嵌套括号,而无需提供持续的视觉混乱。

I always find it useful to remember this... 我总是觉得记住这一点很有用...

Clear, well written code will provide enough of an explanation of what the code is doing for a competent programmer to pick it up. 清除,写得很好的代码将提供足够的什么样的代码是做一个称职的程序员解释把它捡起来。

Comments should be left in the code to explain why the code is doing it! 代码中应留有注释,以解释代码为什么这样做!

In other words, use comments to help the reader of your code understand the algorithm, or what the code is supposed to achieve , not how it's achieving it! 换句话说,使用意见,以帮助你的代码的读者理解算法,或什么的代码应该实现 ,而不是它是如何实现它!

You might want to check out this post by Jeff Atwood . 您可能想看看Jeff Atwood的这篇文章

不要这样做,如果在整个地方使用它,只会增加噪音,此外,适当的缩进还应解决可读性问题。

I would keep it turned off. 我会保持关闭状态。 I only see a point in using this when you have multiple blocks ending in the same place (longer or shorter blocks) - I've used them myself in some cases like these. 当您有多个以相同位置结尾的块(较长或较短的块)时,我只会看到使用它的要点-在这种情况下,我自己使用了它们。 However if they are used it would be better to add them manually, in carefully selected places rather than having some automated tool adding them. 但是,如果使用它们,最好是在精心选择的位置手动添加它们,而不是使用一些自动工具添加它们。

If you have to consider whether or not a certain type of comment is usable or not, it's most likely the latter. 如果您必须考虑某种类型的评论是否可用,则很可能是后者。

Comments are for explaining certain blocks of code or an entity in its whole, to ease up on comprehension; 注释用于解释某些代码块或整个实体,以简化理解。 not to make the formatting easier to read. 不要使格式更易于阅读。

Having a plugin always conform to this behaviour is both obese and ugly. 拥有始终符合这种行为的插件既肥胖又丑陋。

I agree there are much better ways to describe what a code is doing. 我同意,有更好的方法来描述代码在做什么。

If you have a long body of code preceded by a single informative comment like // Fix Work Item, you could take that code and refactor it as its own method. 如果您的代码很长,并且前面有一个信息丰富的注释,例如// Fix Work Item,则可以将该代码作为自己的方法进行重构。 Then use the comment as the new method's name, FixWorkItem(). 然后使用注释作为新方法的名称FixWorkItem()。 Doing this is a quick way to make your code more self-documenting and might even reveal some design characteristics you didn't notice before. 这样做是使您的代码更具自记录性的快速方法,甚至可以揭示一些您以前没有注意到的设计特征。

Keep an eye out for one-liner comments like that as potential refactors, which can be handled automatically by the IDE. 密切注意诸如此类的单行注释,这些注释可能会被IDE自动处理。 Code that documents itself is better than even the best-written standalone comments, except of course when describing intent. 记录自身的代码甚至比最佳书写的独立注释要好,当然在描述意图时也是如此。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM