Is the #region directive really useful in .NET?

后端 未结 17 1908
孤城傲影
孤城傲影 2021-01-03 23:43

After maintaining lots of code littered with #region (in both C# and VB.NET), it seems to me that this construct is just a bunch of \"make work\" for the programmer. It\'s w

相关标签:
17条回答
  • 2021-01-04 00:09

    Nice answers, I agree with them that say it sometimes reflects bad coding and design but #region actually is usefull if you're creating documentation (MSDN style) with the SandCastle. Lets say you have a public API and there is some base class that you want to give an example of usage for. Then you would properly document your public methods and add an example region where you could copy and paste some code. Problem with this is that when/if your base class changes you're supposed to change the example eventually. Better solution is to include a sample code project in your solution and build it all together, so everytime you build your solution if the sample code is not up to date it will not compile. So what does that have to do with regions you will be asking your self by now. Well look at this sample:

    /// <example>
        /// The following code sample is an implementation of LoadPublishedVersion() for XmlPageProvider.
        /// <code source="../CodeSamples/EPiServerNET/PageProvider/XmlPageProvider.cs" region="LoadPublishedVersion" lang="cs"/>
        /// </example>
    

    Notice there is a link to the source code sample file and region for the method that you want to expose as a sample in your documentation. See here the result. That method needs to be in a proper region and will be automatically included in your documentation. That's why I wouldn't throw away #region yet.

    0 讨论(0)
  • 2021-01-04 00:09

    There really isn't a benefit. They are a code smell. After using them for awhile, I got sick of them. If you need to break things out by functionality, use a partial class.

    0 讨论(0)
  • 2021-01-04 00:10

    Often times, both partials and #regions are used as a crutch for bad design (e.g. class is too big or tries to do too many things).

    The best use I've had for #regions so far is the grouping of functionality that is seen in many different classes. For example, value objects that have getters, setters, constructors and supporting fields. I might very well group those ideas into regions. Its a matter of opinion, however, as to whether that makes code cleaner or harder to read.

    0 讨论(0)
  • 2021-01-04 00:10

    I often use them instead of comments to order groups of functionality in the body of a class, e.g. "Configuration public interface", "Status public interface", "internal processing" and "internal worker thread management".

    Using the keyboard shortcuts to "collapse to definitions" and "expand current block", I can easily navigate even larger classes.

    Unfortunately, Regions are broken for C++, and MS doesn't think it needs to be fixed.

    0 讨论(0)
  • 2021-01-04 00:12

    My working day starts with opening files in editor and clicking on "Expand All" to hide all regions. After that I can begin to work.

    0 讨论(0)
  • 2021-01-04 00:14

    I find that they obfuscate the code in all but the simplest of uses. The only use we advocate in our projects are the ones the IDE uses (interface implementations and designer code).

    The right tools should be used for the right purpose. Code should be written to show intent and function rather than arbitrarily grouping things. Organizing things into access modifier grouping or some other grouping just seems to be illogical. I find the code should be organized in a manner that makes sense for the particular class; after all, there are other tools for viewing class members by access modifier. This is also the case for almost every other use of regions; there is a better way.

    For example, grouping properties, events, constants or otherwise together doesn't really make sense either as code is generally more maintainable if the things are grouped together by function (as in, a property that uses a constant should be near that constant, not near other unrelated properties just because it's a property).

    0 讨论(0)
提交回复
热议问题