Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-other / 2018-07

To nest or not to nest

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Santiago A.

Hello:
I use a lot nested procedures and functions, in fact, whenever I can. For instance:  I divide long bodies in small nested procedures. In OOP, If a private method is called only by a procedure, I convert it into a nested routine. In case statements, if the body of a label has more than a few lines, it calls to a nested proc. If statements, if the THEN and the ELSE have long bodies, I convert them into nested procs. I love nested routines, I abuse of them.
Why?

1) In OOP, they save me jumping to the interface to declare things.
2) It shortens bodies, so they can fit in a screen.
2) It allows me declaring objects near to where they are used and easily encapsulate variable names.
3) They save a little of stack.

The cons: Most tools are not very nested routines friendly. Debuggers have problems with them, code tools are not aware of nested routines. Many people that use other languages say "Why? what do you want it for?"

What are your feelings towards nested routines?

Saludos Santiago A.

Reply by email

Graeme Geldenhuys
to Santiago A.
Santiago A. wrote
1) In OOP, they save me jumping to the interface to declare things.

That's what I love about Java (compared to Object Pascal and C/C++) - no header required. Makes the code so much simpler, and I can drag and drop to reorder methods as needed.

Anyway, I think Lazarus IDE can automatically declare methods for you in the interface section - I think.

2) It shortens bodies, so they can fit in a screen. 2) It allows me declaring objects near to where they are used and easily encapsulate variable names. 3) They save a little of stack.

Overall, it's your preference. At least the good thing is, your are breaking up large bodies of code into smaller chuncks. That's a good thing.

Alternatively, I'll create a class that does all the work I need. That also makes it easier to unit test.

The cons: Most tools are not very nested routines friendly. Debuggers have problems with them, code tools are not aware of nested routines.

I know for a fact the Procedure List functionality (Ctrl+G) does support nested methods.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email