SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Code regions: good or bad?

rated by 0 users
This post has 7 Replies | 1 Follower

Top 10 Contributor
Posts 2,284
icelava Devil [6] Posted: 01-29-2007 6:32 PM

I have finally penned down my thoughts about the use of Regions in Visual Studio.

What are your own habits and perception to this issue?

EDIT: Folks, I have failed to delineate the bounds of this topic clearly enough. I am talking about Regions within a method that is used to organise (or really, hide) code logic. I am not referring to the use of Regions to group members of a class or namespace. There is an important distinction here, and let's not be confused and think of them as them as the same.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 25 Contributor
Posts 157

I like to think of regions as a White Box, just like I have treated a library as a Black Box. I have created a template for my classes to follow a certain format, so that I am able to find that piece of code easily to work on. At the same time, I always bear in mind the need to refactor codes of blocks, though not always. I feel that refactoring is just like normalising tables, where I believe optimal comfort and ease is at third level.

For me, region is a great way to help us developers manage, structure and organise our codes.

Top 10 Contributor
Posts 2,284

bronkman:
I feel that refactoring is just like normalising tables, where I believe optimal comfort and ease is at third level.

That is a good perspective to have. But what of your observation of others' coding habits? I personally find an unhealthy majority using Regions as the sole means to "organise" code.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 25 Contributor
Posts 240

I don't use regions too often because of the pain of expanding+collapsing them to keep the code looking neat.

I might use them to group a couple of methods related to certain functionality, and only sometimes to make it easier to read the structure of a code block.

In many cases where others would use regions, I still prefer a simple comment. Where parts of classes are not dependent on each other, partial classes can also be a nice way to organise code.

Top 10 Contributor
Posts 2,284

hannes:
I might use them to group a couple of methods related to certain functionality, and only sometimes to make it easier to read the structure of a code block.

Perhaps let us constraint the scope of this discussion to regions within in a method, and not regions used to group methods together (which I do so habitually too like shelving similar books as a group).

In many cases where others would use regions, I still prefer a simple comment. Where parts of classes are not dependent on each other, partial classes can also be a nice way to organise code.

Commenting and partial classes can be whole volumes of topics by themselves :-)

As Martin Fowler advises along the lines of "each time I am tempted to write a comment, I refactor with Extract Method".

http://www.codinghorror.com/blog/archives/000749.html
http://blogs.msdn.com/ericlippert/archive/2004/05/04/125893.aspx

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 25 Contributor
Posts 157

Regions should be viewed as another means to "organise" code. In addition to having nested regions, I thought that having templates is one good way, so that all codes look and feel the same and makes searching much easier. I also use classes to group pieces of related functions and procedures together, and partial class may provide yet another means to organise (though I have yet to use that, which probably means my code is too big and it is time to review).

I have seen others only using regions as a means to organise codes, but they do not follow any patterns which means regions are more likely to be used as a way to hide chunk of codes. Object-oriented programming is, sad to say, uncommon, and I have found the same update function for the same object used in various places. One example is reading and updating a person particulars. What is worse is that the query is not standardised, and this may proof to be another potential break-down in future. So, as much as coding needs refactoring, even queries should be refactor-ed!

In any case, not that many people are really trained in coding conventions (as I am too) and I have only just found that in VS2003 and VS2005, there are so many tips and tricks that you can do. I felt like a frog in a well after reading "Best Kept Secrets in .NET" by Deborah Kurata!

Top 10 Contributor
Posts 762

I actually used regions to separate my private methods, public methods, constructors, properties, etc. Icelava, did i use regions correctly? Think so :P

Regards, Chua Wen Ching Believe in yourself, and you will succeed
Top 10 Contributor
Posts 2,284
I have editted my initial post to constraint the scope to get a better idea on what I am writing about.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Page 1 of 1 (8 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems