A Brief Collection of Convenient Lies about Functional Programming 11

Posted by Michael Feathers Mon, 23 Mar 2009 15:19:00 GMT

  1. A value is the instantaneous state of an object. – In OO languages, we have objects. In FP languages, we throw out the object and instead manipulate the values it would take on over time.
  2. Algebraic data types are classes. – Every case in an ADT is a state that an “object” can be in.
    data Tree = Empty 
              | Leaf Int 
              | Node Tree Tree
    
    When we write functions over ADTs, we are obliged to cover all of the cases. So, for instance, if we define depth for Empty, we have to define depth for the Leaf and Node cases as well. When we do, we can evaluate depth t for any tree value and have a well-defined result.
  3. The functions which we define over an ADT can be considered its public interface. – There’s a school of thought which says that encapsulation doesn’t matter in an functional programming language because values are immutable and corruption can’t happen. Nothing could be further from the truth. If we add or remove a case from an ADT all of the functions which pattern match against it are impacted. While we don’t need to have an encapsulation boundary as tight as we might have in an OO language – it pays to be conscious of how far ADTs travel in a program. Encapsulation is the act of forming a boundary by transforming an ADT into some other form of data.

Each of these statements is a lie, an artful simplification, but they are a convenient and not entirely false way of thinking about functional programming until it becomes second-nature.

Comments

Leave a response

  1. Avatar
    Curtis Cooley about 1 hour later:

    Wow, I’m obviously a Novice at functional programming because almost none of this made sense. Where does a Novice go to get the information for this to make sense?

  2. Avatar
    Sadek Drobi about 1 hour later:

    You can add to that the lie that a pattern matcher is a more powerful switch statement :)

  3. Avatar
    niv about 3 hours later:

    Sorry, but where did you get this stuff? I’ve read tons of functional programming papers, texts, and I don’t think I’ve ever read something like “(...) encapsulation doesn’t matter in an functional programming language because values are immutable and corruption can’t happen.”

  4. Avatar
    Michael Feathers about 4 hours later:

    niv: It’s really just something I’ve heard in conversation. There’s this notion that we don’t really need to be concerned about the fact that the structures we match upon are not encapsulated. I don’t think I’ve run across a book which discusses the extent of an algebraic data type either except to point out that it can be restricted to a module. That seems a bit too narrow for much of the code I’ve seen.

  5. Avatar
    Patrick Logan 3 days later:

    For an algebraic type, the public interface is the algebra itself.

    Functions that use algebraic types are dependent on the public interface of the type.

  6. Avatar
    nike shoes hosting 8 months later:

    I don’t think I’ve run across a book which discusses the extent of an algebraic data type either except to point out that it can be restricted to a module. That seems a bit too narrow for much of the code I’ve seen.

  7. Avatar
    iphone fix 12 months later:

    yeah, you are absolutely right.

  8. Avatar
    disney restaurants about 1 year later:

    That seems a bit too narrow for much of the code I’ve seen and Never say more than is necessary.

  9. Avatar
    latin shoes about 1 year later:

    Most problems happens before testing, nice post anyway!

  10. Avatar
    latin shoes about 1 year later:

    would you mind updating your blog with more information?

  11. Avatar
    jewellery about 1 year later:

    But the discussion has its way of strengthening the definition overtime.

Comments