简体   繁体   English

方法最佳实践结束时的评论?

[英]Comments at end of method best practice?

I have some code which has comments after each method saying this for example: 我有一些代码在每个方法之后都有注释,例如:

// End of contructor DbFactoryDBConnection()

Should a comment like this be used to indicate the end of a method or not? 这样的评论是否应该用于表示方法的结束? It was something picked up from an earlier employment where it was common place. 这是从早期的就业中获得的东西,它是常见的地方。

I feel such comments are after methods which body does not fit in a single screen. 我认为这样的评论是在身体不适合单个屏幕的方法之后。 I believe this is a good sign for the refactoring and splitting out such long methods to shorten ones or even extracting new entities/services/helpers. 我相信这是重构和拆分这么长的方法来缩短一些甚至是提取新的实体/服务/帮助的好方法。 This is like C# region, sometimes it is used to hide very long code blocks and this is a sign that you have some kind of a God Object anti-patter . 这就像C#区域,有时它用于隐藏很长的代码块,这表明你有某种God Object反模式

No. If you want to know what the braces are closing, without scrolling, you can have ReSharper . 不。如果您想知道大括号的关闭,而不滚动,您可以使用ReSharper Which is great in many ways. 这在很多方面都很棒。

Just put your cursor on braces, and if the opening of them isn't in view - it tooltips the whole line before it. 只需将光标放在大括号上,如果它们的开口不在视图中 - 它会在它之前对整行进行工具提示。

Usually this kind of comments are an overdocumentation hassle. 通常这种评论是一种过度的文件。
The right place where a comments should exist are at the beginning of your classes and of your methods and just before where important decisions will be taken inside your code. 评论应该存在的正确位置是在您的课程和方法的开头,以及在您的代码中进行重要决策之前。
If you find the need to document the end of an IF, SWITCH or METHOD then it's very probable that your code should be reexamined to be simplified. 如果您发现需要记录IF,SWITCH或METHOD的结尾,则很可能需要重新检查您的代码以进行简化。

I think this sort of commenting is completely unnecessary, and only serves to clutter the code. 我认为这种评论完全没有必要,只会使代码混乱。 Most IDE's (such as Visual Studio) have had features for highlighting the scope of a method (or whatever) for many years, so i'm not sure what value you could add you your code by doing this. 大多数IDE(例如Visual Studio)都具有突出方法(或其他)多年的功能,因此我不确定通过这样做可以为您的代码添加什么值。

你可以随时使用PowerCommands for Visual Studio,它在大括号的末尾添加注释你在使用它时是什么样的bracer,它有很多帮助:)

No, such a comment should not be used. 不,不应该使用这样的评论。 The only reason, someone would use a comment like this is the fact that the method is way too long. 唯一的原因是,有人会使用像这样的评论这个方法太长了。 But even than, modern IDEs show you somewhere in which method you currently are and even allows folding methods back to just the definition. 但即便如此,现代IDE还是向您展示了您当前使用哪种方法,甚至允许折叠方法回归到定义。
Comments like this are of no value whatsoever and simply pollute the code-base. 像这样的评论没有任何价值,只是污染了代码库。

This doesn't belong in the source IMHO; 这不属于源恕我直言; it's too easy to get out of sync with the effective source code blocks. 与有效的源代码块不同步太容易了。 There are extensions that will show this info for you in Visual Studio i you thing that this is helpful. 有一些扩展可以在Visual Studio中为您显示此信息,您觉得这很有帮助。

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

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