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
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.
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.
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.
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).
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.htmlhttp://blogs.msdn.com/ericlippert/archive/2004/05/04/125893.aspx
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!
I actually used regions to separate my private methods, public methods, constructors, properties, etc. Icelava, did i use regions correctly? Think so :P