3 ms·
The Problem With Code Folding
- bdfh42 18y agoNow I like regions. They force programmers (by that I mean other people of course - my code is perfect ;) to at least apply some order to the code. If I want to review (say) the update method for a form I do not want to have to search through a mishmash of private declarations, methods servicing form control events, locally declared classes etc. etc. OK - Mr Atwood might have a point for really simple classes but when the code runs to a screen or two a little order helps a lot.
- cstejerean 18y agoordering code is not the same as hiding code. Perfectly fine to throw in some comments to provide hints to readers as to what this section does, but providing hints to the editor and having the editor automatically HIDE the code seems wrong.
- j2d2 18y agobut when the code runs to a screen or two a little order helps a lot. So... all the time? If you're not writing pages of code, you might be leveraging someone else's pages and pages of code.
- nirmal 18y agoI have only successfully used code folding when I write HTML. It's nice to finish a section of the page and close it and forget about it. I never use it for CSS though. I can never remember where that margin or padding is being added from :) I wish TextMate had proper folding logic for Python.
- stcredzero 18y agoNotepad++ does code folding for Python rather well, but it's Windows only.
- raganwald 18y agoThe title of this post got my hopes WAY up: Code folding? Applying catamorphisms to code? Way cool topic!!! But no, it was about how one specific language and one specific editor have some hints that play well together to hide and show blocks of code. Which is interesting even if you don't use that editor and that language, because it looks like another form of abstracting away some details. So for the people who use these Miscrosoft-specific tools, how is the argument against code folding different from the argument against extracting code into its own method? It seems to me that if I take a bunch of code and put it in its own method or class, I can move it out of sight, hiding its ugliness and exposition from view. Is code folding substantially different?
- baha_man 18y ago'...it looks like another form of abstracting away some details...' I think this is the main reason why I don't like it - it's an extra and inferior way of organising code. Automatically generated boilerplate code in ASP.NET is now hidden using partial classes, for example, which I think is an improvement.
- ScottWhigham 18y agoJeff's posts a year ago were so much more interesting than the past year's have been. sigh
- cdr 18y agoIf you don't like #regions, you don't have to collapse them. I like them personally because they aren't comments - they're comments that imply structure. It sets the high-level structure of the file apart.
- pchristensen 18y agoGenerally I either agree with or at least don't disagree with Atwood, but I completely disagree with his point on #region. It's a useful tool to manage long files, you can still search within it, it implies some kind of grouping of methods, etc. Even though the regions are collapsed by default, there's a little '+' next to is that took me about 3 seconds to figure out the very first time I used Visual Studio. Also, the complaints about code helping only one editor are disingenuous considering how little .Net coding is done outside of Visual Studio.
- silentbicycle 18y agoFor something similar in Emacs: (defun toggle-fold () "Toggle current buffer's folding." (interactive) (if selective-display (set-selective-display nil) (set-selective-display language-fold-column))) where language-fold-column is the column that function bodies (or methods within a class, etc.) are indented to by default. I set the language-fold-column variable in my language-mode hook. I find this helpful when skimming a module -- it limits it to function descriptions (docstring-like comments preceding the methods, if any) and the function type (in the OCaml sense); the function bodies are replaced with "...". With well-named functions (long sigh), this is often sufficient. Granted, it's based solely on indentation, but it works pretty well for several languages.