Are there good reasons to wrap a single property in a #region in c#?
I recently inherited some C# code where nearly every item in a file was wrapped in an individual #region/#endregion block -- every class, every function, every property, every enum, but not the fields. Each of these was in turn wrapped in a "grouping" #region (e.g. "Properties", "Constructor", "Methods", etc.). There are multiple overloads of a single function with different argument lists, but they're each wrapped in individual regions with the same name, rather than a single region for all three overloads. The person who wrote this code is no longer with the company, and from the history in source control, it appears that these were present in the initial submission, and the practice continues through successive versions of the code as new properties and methods are added.
Any idea why this might be done? Some thoughts:
- An overzealous VS feature (or code-cleanup tool) automatically inserted the #region开发者_高级运维/#endregion blocks, and the author didn't remove them.
- There's an IDE that folds regions but not functions, so this was necessary to get syntax folding.
- This is a method of stubbing out the structure of your code prior to implementing it.
EDIT: I chose Jonathan's answer because it contributed a new reason why someone might have chosen to do this. Thanks for the discussion!
This seems like the result of a developer who doesn't understand how to use the IDE effectively (ie: code folding of methods, using the navigation features in the editor, etc). I would, personally, remove most (all?) of these regions, as they actually work against you in many ways.
Wrapping things in regions does have its place, but I'd argue that it's actually often counter-productive as a general practice. It's hiding code - and I often find makes it easier for developers to leave in inappropriate code, miss good refactoring opportunities, allow complex methods (that should be split up) to creep in over time, and let types get all together too large.
That being said - regions are a feature, and it's very much personal preference whether and how much to use them, as they have no impact on the compiled code.
When it comes to non-auto-implemented properties it can make the code easier to navigate. For instance a lot of the following code (inside the region) is just extraenuous fluff - which always looks the same (as properties really shouldn't have side-effects). Having a single line saying "Property - Name" is much nicer to navigate.
#region Property - Name
private string _name;
/// <summary>
/// Gets or sets the name of the customer.
/// </summary>
/// <remarks>
/// This should always be the full name of the customer in the format {First Name} {Last Name}.
/// </remarks>
/// <example>
/// customer.Name = "Joe Bloggs";
/// </example>
/// <seealso cref="Customer"/>
/// <value>
/// The name of the customer.
/// </value>
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
#endregion
However, as far as the 'member type' (Methods, Properties, Constructors, Fields) go I feel it makes the code harder to navigate; however other people feel it makes it easier.
In the end it's a coding standard and religous. If you don't like it, don't use it one your personal projects. If you are required to use it due to a standard then use it.
Good reasons? Probably not.
More like overzealous commenting, or possibley a tool that did that.
My regions typically consist of:
#region - Public Methods
#region - Private Methods
#region - Protected Methods
#region - Data Members
#region - Properties
Perhaps they were using an "auto generated help" type of document which looked for region instead of comments to build the "help document?"
Sounds like a horrible usage to me. My only thought is someone thought this was a great way to document code. I would look to remove this over utilization of regions.
I would say that is over-zealous and fails to take account of the concept of regions.
This is excessive regioning. There is no good reason to wrap every function, every enum with a region statement.
I would consider this bad practice and taking the concept of regioning code too far.
Usually, I do regions like:
1. Constructors
2. Public Methods
3. Private Methods
Etc.
Some sort of logical grouping. The regioning that you have described does not seem logical.
精彩评论