News for hackers. Stuff that matters.
Re:Simple Ways of Reducing the Cognitive Load in Code by Sindisil on Tuesday June 28, 2016 @09:36AM (#11993642)
Situations like this are exactly where nested functions can be helpful.

I've always thought that it was a shame that C didn't have them.

Sometimes I almost wish that Algol flavored languages like Pascal anf Modula 2 would have won out for systems programming, instead of C and the languages it inspired.

Actually, GNU C supports nested functions, and a new round if standardization is just starting up, so maybe there's a chance?

Re:Simple Ways of Reducing the Cognitive Load in Code by jerf on Tuesday June 28, 2016 @10:33AM (#11994075)
In languages where braces define a scope even if there's no if, for, etc. keyword around, you can get a lightweight version of that just by sticking braces around your "paragraphs", as needed. They aren't the same as nested functions, in particular because you can't invoke a naked block multiple times, but if you've got a long function that hasn't got any useful break points in it, but you just want to chunk things, curly braces may help you isolate things nicely.

I tend to consider this a "last resort" over using actual functions, but there are certain classes of functions where this is helpful. The two biggest I know of are functions that represent a state machine, and functions that are the big top-level "plumb all the libraries together" where breaking that up into separate functions significantly complicates code due to all the intricate routing of values you have to do.

Re:Simple Ways of Reducing the Cognitive Load in Code by elcritch on Tuesday June 28, 2016 @11:02AM (#11994298)
> They aren't the same as nested functions, in particular because you can't invoke a naked block multiple times,

Sure you can, just use `goto`! It's fantastic for code reuse. ;p

On a more serious note, gcc and clang both support block functions now. Pretty sure they aren't full closures but they can be handy in these situations.

Re:Simple Ways of Reducing the Cognitive Load in Code by Sindisil on Tuesday June 28, 2016 @12:35PM (#11995167)
The thing is, naked scope blocks are missing the main reason I'd use a nested function: the ability to be named.

I do occasionally use scope blocks when I want to constrain the scope of one or more local variables, and there isn't an otherwise appropriate scope already created by a flow control construct.

Re:Simple Ways of Reducing the Cognitive Load in Code by majewsky on Wednesday June 29, 2016 @04:34AM (#12000276)
I also use naked scope blocks sometimes. To me, their main purpose is to make temporary variables go out of scope, to clarify which variables are still intentionally live beyond the block.
Re:Simple Ways of Reducing the Cognitive Load in Code by majewsky on Wednesday June 29, 2016 @04:32AM (#12000267)
> Situations like this are exactly where nested functions can be helpful.

Indeed. When I'm writing Haskell, I'm using a lot of nested functions in `where` blocks, sometimes cascaded down.