Why do #regions exists?
My best guess is that back in the the good old .NET 1.0 and 1.1 days, before partial classes, MS needed a way to visually remove, all the noise the auto generated Windows Forms code made. It kind a made sense, because you don't need to understand the auto generated code, you can just open the form in design view.
Why use #regions?
Often I see #regions used to group different code artifacts, like "Fields", "Constructors", "Properties", "Events", "Methods" etc. When the class gets big, navigating to an event handler becomes as easy as:
- Collapse all "regions.
- Expand the "Events" region
- Start to scroll for the event handler.
That is faster than scrolling through all the code in a big file.
Do we need #regions?
As with most other "coding tools", #regions tackle complexity. A class with several thousands lines of code, can be visually laid out in 20 lines or so. If you have a class with many, many lines of code, #regions do offer an advantage over normal layout. The #regions don't take up to much space compared to the total number of lines, so it is easy to ignore them if not needed. But if the #regions makes sense, they can be used to slice the lines into categories.
An obvious problem with #regions
Like any other categorization it falls apart if there are multiple dimensions. What if we are looking for an event handler, but the #regions define business rules?
My wife categorizes our pictures by month. She creates a folder with a "Month Year" name, and places pictures taken that month there. Every now and then we need to find a picture of someone, it could be my son. The problem with the "Month Year" categorization is that is does not help, we need a "who is on the picture" categorization.
If google created a fixed categorization, it would be much harder to find anything. Because the same topic could be categorized with many dimensions.
A not so obvious code smell lurking behind the #regions
So if #regions are good at categorizing many lines of code, why is it a code smell to use them?
This leads to another question.
>Why do we need to categorize?
The answer is easy.
>Because of the many lines of code.
And finally the question we should be asking
>Why do we allow many lines of code in a single file?
A single class with many lines of code, is called a god object. It is a well known anti-pattern, and will kill maintenance. I will not in depth with the god obejct anti-pattern, but it is well described elsewhere.
Moving away from the god class
Is it possible? It most certainly is...
This form god'ified could look like this in code.
In addition, the tab order is set in the visual designer.
The problem with this sample is ofcourse, it is not several thousans of lines.
If we wanted to de-god'ify the class it could look like this.
Things to notice, there are zero regions!
The microcontrollers look like this.
Even more these MicroControllers are reusable in the next form!
Things normally done(or forgotten) in the visual designer can also be micro controlled.
Tab order is as easy as.
If you use #regions, you might need to apply separation of concerns. This forces your code to be more readable, although it creates a navigation issue, when having lots of classes.