
Ask HN: Does Anyone Use Code Folding? - eugenhotaj
Does anyone use code folding? If so, what do you feel are the biggest benefits it gives you?<p>Also curious to see if anyone has any strong reasons against it?<p>The main benefit I can see is that you can use the folded code as an outline of the classes&#x2F;functions in the file.
======
ksaj
Code folding is awesome for Lisp. Once a function is working, you rarely have
to look at it again. Seeing just the first part, eg: +(defun squared (x) tells
just about everything you need to know for reference. If not, the
documentation string can easily start on that line, too.

Of course a fold that pops up the documentation string on mouse-over would be
the cream on top.

------
davidkneely
I use code folding in Xcode to quickly check if a method is being used in the
code base. Code folding allows me to shrink down the amount of scrolling I
need to do. Code folding allows me to access the methods I'm checking on that
might be "below the fold."

------
galacticdessert
Yep, and I here when people do not align line comments properly

~~~
galacticdessert
Hate*, not here :)

------
kazinator
I use code folding when editing the TXR man page, because it's close to 69000
lines long. I developed some custom Vim folding rules for that. Here is what
it looks like fully folded:

    
    
      +--353 lines: .\" t '\" vim:set syntax=groff:-----------------------------------------------------------------
      +--  3 lines: .SH* NAME---------------------------------------------------------------------------------------
      +--  5 lines: .SH* SYNOPSIS-----------------------------------------------------------------------------------
      +-- 54 lines: .SH* DESCRIPTION--------------------------------------------------------------------------------
      +--563 lines: .SH* ARGUMENTS AND OPTIONS----------------------------------------------------------------------
      +-- 47 lines: .SH* STATUS AND ERROR REPORTING-----------------------------------------------------------------
      +--2290 lines: .SH* BASIC TXR SYNTAX--------------------------------------------------------------------------
      +--7519 lines: .SH* DIRECTIVES--------------------------------------------------------------------------------
      +--2238 lines: .SH* TXR LISP----------------------------------------------------------------------------------
      +--47280 lines: .SH* LISP OPERATOR, FUNCTION AND MACRO REFERENCE----------------------------------------------
      +--4613 lines: .SH* FOREIGN FUNCTION INTERFACE----------------------------------------------------------------
      +--1306 lines: .SH* LISP COMPILATION--------------------------------------------------------------------------
      +--1114 lines: .SH* INTERACTIVE LISTENER----------------------------------------------------------------------
      +-- 98 lines: .SH* SETUID/SETGID OPERATION--------------------------------------------------------------------
      +--149 lines: .SH* STAND-ALONE APPLICATION SUPPORT------------------------------------------------------------
      +--  4 lines: .SH* DEBUGGER-----------------------------------------------------------------------------------
      +--852 lines: .SH* COMPATIBILITY------------------------------------------------------------------------------
      +--315 lines: .SH* APPENDIX-----------------------------------------------------------------------------------
    

Then if we open, say, the FOREIGN FUNCTION INTERFACE section, that looks like
this:

    
    
      .SH* FOREIGN FUNCTION INTERFACE
    
      +--- 35 lines: On platforms where it is supported, \*(TX provides a feature called the------------------------
      +--- 21 lines: .SS* Cautionary Notes--------------------------------------------------------------------------
      +--- 79 lines: .SS* Key Concepts------------------------------------------------------------------------------
      +--- 53 lines: .SS* The FFI Type System-----------------------------------------------------------------------
      +---428 lines: .SS* Simple FFI Types--------------------------------------------------------------------------
      +---1091 lines: .SS* Parametrized FFI Type Operators----------------------------------------------------------
      +--- 12 lines: .SS* Additional Types--------------------------------------------------------------------------
      +--- 69 lines: .SS* Endian Types------------------------------------------------------------------------------
      +--- 95 lines: .SS* Bitfield Allocation Rules-----------------------------------------------------------------
      +--- 44 lines: .SS* FFI Call Descriptors----------------------------------------------------------------------
      +---491 lines: .SS* Foreign Function Type API-----------------------------------------------------------------
      +---544 lines: .SS* Foreign Function Macro Language-----------------------------------------------------------
      +---157 lines: .SS* Zero-filled Object Support----------------------------------------------------------------
      +---177 lines: .SS* Foreign Unions----------------------------------------------------------------------------
      +--- 91 lines: .SS* FFI-type-driven I/O Functions-------------------------------------------------------------
      +---227 lines: .SS* Buffer Functions--------------------------------------------------------------------------
      +---997 lines: .SS* Foreign Arrays----------------------------------------------------------------------------
    

The Vim setup is this:

    
    
      :au BufRead,BufNewFile txr.1            set foldmethod=expr | set foldexpr=TxrManFoldLevel(v:lnum)
      :function TxrManFoldLevel(lno)
      :  let s:line = getline(a:lno)
      :  if a:lno == 1
      :    return ">1"
      :  elseif s:line =~# '^\.SH'
      :    return ">1"
      :  elseif s:line =~# '^\.SS'
      :    return ">2"
      :  elseif s:line =~# '^\.NP' || s:line =~# '^\.co\(NP\|SS\)'
      :    return ">3"
      :  elseif s:line =~# '^\.desc' || s:line =~# '^\.TP'
      :    return ">4"
      :  elseif s:line =~# '^\...IP' || s:line =~# '^\.IP'
      :    return ">5"
      :  elseif s:line =~# '^\.TH'
      :    return "<1"
      :  elseif a:lno == 1
      :    return ">1"
      :  elseif getline(a:lno-1) == "" && s:line != ""
      :    return ">6"
      :  else
      :    return "="
      :  endif
      :endf
    

It has quirks, but does the job more or less.

------
mantawolf
I use it constantly. It helps visualize code flow in larger files by
collapsing branches of loops or conditionals.

