Gerben van Loon

Random thoughts on Microsoft Software Development and other related stuff

#region considered harmful

It’s been blogged about before but I still see people doing it.

With #region you can hide parts of your code. Why is this harmful? Well because why on earth do you want to hide parts of your code? Are you ashamed of it? Most people tend to use it because the method or class they’re writing gets too big to fit nicely on their screen. Please cut up your class or method in normal ways then by introducing new classes or methods, not but just stuffing away your code in a region.

The only thing when it was useful in the past is to place generated code or lot’s of properties for a class in a region. But C# introduced partial classes for this so please use them.

So no excuses anymore for using #region… no really not, please stop using them asap!

Comments

Gabriel Lozano-Moran said:

I don't agree. Using regions is actually very useful if you use it to hide all but the public interface of a class.

I recommend using regions to group members except for everything that is public. With tools such as ReSharper and Regionerate this is configurable and turns out to be actually very useful.

# November 12, 2009 7:37 AM

Kristof said:

I beg to differ. Partial classes is something for generated code. And then regions are for grouping properties. While I would never use regions to group public with public and private with private I do use them for grouping properties by usage, for example in a Global.asax, to group routes for different parts of my website together.

And by the way: I expand regions by default. So no hiding for me :)

# November 12, 2009 9:50 AM

Fadzai Chamba said:

My opinion on this matter is that it isn't so much to do with regions as it is to do with bad coding practices. People who write bad code will write bad code no matter what restrictions you put on them.

As you say, regions don't eliminate complexity, they just hide it. Lack of regions will not solve the complex or bad code problem. The developer who hid the bad code in a region, will now just place it in plain sight. Having no regions will just make it easier to spot.

With this in mind, I wouldn't go so far as to discourage regions completely, but encourage their prudent use. In many cases, I place event handling code in a region because I usually don't want to see this, the public interface will be in another region.

@Kristof - I have another use for Partial Classes. I like to think of them as separation of responsibility/concerns. One such scenario is for implementing complex interfaces for a business object that have nothing to do with the business rules, but more with plumbing. I would write the interface code in a partial class so that those working on the business rules won't get distracted by this plumbing code. Of course, the usefulness of this is arguable but I find it rather useful, though I don't come across this problem often.

# November 12, 2009 6:29 PM

Gerben van Loon said:

Thanks for your interesting comments guys!

I agree on using #region for member grouping when it's about fields or properties, but not methods. Methods is where generally the interesting stuff happens in your class, you don't want to put that away. I've also seen #region a lot within a large methods, think it's better to introduce a new private method for that region then.

# November 28, 2009 1:14 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 8 and 5 and type the answer here: