Thursday, January 28, 2010

Code #region considered harmful

I simply hate #regions. Apparently I'm not alone.

In C#, a bit of code surrounded in #region...#endregion markers is folded shut by any decent IDE. The code becomes invisible until you ask to see it. This sounds like a good idea, but it really is not. It makes code *look* smaller, but the code is not smaller. It is more of a girdle than a diet. The code is instead better-hidden, an attribute I can hardly describe as a virtue.

There are plenty of reasons to hate long methods and long classes. The foremost is that you cannot see and grok them at a single glance. The programmer needs to read code quickly without misunderstanding it. The #region tag is one of the most sure-fire ways to make that problem considerably worse. The region worsens most of the problems long classes and long methods cause.

The #region is almost certainly an American or British policy decision. It makes things seem better while all the time making them considerably worse. Whenever we prefer 'seeming' to 'being' we have taken a wrong turn. It is like the alcoholic who hides his booze so he can seem fine while drinking heavily in secret.

Take off the #region tags and look at your code in the clear light of day. Does it seem like it doesn't belong there? Move it instead of hiding it. Does it seem like too much boilerplate? Then cut down your framework so it demands less of you. You can make your system better, instead of hiding the dirt. Really.


  1. Totally agree. The first time when I was introduced to regions in C# I had the some feeling.

    More feels like a feature you never wanted anyway. You know the bottom 80% features that are simple to think of, but nobody actually needs...

  2. I completely disagree. I use it to organize my code. If I am working on code related to a GridView control, why must I see all of the code related to other controls? In an object class, if I am working on a save method, why should I have to see all of the public properties. I use the #region to seperate public methods, private methods, etc. This saves me time from paging through the code.

  3. I don't hate every usage of #region, just every wrong-headed gratuitious use of #region, and especially those that mask the wrongness of long methods and long classes.

    The problem is, those wrong headed uses of #region are the only ones I've ever seen, and the only ones I've been tempted to include.

    When I see #region help me read the code then I'll like it. Until then, I suspect all uses are bad uses because that is precisely my experience.

    Please don't sweep your code under the rug. Some of us need to get under that rug.

  4. It happened again today. I needed to get into a code sample that had regions to organize the code that come from one interface or another.

    All the code I needed to see was in one folded region or another. It was a troublesome barrier to reading the code.


  5. Today the folded region said "unimplemented functions", and all but two or three were fully implemented. I am done with them. I'm removing them all and not asking for permission.