Re:Functional Programming Doesn’t Work (and what to do about...
by ams6110 on Tuesday December 29, 2009 @02:24AM
(#1020176)
My initial reaction was also "why would you". Take the "two most isolated functions in the entire codebase" and make them dependent on each other? OK, assuming there is a good reason (not given in the example) to do this, I'd say you achieve this by passing whatever bit of state data you need to drive the behavior in the various calls to functions A and B. No need for global variables to achieve this, nor is it really any easier or cleaner to use them.
[ Parent
| on HN ]
Re:Functional Programming Doesn’t Work (and what to do about...
by foldr on Tuesday December 29, 2009 @05:18AM
(#1020319)
>I'd say you achieve this by passing whatever bit of state data you need to drive the behavior in the various calls to functions A and B.
Obviously you can do that. The author mentions this possibility. But his point is that passing the state around may involve adding extra parameters to a very large number of functions, which is much more difficult than just using a global variable.
It seems odd to me to say that faking global variables by clumsy manual state threading is cleaner than just using global variables. It is more error-prone, more verbose, and less indicative of program structure and programmer intent.
Re:Functional Programming Doesn’t Work (and what to do about...
by fhars on Tuesday December 29, 2009 @05:20AM
(#1020320)
Using global variables is far easier in this case and may entail changing a whopping three lines of code: one for the declaration of the variable and one each in the two functions using the new variable. The argument passing variant on the other hand requires changing almost every single function call in the whole program. In this sense, imperative programming is strictly more expressive than purely functional programming (in the sense of the word that IIRCgoes back to Felleisen: a paradigm is more expressive if expressing a construct that is localized in this paradigm requires a global program transform in another paradigm).
Of course this doesn't mean that a program will be easier to read after going the easier to write way too many times...
Re:Functional Programming Doesn’t Work (and what to do about...
by ams6110 on Tuesday December 29, 2009 @03:33PM
(#1021243)
Well could you not write new A and B functions that invoke the original functions with state parameters added while also providing the state-handling mechanisms. Then all other existing calls remain unchanged. This really just introduces a namespace for what would otherwise be a "global" but it keeps the real "global" namespace unpolluted.