We're hiring!

We're actively seeking designers and developers for all three of our locations.

I Hate #regions

As software consultants, we work in many environments. Most of the time we are working in our own environment on a brand new project, but sometimes we work with a team of client developers on existing software. In the later case, we have to be mindful of their coding standards. One practice that drives me nuts is code organized into #regions.

What’s Wrong with Regions?

Microsoft introduced #regions to help organize big files into understandable chunks. In my opinion, if your code can be broken up into regions, then it can be refactored into smaller files. I try to write my classes with single responsibility in mind, where a class has a single responsibility. Therefore regions are not required to organize the code into responsibilities.

Regions are also used to separate private, public, and protected variables, properties, and functions. This is where I see them used most often. If your class is small enough, there is no need to organize them into regions.

Collapsed view in Visual Studio

Collapsed view in Visual Studio

Outlining mode visual studioWhat makes matters worse is the way Visual Studio collapses regions by default when you open a file. This creates extra work for me, expanding all of the regions just so I can see the code. I also use incremental search in Visual Studio (Ctrl i), which fails to search in collapsed regions. This is very annoying. (FWIW, you can turn this behavior off by going to Tools – Options – Text Editor – C# – Advanced and turn off outlining mode.)

Making Regions Easy to Ignore

I recently worked for a client that required the use of #regions even if the class was very small. This bloated the code with unnecessary adornments. All I want to see is the code and not a bunch of #regions. Below is an example where one line of code is bloated into three.

Without i hate regions

I went searching for a solution. It turns out that Visual Studio has an extension appropriately named “I Hate #Regions”.  It not only automatically expands the regions when you open the file, but it also changes the font size and color of the regions, so that you hardly notice that they are there. Here is the same code with I Hate #Regions turned on.

With i hate regions

As you can see, your eyes are drawn to the code and not the regions. This extension does not modify the code in any way, so other members of the team still see the regions as they normally are. It simply changes how they are viewed in Visual Studio.

Mike Woelmer (8 Posts)

This entry was posted in Developer Tools and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.


  1. Chris Carr
    Posted April 16, 2014 at 1:02 pm

    I would imagine you despise #pragma mark in Xcode as well?

  2. Ryan Abel
    Posted April 16, 2014 at 1:16 pm

    I think the one solid use case for #pragma in Objective-C is separating private methods.

  3. Mike Woelmer
    Posted April 16, 2014 at 1:44 pm

    Regions are more annoying than pragmas because Visual Studio will collapse the code within a region by default. Imagine opening up a file in Xcode and only seeing the pragmas.

  4. Posted April 19, 2014 at 2:39 pm

    I generally agree with “smaller chunks is better” idea, but in my experience, there is one thing that has eluded my ability to break down into smaller pieces: GUI Widgets.

    The problem with widgets is that they must be a single class, and that class covers all aspects of the widget. Data management, GUI drawing, user interaction, and a bajillion framework integrations, all tied into a single class. Of course, all the other components depend on the class state, and 90% of the code is not reusable (esp if you only need one custom widget).

    The widget I’m referring to is my ImageSpace widget for GTK, part of my Cropper project. It has datastore integration, internal state management, modal (multi-tool) mouse interactions, drawing, coordinate space transformations, and other tasks. About the only thing removable is tool handling (mouse interaction). As far as I can tell, everything else is tied to class state too much to pull out, and it wouldn’t be reusable anyway.

    That’s my 900 line nightmare.

  5. Posted April 21, 2014 at 4:49 pm

    Hi Jamie,

    You mentioned that your class had several responsibilities like datastore, state management, drawing, etc. Those would be great to break out into separate interfaces and classes. Consider reading the “Refactoring: Improving the Design of Existing Code” by Martin Fowler for some ideas on how to refactor your code.

  6. Jim
    Posted August 17, 2014 at 9:14 pm

    I rarely, if ever, use #region myself, but I love it that Microsoft uses it to hide their automatically generated code (which can be large and which I rarely want to look at or change) and automatically collapses them.

    So perhaps #region is a good feature that (like most) can be misused.

    I admit that the first thing I do on opening a file is usually to use the keystrokes to collapse it – so I can easily see the functions and navigate with minimum scrolling. Perhaps I’m in the minority, but I do see posts by people who want files to open collapsed to the function level by default.