Live data from Hacker News

Destroy All Ifs – A Perspective from Functional Programming

degoes.net

141–150 of 226 posts

Re: Destroy All Ifs – A Perspective from Functional Programming

#141

Earlier quoted context omitted.

Someone else addressed the details of your counter argument, but I'd like to respond to it generally. It seems like every time someone writes an article on how to write better code, there are responses about how it doesn't make sense when taken to some logical extreme, or some special case, as if that invalidates the argument. (FP techniques in particular seem to provoke this.) But code design is like other design di…

The thing is that the tone of the article seems to suggest taking such an extreme: I mean, an "anti-if " campaign? There's like, only one sentence of concession near the end towards those unconvinced by the argument.

> There's like, only one sentence of concession near the end towards those unconvinced by the argument.

But that sentence is "I’m just joking about the Anti-IF campaign". Why would extra words help?

Re: Destroy All Ifs – A Perspective from Functional Programming

#142

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 un…

All 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 poin…

What distinction are you drawing between "first-class functions" and "function pointers"?

Re: Destroy All Ifs – A Perspective from Functional Programming

#143
post #43

Often times when I read about "ideal" ways of programming, I'm curious if it's ever implemented in a production code base built by a team.

Me too. Particularly because every programmer has their own idea of what a "right"/ideal style of programming is. Here, apparently, we must not use conditionals. The more I write code the more I realize that the entire purpose of the code is to have some effect on reality, and the more reliably it can do this, the better the code. I find I code a lot better without design principles, because trying to remember which…

> The more I write code the more I realize that the entire purpose of the code is to have some effect on reality, [...]

Perhaps. But there's the effect you get from running the code on a computer. And the effect reading the code has on humans.

Re: Destroy All Ifs – A Perspective from Functional Programming

#144
post #68
post #43

Earlier quoted context omitted.

Me too. Particularly because every programmer has their own idea of what a "right"/ideal style of programming is. Here, apparently, we must not use conditionals. The more I write code the more I realize that the entire purpose of the code is to have some effect on reality, and the more reliably it can do this, the better the code. I find I code a lot better without design principles, because trying to remember which…

Not functional code though. The aim of functional code is to be side-effect free, and affecting reality really gets in the way of that. /snark, but articles like this really do fall into that trap...

Didn't they have some gratituous IO-monad stuff in the article?

Re: Destroy All Ifs – A Perspective from Functional Programming

#145
post #127

Earlier quoted context omitted.

In 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 Adden…

That coding guideline simply rejects all recursion, even cases that are correct by inspection, or by easy proof.

Interesting. I assume they allow the special cases of tail recursion introduced by 'while', 'for' and similar constructs?

Re: Destroy All Ifs – A Perspective from Functional Programming

#146
The idea that functional programming is a type of inversion of control reminds me of similar idea I had, when comparing OOP and FP.

In OOP, you encapsulate data into objects and then pass those around. The data themselves are invisible, they only have interface of methods that you can apply on them. So methods receive data as package on which they can call methods.

In FP, in contrast, the data are naked. But instead of sending them out to functions and getting them back, the reference frame is sort of changed; now the data stays at the function but what is passed around is the type of processing (another functions) you want to do with them.

For example, when doing sort; in OOP, we encapsulate the sortable things into objects that have compare interface, and let the sort method act on those objects. So at the time sort method is called, the data are prepared to be compared. In FP, the sort function takes both comparison function as an argument, together with the data of proper type; thus you can also look at it as that the generic sort function gets passed back into the caller. In other words, in FP, the data types are the interfaces.

So it is somewhat dual, like a different reference frame in physics.

The FP approach reminds me of Unix pipes, which are very composable. It stands on the principle that the data are the interface surface (inputs and outputs from small programs are well defined, or rather easy to understand), and these naked data are operated on by different functions (Unix commands). (Also the duality is kind of similar to MapReduce idea, to pass around functions on data in the distributed system rather than data itself, which probably explains why MapReduce is so amenable to FP rather than OOP.)

It also seems to me that utilizing this "inversion of control" one could convert any OOP pattern into FP pattern - just instead of passing objects, pass the function (method which takes the object as an argument) in the opposite direction.

I am not 100% convinced that FP approach is superior to OOP, but there are two reasons why it could be:

1. The "nakedness" of the data in FP approach makes composition much easier. In OOP, data are deliberately hidden from plain sight, which destroys some opportunities.

2. In OOP, what often happens is that you have methods that do nothing rather than pass the data around (encapsulate them differently). In FP approach, this would become very easy to spot, because the function that is passed in the other direction would be identity. So in FP, it's trivial to cut through those layers.

Re: Destroy All Ifs – A Perspective from Functional Programming

#147
post #143
post #43

Earlier quoted context omitted.

Me too. Particularly because every programmer has their own idea of what a "right"/ideal style of programming is. Here, apparently, we must not use conditionals. The more I write code the more I realize that the entire purpose of the code is to have some effect on reality, and the more reliably it can do this, the better the code. I find I code a lot better without design principles, because trying to remember which…

> The more I write code the more I realize that the entire purpose of the code is to have some effect on reality, [...] Perhaps. But there's the effect you get from running the code on a computer. And the effect reading the code has on humans.

I'm not sure it's possible to write a piece of software that only you understand, that is of high quality (works reliably). I feel like code correctness (what a computer does with it) and readability (how well a human understands what is written) are two sides of the same coin.

I think I would argue that if someone has happened to write a program that is impossible to understand for human readers (unreadable), and yet it does exactly what it's supposed to do (is correct), at the very least this program will break when the author starts to refactor it, which always needs to be done at some point.

I agree that conveying an idea to another human being through code is useful, but I think the number of times a human being has written a piece of computer code only to convey an idea to someone else is fairly small. If I want to convey an idea to another person I write in words and concepts, but if I want something faithfully executed every time, I need to write the code. And perhaps someone will look at this code later, but the origin of the code was always to get something done, not conveying an idea to someone else.

Re: Destroy All Ifs – A Perspective from Functional Programming

#149
post #82

I'm surprised that the article and none of the comments so far mentioned the "Expression Problem": http://c2.com/cgi/wiki?ExpressionProblem Basically, if you structure the control flow in object oriented style (or church encoding...) then its easy to extend your program with new "classes" but if you want to add a new methods then you must go back and rewrite all your classes. On the other hand, if you use if-statemen…

    > I think its because until recently pattern matching and
    > algebraic data types (a more robust alternative to 
    > switch statements) [...]
Could you elaborate a bit on what this accomplishes, eg. pattern matching vs a "case" statement? As I've programmed in Haskell for the past year or two, I've observed exactly this change in my style of writing - that I've started to get rid of "case" statements inside function definitions, and have moved them into the pattern-matching part instead ("outside" the function definition).

But I have to admit, I'm not entirely sure why I do this. It just feels more robust to me in some way.

Re: Destroy All Ifs – A Perspective from Functional Programming

#150
post #54

tl;dr: prefer callback hell instead of straight forward ifs and somehow that's progress.

Yeah, and the true fun starts when you try to debug it. Debugging streams in java is nightmare compared to debugging the same logic written in a simple foreach loop with a bunch of IFs.
Post reply on HN