Why I Dislike the #region Directive

I am in love Visual Studio.  Me and Microsoft have a secret relationship where I can secretly be in love with Visual Studio, all things .NET, and SQL Server, while publicly trashing their horrible inventions like Windows Mobile phones and Windows Vista.

To me, the #region directive is a horrible nuisance and an annoying invention.  There are two reasons that most people use #region, and both produce ugly code smells.

Example # 1 — “I have a really huge method that I want to collapse into sections”
Let me just go ahead and say that, if your method is that big, you should consider refactoring into smaller methods.  As developers, we want to produce beautiful, encapsulated OO code, right?  And part of that is dividing functionality into small components and functionality.  (As with anything, of course, there are exceptions and sometimes you just have to make a Super Method that is 100 lines long).  Typically, I go by the rule that one of my professors often preached–methods should not be more than 20-30 lines of code.  If they go over that, you should be able to (easily) refactor that into a sub-method.

Example # 2 — “I want to be able to use collapsing/code folding in Visual Studio without collapsing comments”
I like being able to hit that handy “Ctrl-M, Ctrl-O” combo and collapse everything to definition.  However, it irks me too that my comments are also collapsed.  I wish I could see my comments.  If we are constantly folding the code and hiding the comments, then why do we write the comments in the first place?  (There is, however, a notion that I firmly believe in that comments are bad–but that is another topic for another day).  Nonetheless, say we want to keep comments visible in our IDE when we collapse to definition.  So why should I have to write extra code (code that serves no purpose, mind you) just to change the way MY development environment looks or works?  Think about how much extra time you are going to spend to wrap EVERYTHING in a #region directive.

Then, along those same lines, think about refactoring.  When you move code around, you have to constantly make sure that your #region start and end directives are still in place.  If you move a section around, you have to make sure that you didn’t remove the #endregion tag with that code you just moved.  Annoying.

And I’ve also seen developers start a #region directive inside of one method, and end it outside of a second method (intentionally, too).  Yikes.

If we really want to be able to see our comments, we can write macros in VS to accomplish the code-folding without collapsing comments (this will be my next project–once I learn how to do a macro).

I love this quote from Kris on this post over at stackoverflow.

It’s one of my pet peeves that #regions absolutely stink. They might do something very obscure in the background (in wich case I might adjust my opinion) but I doubt that. What I do believe is:

  • #regions don’t increase readability
  • #regions make refactoring code harder because it’s easy to accidentally cut out one end but forget or miss the other, leaving you with a mess that won’t compile for no good reason.

Most of my c# career was spent maintaining existing code, in a team environment where I didn’t get to decide the coding standards so the grass might be greener elsewhere, but seriously, who thought of a dumb way to make a comment look like a compiler directive and made it able to break a build?…

Your Thoughts?

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 )

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