Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

Are C# regions a good thing??

1 min read

Well, I'd say it depends. If you use regions to logically group your code in your class, then it may be a good thing 'cause people come to your class and see something like

public class MyDummyClass : IMySuperInterface
{
public MyDummyClass(...)
{
...
}

#region Properties
...
#endregion

#region Utility
...
#endregion

#region Implementation of IMySuperInterface
...
#endregion
}
In such case it may increase the organization of the code inside your class. I wouldn't say it increases the readability of your class since it's just a matter of code reorganization.

Many times however you find people using regions like
public class MyBLClass : IMySuperInterface
{
public MyBLClass(...)
{
...
}

public IList ReadAllOrComputeWhatever(...)
{
if(parameterA >= someNumberOrString)
{
#region Check for blablabal
// 30LOC+ hidden here of CC 10+
#endregion
...
}
}
}
...then regions suddenly aren't that nice any more. I find that a lot of people actually use it for hiding their ugly, "smelling" pieces of code which would highly have the need of being refactored. I think this is very much related to this here.

So...
  • logically grouping: good thing, though it depends on preferences (I personally don't like it that much)
  • code hiding: bad practice. Sign of a code smell and you should consider to refactor it.

Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus