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...!