Statistics

Total Posts: 34
This Year: 0
This Month: 0
This Week: 0
Comments: 0


RSS 2.0

Recent Posts


On this page....


Archives

 Full Archives By Category
 2007 Calendar View

Categories


Admin

Sign In

Acknowledgments

DasBlog Theme Design by: Tom Watts
E-mail: Send mail to the author(s)
Theme Image by: dreamLogic

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

 Thursday, November 22, 2007

I guess this is quite a dramatic blog title statement but unfortunately it is true. I am often cursing over regions I find in other peoples code. This blog post will tell you why generally I don't like them.

Both C# and VB.NET allows for a feature called regions. Regions at class level may look something like this:

region1.JPG

Now, the benefit of using regions is that you may collapse them easily when you are not currently working with them. Many regions could then look like something like this

region2.JPG

 Of course, in a real scenario, you would have a more real case choice of selecting regions as to group class members. I’ve seen that it is common to group methods and properties etc. according to member accessibility level, i.e. private, protected, internal etc. Also, it is quite common to group regions of common member functionality;  kind of like having inner classes but categorized as regions instead.

To make things worse, C# allows regions to be defined within members too:

region3.JPG

So, how come I don’t like this? Well I have a couple of reasons for this:

1.       Regions at class level make the code harder to work with. I think most advanced (lazy :-)) .NET developers know that the Visual Studio short cut keys Ctrl + M + O collapses all methods and regions in a class. This action gives a quick and good overview of either the members or regions in the class. Even though you may expand/collapse a specific region with Ctrl M + M, it is quite a hassle to have to expand both regions and methods to get an overview of the actual code within the method. I think that an overview of the functionality of a class is best given by the natural language constructs themselves without using regions. Once you start adding regions to you code, you add an additional expand/collapse step in order to be able to start working with the actual code within you methods. This makes the code harder to work with.

2.       Often, when regions are used at class level, it is done because there are too many members in the class to keep track of. This means that the cohesion of this class is low which is bad. Another, very similar principle, founded by Robert C. Martin, is the Single Responsibility Principle. Regions tend to make you violate this principle since classes become too large and contain too much mixed functionality. More on this in a future blog post…

3.       Regions within methods share a common symptom. When regions are used within a method, it is likely done so because the method is very long. Too long if you ask me. Methods should be short and be easy to read and understand. Otherwise they will break what I usually call the Divide and Conquer principle.  In short Divide and Conquer in software design, as I choose to define it, is a major problem split into many smaller problems by decomposing a method into many smaller methods. This is how you understand and solve a major problem a lot easier since you don’t have to be keeping too much detailed information in your mind at the same time.

4.       When you use regions within a method, it is much more difficult to track the actual data flow between different regions. If local variables are used, and not state fields, you can easy see the API for each method. I.e. you see what input data each method needs and what output data it delivers. Regions within methods do not give you this and the code becomes much more difficult to understand.  

5.       Regions within large methods have another problem: if you receive an exception in operation and if you are logging the stack trace of the exception, you have a much more difficult time to track where the actual error occurred. With smaller methods instead, you will at least receive information in which method the error occurred. (…and of course you should not compile your code with the debug flag to true in operation environment…)

Well, these are the negative consequences of regions that I have come up with so far. Of all these negative aspects of regions, I think the potential violation of Single Responsibility and Divide and Conquer Principles are the worst ones. From what I’ve seen, regions tend to make programmers forget that they are developing in an Object Oriented language and instead program as if using a functional programming language. Sure, functional programming languages have their strengths, but improved readability and therefore better maintainability is not one of them.

So. Is there a place when regions actually are ok to use? Well, if someone has placed a region to group some less significant members of a class, I might accept it although I probably would not define the region myself. However, I will never ever accept regions within methods since I think this violates the principle of Divide and Conquer.

Though, I have found one case when I actually use regions myself. I find them excellent to use when I am constructing demos for course material. Regions let me hide code and comments that I don’t want my students to see yet. Alas, I use regions strictly for educational  purpose. Wouldn’t the world be a better if everything just was a big nice demo…. :-) However, it is not. So lay off those regions in your code. Please.

Monday, December 03, 2007 8:41:54 AM (GMT Standard Time, UTC+00:00)
Amen! Regions are evil! :-)

We should start an Anti-Region club
Tuesday, December 04, 2007 9:44:04 PM (GMT Standard Time, UTC+00:00)
we definetly could. :) But then again. Just a couple of days after I had finished writing this blog post I actually found another reason to utilize regions myself. I was writing some functional tests in order to validate the actual string response returned by an web application so that it conformed exactly to a previous response (refactoring old code). Anyway, these return strings were rather long and naturally I placed them in a separate class dedicated for only this purpose of containing these strings. But the only way of to actually getting an overview over these long string values was to use regions...

In Swedish there's a saying: the rule that confirms the exception... I guess I found two of them for region utilization. So far. :)
Tuesday, November 24, 2009 12:06:09 AM (GMT Standard Time, UTC+00:00)
Badly need your help. The reason why worry kills more people than work is that more people worry than work.
I am from Africa and bad know English, give true I wrote the following sentence: "Far all documents look in wealthy quantities; nor are they converted by value to move this business of variable person, mortgage note."

THX :-), Dyllis.
Monday, December 21, 2009 8:10:22 PM (GMT Standard Time, UTC+00:00)
Give please. The people who are regarded as moral luminaries are those who forego ordinary pleasures themselves and find compensation in interfering with the pleasures of others. Help me! There is an urgent need for sites: Most, become an special pocket interview, leave, think therefore 1000 ample players, and invest it away in constantly eight effects and differently have to practice until the preventing development, online poker.. I found only this - [URL=http://www.martininsurance.us/Members/OnlinePoker]best online poker rooms[/URL]. Online poker, happy case is a online card these services, both for the other poker and for those who soar fact for a today. The question includes the online amount tables tell no room by the shuffle, although it can be dealt for sensitive documents of poker, online poker. Thanks :cool:. Robi from Bulgaria.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, i, strike, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview