mostlylucid

scott galloway's personal blog...
posts - 916, comments - 758, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Regions in code - evil or not-so-evil?

Hmm...this is a constant quandary for me, when to use and when not to use the #region...#endregion tags(?) in code. Currently I tend to use them to hide pointless stuff like ASP.NET control declarations and  generally increase redability in long chunks of code (so, parcel up my events, constructors etc...in regions) - but I in no way always do this. Now, a colleague of mine uses this stuff religiously, every class has
  • Declarations
  • Lifecycle
  • General
  • Methods
  • Properties
; with all the bits all hidden away when you open the file (which incidentally drives me nuts - I'm a code-speed reader and it breaks the flow :-)). So here's a question, when do you use regions and where...and more importantly, anyone know a way to make regions expanded by default???

Print | posted on Tuesday, February 15, 2005 3:51 PM | Filed Under [ .NET ]

Feedback

Gravatar

# re: Regions in code - evil or not-so-evil?

Not so evil. I use them ocassionally for the method that is long and I just can't factor it out.
2/15/2005 6:43 PM | Darrell
Gravatar

# re: Regions in code - evil or not-so-evil?

Darrell - yup, pretty much the same here - it *does* looks tidy with all the regions but I don't really see the utility of having so many...
2/16/2005 10:38 AM | Scott Galloway
Gravatar

# re: Regions in code - evil or not-so-evil?

You can expand by default by going into Tools -> Options, and under Text Editor -> C# -> Formatting, you uncheck the checkbox by "Collapse #region blocks when files open"

That should take care of it.

Now, as for me, I use them, but I always struggle with how I should use them. Should they relate to types of entities (public methods, private properties), or functional entities (order processing, order detail methods)? Right now, I'm doing types of entities.
2/17/2005 7:44 AM | Joel Ross
Gravatar

# re: Regions in code - evil or not-so-evil?

Aha...umm...I should've known that really :-S I'm pretty much of the same thinking as Joel - I usually end up putting them in long sprawling classes (which I can't or won't refactor) to help readability of particular functional areas - I've never really seen the point of surrounding 'types' with them, so properties, page declarations etc...except if they make the code less readable...
2/17/2005 10:21 AM | Scott Galloway
Gravatar

# re: Regions in code - evil or not-so-evil?

Thanks for that info from Joel.

Actually it is a standard here at work and it is really hard for me to read those unnecessarily lines. Each property, method, etc. has their own region <sigh>, not to mention the major groupings on this post.
3/10/2005 1:18 AM | joeycalisay
Comments have been closed on this topic.

Powered by: