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.
What 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.
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.
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.
I would imagine you despise
#pragma markin Xcode as well?
I think the one solid use case for
#pragmain Objective-C is separating private methods.
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.
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.
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.
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.
Thank you for this extension!
I’d love to use it in VS2017. Could you please update the extension in the VS gallery and make it VS2017 compatible? Or could you just release it as an open source project on github?
Hi Michael, Unfortunately I did not write the extension. It was written by Shanewho
Comments are closed.