Paragraphing

if 0 {Richard Suchenwirth 2003-11-18 - By "paragraphing" I understand assigning blocks of code ("paragraphs", as in PL/1) to a variable, and building up the control flow by referencing these paragraphs, which replaces long open brace ... close brace blocks with a single "word". Simple example (find the length of the longest string from a list):}

 proc maxlen args {
     set singleList       {[llength $args]==1}
     set expandSingleList {set args [lindex $args 0]}

     if $singleList $expandSingleList

     set res [string length [lindex $args 0]]
     set findLongest {
         set l2 [string length $i]
         if {$l2 > $res} {set res $l2}
     }
     foreach i [lrange $args 1 end] $findLongest
     set res
 }

if 0 { Note that "paragraphs" have to be defined before they are used, so a top-down style is not so easily achieved. An advantage is that the variable names for paragraphs can contribute further to "self-documenting code", with a slightly stronger commitment than pure comments thrown in.

Like so often, I'm not sure how useful this is, but it certainly is not possible in C (well, one could, with preprocessor macros) or a number of other languages...


TV At least it doesn't clutter your function name space...


(willdye) One problem is that most programmers are probably not used to seeing "$foo" and thinking "procedure foo". It might help to use a Hungarian-style naming convention, to indicate which variables are being used as paragraphs. For example, make conditional tests start with "is", like so:

   if $isASingleList $expandSingleList

Sometimes I use this convention for boolean variables. Extending the idea, you could use the prefix "do" to indicate a function-like paragraph:

   if $isASingleList $doExpandSingleList

...but to me it seems sufficient to give function-like names to function-like paragraphs. I don't have any trouble thinking of names like "$expandList" and "$findLongest" as functions. If there is resistance to the idea of paragraphs, however, it might help to tighten up a little on naming conventions.

RS No, functions take arguments, and have their own local variable scope; such paragraphs are more like code snippets that are substituted where they occur, sort of like C's #define SOMETHING ... macros, which can fully refer to variables in the "caller"'s scope.


rdt Well, how about the paradgm:

  set is(singleList)        {...}
  set do(expandSingleList)  {...}

  if $is(singleList) $do(expandSingleList)

???


}