共用方式為


Is Code Folding that bad...?

I am a huge fan of Jeff Atwood and Coding Horror. I usually like his stuff. But I somehow didn't agree too much to his post where he talks about Code Folding (#region). I mean of course I also agree with a few things that he says, so let me put them down here:

 

Things that I do not agree with:

  • "Folding directives are Glorified Comments" : We don't fold code to accommodate the editor, we do it to accommodate ourselves. Code folding is more like indexing. I index my code so I know exactly where to go when I am reading it. It is like creating folders for files. I go to a specific file and know I want to check an event handler for example. so I open the Event Handlers Region and reach the exact event handler I require. Besides, I agree that traditional comments are better and more descriptive, but they have a different purpose than #region.
  • "Folding is used to sweep code under the rug" : Perspective, perspective... I would call it, "neatly arranging it in a nice closet and closing the doors instead of keeping everything out on an open shelf".
  • "Folding is used to mask excessive length" : Nuclear power could cause a nuclear holocaust or it could assist in the creation of an alternative source of power to keep cars running, Air conditioner fans turning and perhaps to achieve warp speed, who knows. Writing short and clean code is a matter of personal discipline just like checking in code into your repository before leaving for home. Even when there were no regions, people did write excessively long and convoluted source code. And #regions wouldn't hide code during inspection or code reviews. Everything would be reviewed when it is reviewed; folded or otherwise. In fact, it might make reviews more convenient. The reviewer unfolds everything and then systematically folds everything that has been reviewed.
  • "Folding can hide deficiencies in your editor" : This "scutwork" really helps good programmers to read code. And as I have read somewhere, good programmers read more code than they write. Code folding enables that.

Things that I agree with:

  • "The editor should automatically offer to fold up these common structural blocks to you"
  • "#region is criminally overused"
  • "Code should be written that doesn't need folding to be readable, clear and concise"

 

So Jeff, I love your writing, no questions... And you have a right to your opinions and to what you publish... But be gentle at times man...!

Comments

  • Anonymous
    July 11, 2008
    PingBack from http://blog.a-foton.ru/2008/07/is-code-folding-that-bad/

  • Anonymous
    July 11, 2008
    Yes, you've got a better perspective :). And here I thought that the biggest problem of #region was that it's actually part of the C# spec (versus say just an IDE thing).

  • Anonymous
    July 11, 2008
    Ctrl-M-O and Ctrl-M-P...enough said.

  • Anonymous
    July 11, 2008
    I tend to use regions liberally, even within methods. It's a level of organization that i use in conjunction with the member dropdown to jump around, but also when used within methods, they indicate good refactoring areas (in other words, put in a region now, but go back in and see if some of those regions can be extracted into methods). Also, when looking at a method as just its regions, it can become easier to see areas that can be optimized by rearranging some of the regions.