谁喜欢 Visual Studio 中的#regions?
就我个人而言,我无法忍受区域标签,但显然它们对于组织代码具有广泛的吸引力,因此我想测试一下其他 MS 开发人员对这个想法的看法。
我个人的感觉是,任何简化代码的愚蠢技巧只会鼓励可怕的编码行为,例如缺乏凝聚力、意图不明确以及糟糕或不完整的编码标准。
一位程序员告诉我,代码区域通过明确其他程序员应该将其贡献放在哪里来帮助鼓励编码标准。
但是,坦率地说,这对我来说听起来像是一堆马粪。如果您有一个标准,那么程序员的工作就是了解该标准是什么......您不需要在每个类文件中定义它。
而且,没有什么比打开文件时所有代码都崩溃更烦人的了。我知道 cntrl + M, L 会打开所有内容,但随后您需要阅读可怕的“哈希区域定义”打开和关闭行。
他们只是令人恼火。
我最坚定的快速编码理念是所有程序员都应该努力创建清晰、简洁和有凝聚力的代码。区域标签只是用来制造噪音和冗余意图。
区域标签在经过深思熟虑且有意为之的类中是没有意义的。
对我来说,它们唯一有意义的地方是在自动生成的代码中,因为您永远不必出于个人好奇心而阅读它。
Personally I can't stand region tags, but clearly they have wide spread appeal for organizing code, so I want to test the temperature of the water for other MS developer's take on this idea.
My personal feeling is that any sort of silly trick to simplify code only acts to encourage terrible coding behavior, like lack of cohesion, unclear intention and poor or incomplete coding standards.
One programmer told me that code regions helped encourage coding standards by making it clear where another programmer should put his or her contributions.
But, to be blunt, this sounds like a load of horse manure to me. If you have a standard, it is the programmer's job to understand what that standard is... you should't need to define it in every single class file.
And, nothing is more annoying than having all of your code collapsed when you open a file. I know that cntrl + M, L will open everything up, but then you have the hideous "hash region definition" open and closing lines to read.
They're just irritating.
My most stead fast coding philosophy is that all programmer should strive to create clear, concise and cohesive code. Region tags just serve to create noise and redundant intentions.
Region tags would be moot in a well thought out and intentioned class.
The only place they seem to make sense to me, is in automatically generated code, because you should never have to read that outside of personal curiosity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我喜欢区域,并且一直使用它们。我用它们将班级中同类的成员分组。
您已经可以在编辑器中折叠方法、类和命名空间。区域为您提供了创建另一个级别的选项,使您可以按照您认为重要的方式排列代码。
I like regions, and use them all the time. I use them to group members of the same kind inside classes.
You already have the means to collapse methods, classes and namespaces in the editor. Regions give you an option to create another level that lets you arrange the code in a way that corresponds to what you think is important.
StyleCop 不喜欢区域:
关于这是否是合理的规则,有一些讨论。
共识似乎是有些人喜欢地区,有些人不喜欢——这取决于各个团队的决定。最重要的是在整个项目中使用一致的风格。
区域可能可接受的一个地方是将实现特定接口的所有方法分组。值得注意的是,如果您使用代码生成功能来提供用于实现接口的方法存根,Visual Studio 会自动添加一个区域。
部分类功能更适合将自动生成的代码与手动生成的代码分开在同一个班级内。
StyleCop doesn't like regions:
There is some discussion about whether or not this is a reasonable rule.
The consensus seems to be that some people like regions and some people don't - it is up to individual teams to decide. The most important thing is to use a consistent style throughout your project.
One place where regions might be acceptable is to group all the methods that implement a particular interface. It is worth noting that Visual Studio automatically adds a region if you use the code generation feature to provide method stubs for implementing an interface.
The partial class feature is better for separating automatically generated code from manually generated code within the same class.
我认为
#region
非常好。我自己从未使用过它,但如果您有一个大型或复杂的课程,它可以帮助您找到所需的内容。想象一下,如果您正在实现 ID3D10Device1,则需要实现一百多个方法。您想将它们全部扔到一处吗?I think that
#region
is perfectly fine. I've never used it myself, but if you have a large or complex class, it can help with finding what you're looking for. Imagine if you're implementing ID3D10Device1- that's over a hundred methods to implement. You want to just throw them all in one place?我确实使用区域来组织类中更大的结构,即。具有回调和由此调用的方法的依赖属性,例如。您有一个采用
IEnumerable
的 DP,并且您希望使用弱事件模式来响应INotifyCollectionChanged
。这可能需要一些代码,并且由于编码后我不会碰它,所以我将其放在一个区域中。但是,如果您采用区域来构建逻辑,则会产生严重的代码异味,这就是 Mark 帖子中的 StyleCop 规则所指出的。
I do use regions to organize bigger constructs in a class, ie. a Dependency Property with callback and methods getting called by this, eg. you have a DP that takes an
IEnumerable<T>
, and you want to use the weak event pattern to respond toINotifyCollectionChanged
. This can take some code, and as I won't be touching it after coding it, I put it in a region.However, if you resort to regions to structure your logic, this is severe code smell and that's what the StyleCop rule in Mark's post is pointing hat.
当我编程时,我经常使用区域,它们帮助我保持代码井井有条,并且能够只关注我关心的部分。我总是添加有关区域的评论以及它们的内容。但是当我完成后,我总是删除它们。它是一个方便的工具,仅此而已。
while I am programming I use regions a lot they help me keeping my code organised and be able to focus only on parts I care. I always add comments on the regions and what are they about .But when I am done I am always removing them. Its a handy tool, that's all.
IHMO,如果类中有
#region
部分,则意味着类中有两种行为,因此您应该将类拆分为两个对象。IHMO, if you have
#region
section in a class, it means that you have two behaviors in your class and so you should split your class in two objects.