Just Say NO to region tags.

There was a time when I loved using region tags in C#, it seemed like a great way to keep messy classes organized. I wasn’t a very good developer, and my classes were messy, but they were organized.

Now let’s think about what that means for a second. You can take a class that’s ugly and make it look better, not make it actually be any better, but it would look better. I’ve come around to thinking that ugly code should look ugly, so you are more inclined to fix it, and (most importantly) less inclined to make it ugly in the first place.

Also, regions are like comments, that is, they can lie. Very often I’v seen public methods in the region marked “Private Methods” and vice-versa. Automated refactoring tools actually make this problem worse. If you do an extract method refactoring on a huge public method using ReSharper, it will put the newly created (private, probably) method right beneath it. And I don’t really have a problem with it.  I generally try to keep to the guidance of fields/constructors/properties/public methods/private methods, but if a class is so big that I find things in it quickly, organizing/alphabetizing my methods isn’t the solution.

The only time I don’t remove region tags when I see them these days is if it’s a region of automatically-generated code from the IDE, and now that the new versions of Visual Studio use partial classes, I don’t see them much anymore.

Leave a Reply

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 )

Google photo

You are commenting using your Google 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

%d bloggers like this: