Friday, October 06, 2006

No to #region!

The #region directive in C# was invented so that a code file can be split up into collapsable regions to aid navigation. I can stand them, and here's my list of region irritations...

  1. They're like comments, they don't execute so it's easy to have regions which tell you something completely wrong. How about the region '#region public properties' that contains nothing but private methods. Yeah, I've seen that enough. Martin Fowler in his excellent book Refactoring says "When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous". The same goes for regions, if you feel a region comming on, maybe you need to refactor, which brings me to...
  2. If you're code file is so large that it needs regions to keep it organised, maybe your code file is too large. Visual Studio likes to have one class per file and classes shouldn't be so large that they need to be split into regions. Maybe when you feel a region comming on you should try refactoring your class into more smaller classes. What about regions that split up a method into easier to understand segments? You have regions inside methods???? That's too far gone!
  3. What's wrong with the great tools that come with Visual Studio for helping you navigate around your code. I think one look at the Class View is worth a million stupid regions and if what you see in the class view doesn't make any sense then you should really get a copy of 'Refactoring'. Well named methods and classes in a well designed object model should make your code easy to understand and navigate.

So just say no to regions, I'm sick of clicking on those stupid little + signs!

4 comments:

Iain said...

Respectfully, I disagree. Ctrl+M+L is your friend. Like anything it's something that can be abused as much as used. I like to use regions to split a class into:

- Fields
- ctors
- Private methods
- Public methods
- Properties

It's also a good way of gently enforcing a coding standard within a group.

GrumpyYoungMan said...

"They're like comments, they don't execute so it's easy to have regions which tell you something completely wrong."

I detest this perennial lame excuse by programmers. Newsflash: it's our job to update the comments as well as the code.

If comments (or region labels) are found that are out of date, that's a black mark against the programmer who failed to update them. Enough black marks results in disciplinary action. End of story.

Mike Hadlow said...

Hi GrumpYoungMan,

Great comment :) I don't agree with you though.

It's not a lame excuse, it's something I've seen in code that I've had to pick up. And that's not just once either. OK, so you can blame the programmer that's now moved on, but it would be much simpler if the code was nicely factored in the first place rather than simply being divided up with non-executing region directives. Surely if you can split something up with regions you are also capable of refactoring your code into meaningful methods and classes?

Michael Lidgren said...

I completely agree. If you need regions to organize your code; there's something wrong with it.