We Don’t Need No Regions

If your code reaches a level where you want to hide it behind regions then you have a problem that regions won’t solve. Regions are good to hide things that you don’t want to have knowledge about such as auto-generated code. Normally, when you’re developing you end up reading more code than you write it so why would you want to complicate the reading process.

I, for one, would love to have that one discussion around regions where someone convinces me that they solve a problem that has no other alternative solution, but I’m still waiting. The most frequent argument I hear about regions is that they allow you to structure your code, but why don’t just structure it using classes, methods and all that other stuff that OOP is about because at the end of the day, you should be doing object oriented programming and not region oriented programming.

Having said that, I do believe that sometimes is helpful to have a quick overview of a code file contents and Visual Studio allows you to do just that through the Collapse to Definitions command (CTRL + M, CTRL + O) which collapses the members of all types; if you like regions, you should try this, it is much more useful to read all the members of a type than all the regions inside a type.


2 thoughts on “We Don’t Need No Regions”

  1. I find them useful for grouping properties into functional areas in classes that have lots of properties, or even just for separating out const declarations, static properties, properties, instance variables, constructors and init functions, methods, event handlers etc. I also find them useful to group everything in a class that helps it implement a given interface.

    1. I do understand the use case behind having functional areas and already worked on projects that used that approach. However, sometimes it not so clear the functional separation between properties and methods on the same class and this leads to extra effort being required to maintain and define the appropriate regions. Another thing that is required is a well disciplined team, otherwise things get messy very quickly because it is also not easy or almost impossible to validate the proper usage of regions in a automated way.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s