What is the groups opinion on nested routines? 
Author Message
 What is the groups opinion on nested routines?

This is a debate we could have had in the seventies.

The present view is more that units (packages) have made nested
routines very rare and something to be used if there is a technical
reason to have them - like for iterators.

The fact is that code with nested routines is more difficult to understand
and prove correct.  Do not nest routines if you have another way
of doing things.

Jean D. Ichbiah



Wed, 18 Jun 1902 08:00:00 GMT  
 What is the groups opinion on nested routines?


Quote:
>This is a debate we could have had in the seventies.

>The present view is more that units (packages) have made nested
>routines very rare and something to be used if there is a technical
>reason to have them - like for iterators.

>The fact is that code with nested routines is more difficult to understand
>and prove correct.  Do not nest routines if you have another way
>of doing things.

I agree with this.  Generally code is clearer if it doesn't use nested
routines, but sometimes they solve otherwise hard technical problems.

For another example besides an iterator: you can reduce the stack
usage of a recursive routine a lot by offloading stack-intensive
operations to a nested (non-recursive routine).   This comes up in BP
with string operations:  they generally cause one or more 256 byte
temporary values to be allocated on the stack.  If the routine doing
the string operation is called recursively, your stack gets eaten up
very quickly.

On the other hand, if you move all the string operations to a nested
procedure, the temporaries are only allocated while it is running, and
you don't have useless copies of them sitting around in all the
inactive stack frames of the earlier recursive calls.

This makes a big difference in something like a directory tree
traverser:  if you build your new directory name in the main routine,
you'll probably be limited to a depth of about 20 before your stack is
full, while if they are put in a nested routine, it's not hard to
reduce your needs to 10 or 20 bytes per level, allowing you to easily
go a thousand directories deep (far more than DOS could handle).

Duncan Murdoch



Wed, 18 Jun 1902 08:00:00 GMT  
 What is the groups opinion on nested routines?
I've been programming in Pascal for almost five years now, and I was'nt
aware that nested procedures was possible *humbled*

Personnaly I can't see that I'll use it myself, as someone else pointed
out units can take the place of such things, mind you now I know about it
it'll probably start to creep into my code!

Oh well back to hacking my 3D engine together.
Anyone got any old routines for polygon bitmapping?
Oh and what is the maths algorithum for light sourcing?

{*filter*}vampyre         - At Liverpool John Moores University



Wed, 18 Jun 1902 08:00:00 GMT  
 What is the groups opinion on nested routines?

Quote:
>I've been programming in Pascal for almost five years now, and I was'nt
>aware that nested procedures was possible *humbled*

>Personnaly I can't see that I'll use it myself, as someone else pointed
>out units can take the place of such things, mind you now I know about it
>it'll probably start to creep into my code!

Units _can_ take the place of such things, but just because they _can_
doesn't always mean they _should_.  Reminds me of the old saying along
the lines of " if all you have is a hammer, you'll find lots of
things to do with the hammer".

1.)  Simple little test:

     proc calculate_squares(Num: integer);
        func square(i: LongInt): LongInt;
        begin
           square := I * I;
        end;
     var i: integer;
     begin
        If Num < 0 then Num := - Num;
        If Num > 45000 then Num := 45000;  { to keep Num*Num in LongInt range.}
        For i := -Num to Num do
            Square(i);
     end;

     Now move the "square" function off to a unit all by itself and leave
     the "calculate_squares" in the main body of your little test program.

     Time both versions.

     Wow! Ain't that little nested routine a lot faster!    

2.)  Using the example above, if the "squares" func is only
     used  in one place why not leave it as a nested function simply
     so that when you are trying to figure out what its "owner" does,
     all the code you need is right there - no need to track down
     which unit its in, scroll through that unit to find it, etc.  
     Ie., ease in understanding and maintaining your code should count
     for something.  

     In other words, put things that belong together, together.  
     Sure, sometimes that means putting things together in the same
     unit, but sometimes it means putting proc and funcs where they
     are used rather than somewhere else.

Just my 2 bits worth.

Rob



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. What is the groups opinion on nested routines?

2. Am I in the right group?

3. Am I in the Right Group?

4. Anybody knows how to create multiple group in Quick Report (A group inside another group) ?

5. Please help with nested SQL !

6. parameterized nested query

7. Nested SQL help needed

8. Nested ClientDataSets

9. nested master-detail?

10. Nested Queries

11. Nested SQL statements?

12. Nested SELECTs?

 

 
Powered by phpBB® Forum Software