icelava.net

Sketchpad Anywhere
Welcome to icelava.net Sign in | Help
in Search

Code regions - more or less readable?

Last post 04-05-2007, 12:39 by icelava. 1 replies.
Sort Posts: Previous Next
  •  01-29-2007, 4:34 1441

    Code regions - more or less readable?

    I recently gave a talk to my fellow developer colleagues which was ambitiously themed "How to help your colleagues develop faster". It was not some magic herbal tea or soup that instantaneously buffs developers with 100% increased speed for code churn. It was primarily dealing with the thought process that developers run through their minds when programming - are we writing code for the computer to execute (and thus deliver the assigned task/feature), or are we writing instructions for another developer to read?

    That overall topic per se is not what I wish to record here, however.

    What I wish to highlight is an interesting point brought up during the my opinions on the evil of lengthy code blocks - the use of Regions in Visual Studio. In fact that was something I wanted to write about (I even made screen captures) but lost out in the Grand Scheme of Life. The talk that night has reminded me to note this down quickly once and for all.

    The Region feature has always been a great source of ambivalence to me everytime I use the IDE. Have a look at the following screen shots (you may choose to randomly look at the right instead of the left picture first) :

    Region to the rescue Region abuse

    As with the left illustration, Regions are a sanity saver by allowing a developer who is taking over (or just came back from coffee break) to encapsulate and hide away lengthy code irrelevant to a certain level of abstraction. It affords a developer the peace of mind to realise which section of code belongs to which block.

    On the other (right) hand, VS-bred developers get so comfortable with Regions that they instinctively rely on it and hardly ever refactor what is in actuality ugly messy unreadable chunks of code. It is very common to see methods grow to the size of thousands of lines. Way too common. One key point that perhaps developers do not realise - the Region is not an organising feature of the programming language; it is an add-on illusion brought about by the IDE.

    So what, you ask. Who is actually going to develop .NET without VS anyway? In fact, who knows how to develop outside of VS?. True enough, VS provides so many productivity features that it is actually silly to gung-ho it with Notepad. The point here though, is that Regions do not improve what is already poorly-factored code. They merely hide it. Like a bunched of objects hastily thrown into drawers, they still appear as cluttered mess when pulled out into view. It is helpful by making the desk appear clean for awhile, but it is no substitute to well-factored code blocks - the ultimate contributor to readable code.

    The advice? Use Regions consciously and judiciously. Like what Martin Fowler mentions in Refactoring, once your method has alot of Regions, or worse, mult-nesting of Regions, it is time to consider refactoring to their appropriate level of abstractions. Recognise that the code you write says alot about how neat and organised your thinking is. Even more so than the physical mess on your desk.

    Filed under:
  •  04-05-2007, 12:39 1469 in reply to 1441

    Re: Code regions - more or less readable?

    Found another developer who shares the same sense of dread about Regions.

    http://blogs.msdn.com/rido/archive/2005/05/23/DontLikeRegions.aspx

    Filed under:
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems