I often see large functions consisting of multiple #region tags. Is #region/#endregion quickly evolving evolving 开发者_StackOverflow社区as means to avoid refactoring into smaller functions?
In most of the cases code defined in #region block could be moved to a separate function. What is the original intent of #region?
Before partial
Visual Studio used it to separate auto-generated code from user defined code.
Regions are a tool just like anything else. If you have a use for them ... goody. If not, awesome. They allow you to close and separate entire blocks of code so a developer can organize things and find them rapidly. If you have a region called "Properties", it doesn't take a rocket surgeon to figure out what is in there. Thus it can be collapsed and will no longer interfere with scrolling the page unnecessarily.
It's a maintenance item that may or may not help developers keep track of their classes and maybe avoid stepping on each other's code. For those that don't find them useful, don't use them. For our team we defined a standard for using them, and we pretty much stick to that standard. Just throwing things into regions can make the problem even worse, so it's always good to adopt a standard and stick to it (whichever way you choose).
EDIT: I can't think of a good use for using regions within specific methods. That just seems like clutter to me, but it wouldn't surprise me to find someone who found a decent use for it (in their opinion).
To obscure / obfuscate code
Its lazy coding. If a method has #regions within it. It should be refactored into smaller methods.
It can also be used to break down a class into it's different sections (like methods, properties). I'm not a big fan of that anymore.
I use to collapse my code into organized groups, nothing more, nothing less.
To make code more readable and easier to browse. It's not just something intended for functions, it's lot more useful in the class scope. For example, you might want to group different kinds of methods, fields, properties etc together.
When you are using regions inside functions then you should probably consider refactoring instead
There are many cases--usually in class scope, but occasionally in function scope, where the rules of a language require one to generate a lot of repetitive code. One nice use for #region is to visually replace a large amount of such code with a single line indicating what was replaced. For example, in VB if one wants to have a lot of readonly properties backed by private read-write fields, one might declare the fields, and then have a #region whose description is "Simple read-only properties for foo, bar, baz, and fnord." The code for those properties would be 20 lines (plus, typically, a blank line between each property), but anyone reading the description would know exactly what those twenty lines are going to say. Give a dozen programmers that description, along with the declarations for the variables, and all of them would probably produce character-for-character identical code (except, possibly, for inter-property spacing and comments). Why fill the editor with such clutter?
I use #region/#endregion to mark off a section of common code. That way I can fold it up and out of my way. For example:
#region Getters and setters
...
#endregion
is in every class I create. Those almost never change, and I don't need them cluttering my view while working in the class.
I don't like to use it either but I have used it in one particular case that comes to mind. I have a local domain creation tool that was built to create/setup the initial customer sites that we host. There are about 20 sections to the creation process so I used #region blocks to divide the sections up...the only purpose being organization...but I've accidentally deleted entire blocks of code before because a collapsed region tag got selected in with other code...I wouldn't recommend getting in the habit of using them.
I divide members of my class into groups using the #region
directive. I have Constants, Fields, Properties, Constructors, Overrides, etc. sections in every one of my files (though not each one is present every time). It's my own personal coding standard and it helps me find things quickly. I NEVER put #region
directions inside of functions. You're function is probably too large if you need #region
for it.
精彩评论