谁喜欢 Visual Studio 中的#regions?

发布于 2024-10-10 02:40:19 字数 545 浏览 3 评论 0原文

就我个人而言,我无法忍受区域标签,但显然它们对于组织代码具有广泛的吸引力,因此我想测试一下其他 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 技术交流群。

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

发布评论

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

评论(6

月下伊人醉 2024-10-17 02:40:19

我喜欢区域,并且一直使用它们。我用它们将班级中同类的成员分组。

您已经可以在编辑器中折叠方法、类和命名空间。区域为您提供了创建另一个级别的选项,使您可以按照您认为重要的方式排列代码。

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.

姐不稀罕 2024-10-17 02:40:19

StyleCop 不喜欢区域

SA1124:DoNotUseRegions

原因

C# 代码包含一个区域。

规则说明

只要将区域放置在代码中的任何位置,就会违反此规则。在许多编辑器(包括 Visual Studio)中,默认情况下该区域将显示为折叠状态,从而隐藏该区域内的代码。隐藏代码通常是一种不好的做法,因为随着时间的推移,代码的维护可能会导致错误的决策。

如何纠正违规行为

要解决违反此规则的问题,请从代码中删除该区域。

关于这是否是合理的规则,有一些讨论

共识似乎是有些人喜欢地区,有些人不喜欢——这取决于各个团队的决定。最重要的是在整个项目中使用一致的风格。

区域可能可接受的一个地方是将实现特定接口的所有方法分组。值得注意的是,如果您使用代码生成功能来提供用于实现接口的方法存根,Visual Studio 会自动添加一个区域。

对我来说,它们唯一有意义的地方是自动生成的代码,因为您永远不必出于个人好奇心而阅读它。

部分类功能更适合将自动生成的代码与手动生成的代码分开在同一个班级内。

使用自动生成的源代码时,可以将代码添加到类中,而无需重新创建源文件。 Visual Studio 在创建 Windows 窗体、Web 服务包装器代码等时使用此方法。您可以创建使用这些类的代码,而无需修改 Visual Studio 创建的文件。

StyleCop doesn't like regions:

SA1124: DoNotUseRegions

Cause

The C# code contains a region.

Rule Description

A violation of this rule occurs whenever a region is placed anywhere within the code. In many editors, including Visual Studio, the region will appear collapsed by default, hiding the code within the region. It is generally a bad practice to hide code, as this can lead to bad decisions as the code is maintained over time.

How to Fix Violations

To fix a violation of this rule, remove the region from the code.

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 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.

The partial class feature is better for separating automatically generated code from manually generated code within the same class.

When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.

焚却相思 2024-10-17 02:40:19

我认为 #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?

淡墨 2024-10-17 02:40:19

我确实使用区域来组织类中更大的结构,即。具有回调和由此调用的方法的依赖属性,例如。您有一个采用 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 to INotifyCollectionChanged. 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.

可是我不能没有你 2024-10-17 02:40:19

当我编程时,我经常使用区域,它们帮助我保持代码井井有条,并且能够只关注我关心的部分。我总是添加有关区域的评论以及它们的内容。但是当我完成后,我总是删除它们。它是一个方便的工具,仅此而已。

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.

彼岸花ソ最美的依靠 2024-10-17 02:40:19

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.

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