Re: Nested functions - better example?

new topic     » goto parent     » topic index » view thread      » older message » newer message
ChrisB said...

Call me thick if you want

I'm tempted blink

ChrisB said...

what is the advantage of a nested function, as opposed to an external function?

Consider this (although semi-based on something, it's actually complete fiction):

-- file pGUI.e 
function IupTreeView(...) 
    function createTVelem(...) 
    function iupTreeSetNodeAttributes(...) 
    function iupTreeAddNodesRec(...) 
    function deleteChildren(..) 
    function clickHandler(...) 

as compared to:

-- file pGUI.e 
function createTVelem(...) 
function iupTreeSetNodeAttributes(...) 
function iupTreeAddNodesRec(...) 
function deleteChildren(...) 
function clickHandler(...) 
function IupTreeView(...) 

Obviously, you're getting the same "breakdown into smaller more manageable units" value from both.
There's probably not much difference in understanding IupTreeView, but as a whole pGUI.e becomes far easier.
When modifying IupTreeView, in the second case you have to worry about/check whether anything else is using them,
whereas in the first case you can hack at them with impunity, only caring about a much smaller subset of pGUI.e
Some of them might obviously belong, but could you be so sure about deleteChildren() or clickHandler()?
There is nothing to stop you from having ten nested clickHandler()s, but you can only have one at top-level.

So, on the small scale and short include files the advantages are pretty much zero, but there's a gain when they grow.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu