Over the last few evenings I have been reviewing code done by a fellow developer, and have come to a conclusion.

The .NET code regions (aka Collapse Regions, Collapsible blocks and dear god knows what else in other languages) can really be misused.

Correct use of regions can group common sections of code – for example code that all interacts with the same property (you might have the property itself and also a number of private functions that work with or derive from the property), for improving readability (yes, we all know that we should not have units / classes that size but we all have right?) and generally for being neat (it is cool seeing it all shrink down isn't it?). BUT, likewise you can seriously go overboard.

This code in question has many levels of nest regions, and although it certainly does tidy the code up, it certainly does not help it’s maintainability.

I’m sure I’m not alone in the fact that I typically use a lot of keyboard shortcuts when I’m developing, but adding many many many regions, with many layers of nesting, this makes keyboard shortcuts for the region collapse / expand pretty useless. No matter what you do, you always seem to end up with too much or too little code on screen – either way, it makes development difficult.

Which brings onto a final point … if this is the case (i.e. the code is damn near impossible to understand at a glance due to the structuring in place), doesn’t this qualify as un-maintainable code?  Perhaps we should be setting up rules on TFS and the like to detect the layers of nested regions, and flag them for review when it sees someone going to excess?

Comments, as always, welcomed :)