DEV Community

The Great C# Regions Debate

Rachel Soderberg on March 29, 2019

C# regions were introduced way back in 2003 via Visual Studio .NET as a way to name and collapse a block of code using the keywords #region to open...
Collapse
 
twigman08 profile image
Chad Smith

My view on regions is mixed. I will say, I do usually sense a code smell when I see a region when I first get into a code base. Mostly because I've lived the nightmare of diving into a region and seeing another region inside that region, and then even regions inside single functions. When I see that then they really get to me as it's just trying to hide the fact that you need to rewrite this code.

I've also seen regions used in places that I'm fine with. An example being a RESTFUL API. I have seen regions used to keep GET requests separate from POST requests in a controller with the controller being well written and skinny. I'm fine with them being used in situations like that.

So like a lot things: use them appropriately. Don't use them to try to hide your badly designed code, and please don't start using nested regions inside a single function call.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

Have you ever seen a region nested in a region? That would probably make me cry...

Collapse
 
twigman08 profile image
Chad Smith

I sadly have. I definitely cried.

To make it worse: I have seen a nested regions inside a constructor before. I am still having nightmares over that...

Thread Thread
 
rachelsoderberg profile image
Rachel Soderberg

Oy, I didn't even consider that as a possibility. It hurts to even read it

Collapse
 
beagle1984 profile image
Sergio Aquilini

Maybe CustomerService and CustomerLogic aren't such a great design. Did you ever consider this?

In order to avoid god classes and really strive for that SRP, you should start with giving your classes a meaningful name that implies a single responsibility, something like CustomerMailer, CustomerPurchaseStatistics, CustomerTicketsHandler, etc.

Collapse
 
netferret profile image
netferret

I think its quite simple, if you need to hide blocks of code like this then your class is clearly too large. You need to use Separation of Concern and split it out accordingly.

Hiding properties, constructors etc is just silly as you need to need to see them as developer or how can you analyse the code and do your job?

I personally hate the use of these regions, its a faff, hides code, make its look nicer for the person who wrote it, but its not the person who is going to do the development on the code going forward its a hassle. At least with methods you can press CTRL+M CTRL+O to collapse all.

Collapse
 
davidisnotnull profile image
David Johnson

I can see the horror show that regions can represent if they're used with abandon, but I do occasionally find some uses. If I need a couple of private methods to handle functionality in a class, then I'd create a region called "Private Methods" at the bottom so I can separate out the public and private sections.

Collapse
 
scotthannen profile image
Scott Hannen

Those are good rules, although I'd be way more restrictive. Yes, they are arbitrary numbers. They are pulled from thin air. I prefer no more than 15 lines per method.
Those arbitrary limits are important. If you can't have a 30 line method then you can't have a 200 line method.

Collapse
 
rachelsoderberg profile image
Rachel Soderberg • Edited

You make a good point! I would definitely keep all of the customer logic methods in one class. Made up rules for the sake of making up rules can be silly.

Collapse
 
lexruster profile image
Alexander

Looks like to late for comment. Any plain structure with dozens of child of the same rang is hard to maintain, navigate. It is not only about methods in the class. It is about everything. Like UI with 100 buttons. You want to group it. Almost everything can be grouped. So even this CustomerLogic can be decomposed. May be to
CustomerOrderLogic
CustomerAccountLogic. Need real examples.

Thread Thread
 
rachelsoderberg profile image
Rachel Soderberg

Never too late! And you are absolutely right, it could be broken even further into more refined classes that only perform one job. I love that you mentioned this point, because I am definitely still learning and appreciate any holes in my knowledge being pointed out.

Collapse
 
kubiak200 profile image
Alex

Regions should have another background color,
closing the code is horrible

Collapse
 
rachelsoderberg profile image
Rachel Soderberg

It would be nice if regions had a slightly different back color, you make a great point! Even if it were just a slightly off-grey to set them apart from the rest of the code. I've found myself overlooking a region because it just didn't catch my eye well enough at first glance.