3 ms·
Problem is, a decision has to be made somewhere about which function to pass into that "if-free" block of code. The if-like decision has just moved elsewhere. T
by kazinator 10y ago
Problem is, a decision has to be made somewhere about which function to pass into that "if-free" block of code. The if-like decision has just moved elsewhere. That is a win if it reduces duplication: if a lambda can be decided upon and then used in several places, that's better than making the same Boolean decision in those several places.
Programs that are full of function indirection aren't necessarily easier to understand than ones which are full of boolean conditions and if.
The call graph is harder to trace. What does this call? Oh, it calls something passed in as an argument. Now you have to know what calls here if you want to know what is called from here.
A few days ago, there was this HN submission: https://news.ycombinator.com/item?id=12092107 https://news.ycombinator.com/item?id=12092107
"The Power of Ten – Rules for Developing Safety Critical Code"
One of the rules is: no function pointers. Rationale: Function pointers, similarly, can seriously restrict the types of
checks that can be performed by static analyzers and should only be used if there is a
strong justification for their use, and ideally alternate means are provided to assist
tool-based checkers determine flow of control and function call hierarchies. For
instance, if function pointers are used, it can become impossible for a tool to prove
absence of recursion, so alternate guarantees would have to be provided to make up
for this loss in analytical capabilities.
- bunderbunder 10y agoAll valid points. If you're going to do this sort of thing with much success, you really need to have a language with a fairly powerful type system. If function pointers are your only option for higher-order programming, I wouldn't even try. First class functions or interface polymorphism help, but I'd also want to have a language that makes it relatively easy to create (and enforce) types so that your extension points don't end up being overly generic.
- thaumasiotes 10y agoWhat distinction are you drawing between "first-class functions" and "function pointers"?
- bunderbunder 10y agoAbout the same distinction as I'd draw between an integer and a pointer to an integer.
- kazinator 10y agoBoth function pointers and "first class functions" refer to function indirection. In its treatment of expressions, C doesn't draw the distinction between function and pointer to function. When you call printf("foo\n"), the printf part is a primary expression which designates a function, and evaluates to a function pointer. That pointer is then dereferenced by the postfix (). The main difference between a "first class" function and a function pointer is that a first class function carries an environment, which allows the body of the function to make references to lexically scoped names outside of the function. A function pointer carries a reference to the code only.
- moogleii 10y ago>The main difference between a "first class" function and a function pointer is that a first class function carries an environment Isn't that specifically a closure? I think first class functions have a more general definition.
- eru 10y agoIn a language like Haskell you wouldn't want to prove the absence of recursion, but that all recursions in the program fit into a handful of patterns. (Eg 'structural-recursion' or 'tail-recursion'.) Some type systems are strong enough to put that kind of analysis / constraints directly into the language. (Haskell might already be strong enough with GADTs and other language extensions enabled.) In any case, the Addendum at the end of the blog post provide a different perspective on the problem you mentioned.
- kazinator 10y agoThat coding guideline simply rejects all recursion, even cases that are correct by inspection, or by easy proof.
- cousin_it 10y agoTee hee, Haskell doesn't have tail recursion (e.g. foldl takes linear space), and structural recursion in Haskell isn't guaranteed to terminate (e.g. if you're given an infinite list). If I were in charge of developing a safety critical system, and someone came to me with a proposal to write it in Haskell, I'd be very skeptical.
- 10y ago