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 collapsible 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!

2 thoughts on “C# Regions are evil Leave a comment

  1. Agreed Marc, regions are used to hide bad code that’s written against clean code standards.
    The software engineer should distribute the code to meaningful classes, methods instead.

    When i want to see the code, i automatically remove all of the regions from the whole solution using Resharper extension.
    Because regions don’t make it easier to read, they instead hide the code, and group them in a way, that’s only meaningful to the original author.

    The good code should be self explanatory and easily understandable by any trained developer.

    Like

  2. The main problem with regions is simple: They hide the code. Since that is also their only function, it follows that they’re a complete waste of time.

    Elaborating on the problem:
    You often need to find code that has been collapsed by editor… and collapsed code prevents many of the “accidental discoveries” in adjacent code that save time.
    Figuring out (as well just the clicking) what regions and thus code should currently be collapsed is hell to the reader.
    Every programmer has a different idea on region organization… even the same programmer will have different ideas depending on the phase of the moon.
    You shouldn’t have to spend a lot of time deciding where to place a new method or field on the basis it might fall out of a subjective region structure.
    Using #region/endregion in what is fundamentally a block tree ({ }) is an impedance mismatch, just like with #ifdef, although in that case it has functionality that warrants it.
    Modern IDEs (e.g. VisualAssist) let you navigate huge files with ease should you have them (not saying you should).

    How did I find out about this [a long time ago]? Every time I added regions somewhere, a lot additional time thinking about precisely how to put them was used, and the result was just horrible to browse later even if it seemed neatly structured while writing them. So later I would remove them in frustration. That in turn made me soon realize that I’m better off pretending #region doesn’t exist.

    Like

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: