About Procedure Nesting

Nesting

Naming conventions in programming are a touchy subject.  Program text layout is equally touchy.  Reflections on the choice of a good programming language can lead to flame wars.  Procedure nesting is all but extinct.  I give here my own views.

Nostalgic note:

One thing I always sorely miss is Pascal’s name scoping:  in Pascal you can put a procedure P2 inside a larger one P1, and P2 then inherits the names from P1.  This is the very first type of inheritance (it originated in Algol), but object oriented languages do not have it! (the exceptions are Eiffel and Oberon).

Why nesting

Nesting is useful if you have a number of small routines that support a larger one but that don’t have much meaning elsewhere.  Say you have a handler H1 in which there is a tricky part of logic.  It would add to clarity if that would be implemented as a separate handler H2.  If H2 is put inside H1, then H2 cannot be called from anywhere else than within H1.  This type of code structuring is impossible in so-called “modern” languages, but if used makes your code very readable and also more robust.  It limits the number of names one has to worry about and reduces parameter passing.

on RecurseOverFolders fFolderPath,fFileHandler,fFolderHandler,fCaller

-- fFolderPath is the path to the folder we want to recurse over,

-- fFileHandler is the handler to call for each file, with the file path as argument

-- fFolderHandler is the handler to call for each folder, with the folder path as argument

-- fCaller is the name of the object where the call-back handlers are located.

local lRootFolder,lFolderDepth

on DoFolders fFolderPath,fFolderDepth

-- get the alphabetical mixed list of files and folders:

put empty into lItems FolderItems fFolderPath,lItems

put the keys of lItems into lItemKeys;

put (the number of lines of lItemKeys)/2 into nItems

-- lItems[i,1] is either "f" for a file, or "d" for a directory (folder)

-- lItems[i,2] is the name of the item

repeat with iItem=1 to nItems

if lItems[iItem,1] = "f" then

put fFileHandler&space&quote&fFolderPath&(lItems[iItem,2])&quote&","&fFolderDepth into lCall

send lCall to fCaller

else

put fFolderHandler&space&quote&fFolderPath&(lItems[iItem,2])&"/"&quote&","&fFolderDepth into lCall

send lCall to fCaller

RecurseOverFoldersDoFolders fFolderPath&(lItems[iItem,2])&"/",fFolderDepth+1

end if

end repeat

end DoFolders

put the defaultfolder into lDefaultFolder

if there is a folder fFolderPath then

put fFolderPath into lRootFolder

set the defaultfolder to lRootFolder

put 1 into lFolderDepth

DoFolders lRootFolder,lFolderDepth

else

answer "No such folder..."

end if

set the defaultfolder to lDefaultFolder

end RecurseOverFolders

 

Apart from eliminating three parameters to the inner recursive routine, it also shortens the name of that routine.  The handler "FolderItems" is not given here.  See also what this looks like with better indentation.

Procedure nesting is gone I’m afraid...  Pascal and Betamax were better, but C and VHS won.