title: Procedure inside a procedure?
body: <p>I found this code online that has a procedure inside a procedure.
  I cannot understand why the author would have chosen to write it this way. 
  What I do notice is a recursive function being executed. </p>
  
  <p>Why did he not separate the procedures like most code I have seen. </p>
  
  <p>His implementation:</p>
  
  <pre><code>procedure XML2Form(tree : TJvPageListTreeView; XMLDoc : TXMLDocument);
  var
    iNode : IXMLNode;
  
    procedure ProcessNode(
      Node : IXMLNode; 
      tn   : TTreeNode);
    var
      cNode : IXMLNode;
    begin
      if Node = nil then Exit;
      with Node do
      begin
        tn := tree.Items.AddChild(tn, Attributes['text']);
        tn.ImageIndex := Integer(Attributes['imageIndex']);
        tn.StateIndex := Integer(Attributes['stateIndex']);
      end;
  
      cNode := Node.ChildNodes.First;
      while cNode &lt;&gt; nil do
      begin
        ProcessNode(cNode, tn);
        cNode := cNode.NextSibling;
      end;
    end; (*ProcessNode*) 
  begin
    tree.Items.Clear;
    XMLDoc.FileName := ChangeFileExt(ParamStr(0),'.XML');
    XMLDoc.Active := True;
  
    iNode := XMLDoc.DocumentElement.ChildNodes.First;
  
    while iNode &lt;&gt; nil do
    begin
      ProcessNode(iNode,nil);
      iNode := iNode.NextSibling;
    end;
    XMLDoc.Active := False;
  end; (* XML2Form *)
  
  
  procedure Form2XML(tree: TJVPageListTreeView);
  var
    tn : TTreeNode;
    XMLDoc : TXMLDocument;
    iNode : IXMLNode;
  
    procedure ProcessTreeItem(
      tn    : TTreeNode;
      iNode : IXMLNode);
    var
      cNode : IXMLNode;
    begin
      if (tn = nil) then Exit;
      cNode := iNode.AddChild('item');
      cNode.Attributes['text'] := tn.Text;
      cNode.Attributes['imageIndex'] := tn.ImageIndex;
      cNode.Attributes['stateIndex'] := tn.StateIndex;
      cNode.Attributes['selectedIndex'] := tn.SelectedIndex;
  
      //child nodes
      tn := tn.getFirstChild;
      while tn &lt;&gt; nil do
      begin
        ProcessTreeItem(tn, cNode);
        tn := tn.getNextSibling;
      end;
    end; (*ProcessTreeItem*)
  begin
    XMLDoc := TXMLDocument.Create(nil);
    XMLDoc.Active := True;
    iNode := XMLDoc.AddChild('tree2xml');
    iNode.Attributes['app'] := ParamStr(0);
  
    tn := tree.TopItem;
    while tn &lt;&gt; nil do
    begin
      ProcessTreeItem (tn, iNode);
  
      tn := tn.getNextSibling;
    end;
  
    XMLDoc.SaveToFile(ChangeFileExt(ParamStr(0),'.XML'));
    XMLDoc := nil;
  end; (* Form2XML *)
  </code></pre>
  
  <p>or modified implementation:</p>
  
  <pre><code>procedure ProcessNode(Node : IXMLNode; tn : TTreeNode);
  var
    cNode : IXMLNode;
  begin
    if Node = nil then Exit;
    with Node do
    begin
      tn := tree.Items.AddChild(tn, Attributes['text']);
      tn.ImageIndex := Integer(Attributes['imageIndex']);
      tn.StateIndex := Integer(Attributes['stateIndex']);
    end;
  
    cNode := Node.ChildNodes.First;
    while cNode &lt;&gt; nil do
    begin
      ProcessNode(cNode, tn);
      cNode := cNode.NextSibling;
    end;
  end; (*ProcessNode*)
  
  procedure ProcessTreeItem(tn : TTreeNode; iNode : IXMLNode);
  var
    cNode : IXMLNode;
  begin
    if (tn = nil) then Exit;
    cNode := iNode.AddChild('item');
    cNode.Attributes['text'] := tn.Text;
    cNode.Attributes['imageIndex'] := tn.ImageIndex;
    cNode.Attributes['stateIndex'] := tn.StateIndex;
    cNode.Attributes['selectedIndex'] := tn.SelectedIndex;
  
    //child nodes
    tn := tn.getFirstChild;
    while tn &lt;&gt; nil do
    begin
      ProcessTreeItem(tn, cNode);
      tn := tn.getNextSibling;
    end;
  end; (*ProcessTreeItem*)
  
  procedure XML2Form(tree : TJvPageListTreeView; XMLDoc : TXMLDocument);
  var
    iNode : IXMLNode;
  begin
    tree.Items.Clear;
    XMLDoc.FileName := ChangeFileExt(ParamStr(0),'.XML');
    XMLDoc.Active := True;
  
    iNode := XMLDoc.DocumentElement.ChildNodes.First;
  
    while iNode &lt;&gt; nil do
    begin
      ProcessNode(iNode,nil);
      iNode := iNode.NextSibling;
    end;
    XMLDoc.Active := False;
  end;
  
  procedure Form2XML(tree: TJVPageListTreeView);
  var
    tn : TTreeNode;
    XMLDoc : TXMLDocument;
    iNode : IXMLNode;
  begin
    XMLDoc := TXMLDocument.Create(nil);
    XMLDoc.Active := True;
    iNode := XMLDoc.AddChild('tree2xml');
    iNode.Attributes['app'] := ParamStr(0);
  
    tn := tree.TopItem;
    while tn &lt;&gt; nil do
    begin
      ProcessTreeItem (tn, iNode);
  
      tn := tn.getNextSibling;
    end;
  
    XMLDoc.SaveToFile(ChangeFileExt(ParamStr(0),'.XML'));
    XMLDoc := nil;
  end; (* Form2XML *)
  </code></pre>