Is the #region directive really useful in .NET?

后端 未结 17 1905
孤城傲影
孤城傲影 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:25

    Our Business Objects all have Regions - and we love them.

    We have;

    • Business Properties and Methods
    • Shared Methods
    • Constructors
    • Authorization
    • Data Access
    • Events

    We have a few others depending on the type of Business Object we are dealing with (Subscriber etc)

    For many classes regions just get in the way - but for our standard business objects they save us a ton of time. These Business Objects are code gen'd, so they are very consistent. Can get to where I want to be way faster than the clutter if they aren't, and the consistency makes it easy to find each other's stuff.

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

    They can be overused, but I like them for separating private methods, public methods, properties, and instance variables.

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

    Going on with what has been previously said by Russell Myers, if you learn how to refactor your code properly (a skill proficient developers must learn), there really isn't too much of a need for regions.

    A couple of weeks ago I thought regions were great because they allowed me to hide my fat code, but after exercising my code skills I was able to make it slimmer and now I fit into a size 7 class (someone should SO make that a measurement for refactoring in the future! :P)

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

    There are times when your methods HAVE to be long, especially with web development. In those cases (such as when I've got a gridview with a large, complex object bound to it) I've found it useful to use regions:

    #region Declaring variables for fields and object properties
    
    #region Getting the fields in scope
    
    #region Getting the properties of the object
    
    #region Setting Fields
    

    These are discrete sections of the method that COULD be broken out, but it would be difficult (I'd have to use variables with larger scope than I like or pass a LOT of variables as 'out'), and it is basic plumbing.

    In this case, regions are perfectly acceptable. In others, they are needless.

    I will also use regions to group methods into logical groups. I reject partial classes for this purpose, as I tend to have a lot of pages open when I'm debugging, and the fewer partial classes there are in an object (or page, or dialog), the more of them I can have on my tab list (which I limit to one line so I can see more code).

    Regions are only a problem when used as a crutch, or when they cover poor code (for instance, if you are nesting regions inside of each other within the same scope, it's a bad sign).

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

    I use them all the time. Again, like anything else, they can be used for both evil and good, and can certainly be the hallmark of bad design, but they can be used to help organize code very well.

    #region Properties
    
    #region Update Section
    
    #region Accessors
    

    Certainly you should avoid Jeff's example of

    #Sweep under carpet
    

    What I find odd about them, as Jeff pointed out, is that they are a compiler preprocessor command for ui purposes. I'm sure VS team could have done something just as useful in another way.

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