visual-studio 您如何看待代码折叠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:20:54  来源:igfitidea点击:

How do you feel about code folding?

visual-studiofolding

提问by Bryan Denny

For those of you in the Visual Studio environment, how do you feel about wrapping any of your code in #regions? (or if any other IDE has something similar...)

对于在 Visual Studio 环境中的那些人,您如何看待将任何代码包装在 #regions 中?(或者如果任何其他 IDE 有类似的东西......)

回答by Lars M?hlum

9 out of 10 times, code folding means that you have failed to use the SoC principlefor what its worth.
I more or less feel the same thing about partial classes. If you have a piece of code you think is too big you need to chop it up in manageable (and reusable) parts, not hide or split it up.
It will bite you the next time someone needs to change it, and cannot see the logic hidden in a 250 line monster of a method.

Whenever you can, pull some code out of the main class, and into a helper or factory class.

10 次中有 9 次,代码折叠意味着您没有充分利用SoC 原理
我或多或少对部分类有同样的感觉。如果您认为一段代码太大,则需要将其分成可管理(和可重用)的部分,而不是隐藏或拆分。
下次有人需要改变它时它会咬你,并且看不到隐藏在一个方法的250行怪物中的逻辑。

只要有可能,就从主类中提取一些代码,然后放入辅助类或工厂类中。

foreach (var item in Items)
{
    //.. 100 lines of validation and data logic..
}

is not as readable as

不像

foreach (var item in Items)
{
    if (ValidatorClass.Validate(item))
        RepositoryClass.Update(item);
}



My $0.02 anyways.



反正我的 0.02 美元。

回答by Pat

This was talked about on Coding Horror.

这是在Coding Horror上谈到的。

My personal belief is that is that they are useful, but like anything in excess can be too much.

我个人的信念是它们很有用,但就像任何多余的东西都可能太多。

I use it to order my code blocks into:
Enumerations
Declarations
Constructors
Methods
Event Handlers
Properties

我用它来将我的代码块排序为:
Enumerations
Declarations
Constructors
Methods
Event Handlers
Properties

回答by Joseph Daigle

Sometimes you might find yourself working on a team where #regions are encouraged or required. If you're like me and you can't stand messing around with folded code you can turn off outlining for C#:

有时,您可能会发现自己在一个鼓励或需要#regions 的团队中工作。如果你像我一样无法忍受折叠代码,你可以关闭 C# 的大纲:

  1. Options -> Text Editor -> C# -> Advanced Tab
  2. Uncheck "Enter outlining mode when files open"
  1. 选项 -> 文本编辑器 -> C# -> 高级选项卡
  2. 取消选中“打开文件时进入大纲模式”

回答by EndangeredMassa

I use #Region to hide ugly and useless automatically generated code, which really belongs in the automatically generated part of the partial class. But, when working with old projects or upgraded projects, you don't always have that luxury.

我用#Region隐藏了丑陋无用的自动生成代码,真正属于partial类的自动生成部分。但是,在处理旧项目或升级项目时,您并不总是那么奢侈。

As for other types of folding, I fold Functions all the time. If you name the function well, you will never have to look inside unless you're testing something or (re-)writing it.

至于其他类型的折叠,我一直在折叠函数。如果您给函数命名得很好,除非您正在测试或(重新)编写它,否则您将永远不必查看内部。

回答by Greg Hurlman

While I understand the problem that Jeff, et. al. have with regions, what I don'tunderstand is why hitting CTRL+M,CTRL+Lto expand all regions in a file is so difficult to deal with.

虽然我理解杰夫等人的问题。阿尔。有区域,我明白的是为什么点击CTRL+ M, CTRL+L来扩展文件中的所有区域如此难以处理。

回答by Jon Limjap

I prefer partial classes as opposed to regions.

我更喜欢部分类而不是区域。

Extensive use of regions by others also give me the impression that someone, somewhere, is violating the Single Responsibility Principle and is trying to do too many things with one object.

其他人对区域的广泛使用也让我觉得有人在某处违反了单一职责原则,并试图用一个对象做太多事情。

回答by Teifion

I use Textmate(Mac only) which has Code folding and I find it really useful for folding functions, I know what my "getGet" function does, I don't need it taking up 10 lines of oh so valuable screen space.

我使用具有代码折叠功能的Textmate(仅限 Mac),我发现它对折叠功能非常有用,我知道我的“getGet”功能的作用,我不需要它占用 10 行如此宝贵的屏幕空间。

I never use it to hide a for loop, if statement or similar unless showing the code to someone else where I will hide code they have seen to avoid showing the same code twice.

我从不使用它来隐藏 for 循环、if 语句或类似语句,除非将代码显示给其他人,我将隐藏他们看到的代码以避免两次显示相同的代码。

回答by Kev

@Tom

@汤姆

Partial classes are provided so that you can separate tool auto-generated code from any customisations you may need to make after the code gen has done its bit. This means your code stays intact after you re-run the codegen and doesn't get overwritten. This is a good thing.

提供了部分类,以便您可以将工具自动生成的代码与代码生成完成后可能需要进行的任何自定义分开。这意味着您的代码在重新运行代码生成后保持完整并且不会被覆盖。这是一件好事。

回答by Serhat Ozgel

Regions must never be used inside methods. They may be used to group methods but this must be handled with extreme caution so that the reader of the code does not go insane. There is no point in folding methods by their modifiers. But sometimes folding may increase readability. For e.g. grouping some methods that you use for working around some issues when using an external library and you won't want to visit too often may be helpful. But the coder must always seek for solutions like wrapping the library with appropriate classes in this particular example. When all else fails, use folding for improving readibility.

绝对不能在方法内部使用区域。它们可用于对方法进行分组,但这必须非常谨慎地处理,以免代码的读者发疯。通过它们的修饰符折叠方法是没有意义的。但有时折叠可能会增加可读性。例如,在使用外部库时将一些用于解决某些问题的方法分组并且您不想经常访问可能会有所帮助。但是编码人员必须始终寻求解决方案,例如在此特定示例中使用适当的类包装库。当所有其他方法都失败时,使用折叠来提高可读性。

回答by Tom

I'm not a fan of partial classes - I try to develop my classes such that each class has a very clear, single issue for which it's responsible. To that end, I don't believe that something with a clear responsibility should be split across multiple files. That's why I don't like partial classes.

我不喜欢部分类 - 我尝试开发我的类,以便每个类都有一个非常明确的、它负责的单一问题。为此,我认为不应将具有明确责任的内容拆分到多个文件中。这就是为什么我不喜欢部分课程。

With that said, I'm on the fence about regions. For the most part, I don't use them; however, I work with code every day that includes regions - some people go really heavy on them (folding up private methods into a region and then each method folded into its own region), and some people go light on them (folding up enums, folding up attributes, etc). My general rule of thumb, as of now, is that I only put code in regions if (a) the data is likely to remain static or will not be touched very often (like enums), or (b) if there are methods that are implemented out of necessity because of subclassing or abstract method implementation, but, again, won't be touched very often.

话虽如此,我对地区持观望态度。大多数情况下,我不使用它们;然而,我每天都在处理包含区域的代码——有些人对它们非常重视(将私有方法折叠到一个区域中,然后将每个方法折叠到自己的区域中),而有些人则对它们轻描淡写(折叠枚举,折叠属性等)。到目前为止,我的一般经验法则是,如果 (a) 数据可能保持静态或不会经常被触及(如枚举),或者 (b) 如果有方法由于子类化或抽象方法的实现,是出于必要而实现的,但同样,不会经常触及。