Skip to content

C# Regions are evil

The programming language C# supports the use of regions since ever. To be more precise it dates back into VS .Net 2003 and .Net 1.x. The concept is fairly simple: use the keyword #region and #endregion to mark a block of code as a collapsable and optionally named region.

History

Where do the regions come from? They started with C# 1 when the language did not have that much language features. Not only Generics where missing but also partial classes didn’t exists.

In these early days tools like Visual Studio did use the regions to wrap their generated code. This way the IDE by default collapsed the code generated from tools like the Forms-Designer.

With C# 2.0 Microsoft introduced the feature called partial classes. With this feature a class can span more then one file so the tools could generate their code into an entire separate file. No need for regions anymore.

Additionally Microsoft added foldable code sections based on the semantics of the code into the text editor. This way a developer can collapse methods, IF-Statements, classes, namespaces, etc.

Today

I am surprised how many developers still use Regions in their C# code even in 2016. Some years ago I tend to use regions too. That time seniors asked me: “Why are you using regions?”. My immediate answer was to clean the code and increate its readability. Shouldn’t one structure its code the way collapsable code (aka regions) is not needed at all? From then on I challenged myself for each use of a region: “This should go into its own method (or even its own class). Should it?”. The result was, that I never ever wrote a region again.

Problems with regions

So what are the problems with regions? Why the hell do I write an entire blog article about them? Even in 2016 developers around the world (or at least around Europe) are still using them. This made me think about the problems with regions:

  1. Regions are painful for other developers inspecting your the first time. They first have to expand all the collapsed sections to get the full picture of the code. And because of all the #regionand #endregion lines the code gets even larger.

  2. Regions often lead to bad code because developers easily put a region around larger code blocks to make them hide instead of thinking how to structure their code well.

The second issue is really bad. Stuff that leads in writing bad code hurts me. Regions are one of these things.

I find usages of regions mainly in these places:

Grouping members in a class (private, public, etc.

If a class contains that many members so collapsable grouping is needed the class probably needs to be split into multiple classes. With large classes chances are hight that they are responsible for too many things. See the “Single Responsibility Principle” from SOLID.

On the other hand: if a class only serves a single responsibility it is probably small enough so it doesn’t need regions for grouping its members.

Using regions here lead in bad design of the classes as they handle way too many things. For small classes regions are just a waste of code-lines. No need for regions here.

Regions within methods

That’s another area I regularly find regions. When methods get a little longer some developers tend to group the sub-parts of a method into regions. I did so too. The idea is that one can collapse them and just read the region-names to get the big picture of the method.

As you may guess: bad design too. Not only clean-code suggest to write short methods so maintenance and testability of them is good.

Instead of using a region here one can easily extract a new method with the given block of code. Sometimes extracting its own class is even a better match. If a developer cares about the “Single Responsibility Principle” the classes don’t get that complicated so methods tend to be less complex and shorter too.

So again: using regions here leads in bad code.

Collapse generated code

As written above: since C# 2.0 there are partial classes for this. Use them instead regions!

What to do with regions

It would be great if Microsoft can remove them from the language features but this isn’t really an option as it will break a lot of existing code. So the best thing is to not use them anymore.

So far so good but what with existing code?

One of the first Visual Studio extensions I install on my dev machines is the “I hate regions” extension. It auto-expand all regions and renders them in a small and grey font so the don’t disturb that much.

Another thing one can do is to set up a R# rule and apply it to the entire solution. Bye-bye regions.

So, dear developers – don’t use regions anymore. Let them go! R.I.P Regions!

marcd View All

I love to write software. More then two decades ago I managed to make my hobby my full-time job so I spent more then 20 year writing professional software (I guess that makes me a "Senior Software Developer"). The last few years I spend most of the time developing in C#/.Net for all kind of windows-, web- and embedded-software.

In my free time I enjoy my family, taking photos and go diving in cold lakes and rivers in Switzerland.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: