EVIL (?) REGIONS

Beyza Celep
4 min readApr 13, 2022

#region New Post

Hello,

In this post I am going to talk about something kinda different from my other contents. Even though so much reading and searching, I still couldn’t 100% decide if it is a good or bad practice ! So please after you read this article, share your opinions & reasons with me. As you can see above the subject is regions. I know it sounds pretty simple but here we are :) Everything started with a tweet that I saw this morning from Nick Craver (who is a software engineer at Microsoft) :

And also this one :

Recently, clean code is something that I am really obsessed with. I am reading books, searching for examples and discussing with other developers, reading blog posts and threads to improve my code. Not just learning but I am also using what I learn and trying not to be “previous developer” :) . But I have never seen an evil region discussion before so decided to deep dive into it ! Now I am going to comparatively share with you opinions that I found.

First, let’s start with “evil region” group :

They say :

  • Regions complicate reading the code. You need to expand all the regions to see all of the code.
  • Expanding is one thing, but region lines also makes the code longer.
  • Some developers use region tags as a hiding method. Instead of spending time to write cleaner code or create a well-designed structure they just use #region #endregion and “put out of sight” all the mess.
  • Grouping members mostly a sign of that the class must split into smaller ones. If you need grouping in a class or in a method, that means object does more than one thing. We all know Single Responsibility Principle. If there are more than one responsibilities just split them. Do not try to hide them with tags or show them as a natural part of the object.
  • Code smell is a symptom which tells there is a problem about the design which will potentially increase the number of bugs : this is not the case for regions, but regions can lead to code smells like long methods, unuseful variables, conflict with SRP etc.
  • Regions are anti patterns. This means you must avoid to use them.
  • Do not use regions, refactor the code instead.
  • Do not use regions to hide similar objects (specially entities here). For example, when you have so many properties (or you may say fields) in a class and you collapsed them with regions. Even when you are able to refactor the code -now or later- by removing unnecessary fields, removing the ones that does the same job at the end of the day etc. you never see them and you never refactor the code.
  • We all need to confess regions are mostly using for forgetting about code smells or not to see bad code we wrote. What the eye doesn’t see the heart doesn’t grieve over. Just breathe and refactor !
  • It can not be obvious if there are dependencies between the regions. This makes testing harder.
  • If you use regions everywhere, you’ll find yourself creating regions even for one block. This does not make any sense.
https://csharpcorner-mindcrackerinc.netdna-ssl.com/article/using-regions-to-improve-code-readability/Images/UsingRegnsImg1.gif

Let’s go on with the opposite group :

They say :

  • When segregation for code functionality is needed, regions are perfect fit.
  • Using so many and unnecessary regions cause complexity but if you choose where to use them properly, they increase readability. So the readability increases, the idea of refactoring becomes easier and you find yourself refactoring the code.
  • In some scenarios, objects can have multiple functionality or necessary fields. That’s why using regions to group these functionalities and make code good looking is not a bad idea at all !
  • You can use shortcut keys to expand all the regions, if it is hard for you to follow the code.
  • There are various scenarios and necessities that using regions is a good practice. We just shouldn’t ignore them totally.
  • Regions tells a summary about code and it can be substantially easy for new developers in the team to read and understand the concept of code block.
  • Also, remember that every developer has a style and we can use this “evil” mindset for literally everything. There aren’t blacks and whites. There are suitable conditions.

In the consideration of these all point of views, I turned back to the code blocks that contains regions and realized that in most places there are violation of Single Responsibility Principle. Of course there are places that I used regions to group variables, constructors etc. to make a class look clear (and I agree that they make the code seem good & understandable). But I am still not sure If I am going to stop using regions or not. One thing is sure that I am going to scrutinize from now onward ! Please share if you have recommendations, experiences or ideas about this subject. Looking forward to hear about your approaches !

Thank you for reading. See you in next post :)

#endregion

--

--