I'm stuck updating other people's code at work. It's bad, bad code, and as such, it's hard to understand. It's not uncommon for me to stare at it glaze-eyed for several minutes before realizing that these 15 lines are just checking if a file exists.

So that gave me an idea. It sure would be nice if, once I figure out what a particular chunk of code does, I could somehow hide it from view, replaced by a one-line comment on what its function is. Then I could collapse the code one piece at a time, and when I'm all done, I'll have nothing but a big page of pseudocode explaining the whole process. Even better, if I could take those collapsed bits of pseudocode and collapse them together into a higher level of abstraction, and so on. In effect, I'd be generating the kind of comments that should have been placed there by the original programmer to begin with.

So I'm wondering. Does anything out there already do this? I can't see how it'd be useful for coding from scratch, but it'd be invaluable for making sense of legacy code, especially in big projects. Anybody know of such a beast?

I'm too sexy for my .sig.

Replies are listed 'Best First'.
Re: Can your editor do this?
by AgentM (Curate) on Apr 11, 2001 at 20:37 UTC
    Emacs comes with elisp scripts to do just that as well as similar things. You can arbitrarily collapse function bodies using a simple command sequence. Additionally, if you know elisp (I won't pretend that I do), you can really do anything. For example, if you decided that you wanted to collapse every third recursion of braces, it's a very short elisp script. In your case, I would throw some boundary separators around the block you want to hide. In fact, braces would be fine for this. Additionally, if you find yourself using common code among several programs that you decide that you are not interested in, separate it into a separate library. Even within your program, you should separate your operations into functions for clarity. Except for me, most programmers I know like to have main functions no longer than twenty lines. That should help you clean up.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Can your editor do this?
by stefan k (Curate) on Apr 11, 2001 at 21:02 UTC
    Of course XEmacs can ;-)
    take a look at this node...

    outline-mode and folded are the most important keywords. I can fold selected regions, too, if I remember things right, but I'm not completely sure about that....

    Regards Stefan K

Re: Can your editor do this?
by rchiav (Deacon) on Apr 11, 2001 at 21:39 UTC
    look at Komodo from Activestate. It allows for code folding along with much much more. It's available for Wintel platforms and there's a technology preview for Linux. I curently use the Windows version for scripts on Win platforms along with using Samba to export unix disk space via netbios. It's worth a look at least.


Re: Can your editor do this?
by infoninja (Friar) on Apr 11, 2001 at 20:23 UTC
    You could probably do this with FTE. I currently use it as my main editor, and I've used the folding functionality before, which I think could be adapted for your purposes.
      I have to agree with infoninja because right now I am quite enthusiatic about FTE, having recently found it (upon the suggestion of a fellow Perl Monk). With FTE, you can have it "fold" at your Perl sub statements, at locations specified by a regex, or at isolated specified locations.

      Take a little time to figure out how to customize your configuration, and you can set the folding and unfolding to occur by keystroke, function key, and/or popup menu. And you might want to change the colors/fonts also....I wasn't comfortable with the default install.

      I guess FTE is known only by word-of-mouth....sourceforge and other app sites don't even have a description of FTE...

        I took a look at it, but couldn't find the folding features anywhere. I sifted through the docs for a bit, and saw "skipped" by the folding stuff. It looks like, at least for now, folding isn't supported on the Win32 build of FTE. (which is what I'm stuck with at work) It's something to keep an eye on, though.

        I'm too sexy for my .sig.

Re: Can your editor do this?
by Desdinova (Friar) on Apr 11, 2001 at 21:39 UTC
    Not exactly what you are looking for but the Komodo IDE over at Activestate does have a feature for folding up loops subs and if/else blocks. It Doesnt seem to be configurable though. This does seem like a cool idea I'll have to add it to my wish list for the 'perfect editor'
Re: Can your editor do this?
by voyager (Friar) on Apr 11, 2001 at 20:39 UTC
    This doesn't directly answer your question, but (using your example) why don't you take those 15 lines of obstruse code and put them in a function at the bottom of the script (i.e., out of the way) and name that function "file_exists" and have it return true/false.