On 17 October 2014 01:08, Mark Engelberg <mark.engelb...@gmail.com> wrote:
>
> But let's say later you decide you want your data model to be {:first-name
> "Alice", :last-name "Beasley", :email "al...@example.com"}, and you want
> to change name to be a computed value that concatenates first and last
> names -- this is going to break all your existing code.  If name were only
> accessed throughout your code via the call `get-name`, then it would be
> trivial to make this change.
>

Sure, that's the theory behind encapsulation, but I'm not convinced there
are many cases in practice where the API can remain consistent while the
data changes.

Let's take names as an example. Let's assume you start with a data
structure:

    {:first-name "Alice", :last-name "Beasley"}

And we construct an API around that:

    (defn get-first-name [p] (:first-name p))
    (defn get-last-name [p] (:last-name p))

What happens when we come across a name that doesn't fall this scheme, like
"Isa bin Osman" or "Madurai Mani Iyer"? In those cases our getter functions
become as obsolete as the fields they map to.

You might argue that a smart person would write more abstract accessors:

    (defn get-full-name [p]
      (str (:first-name p) " " (:last-name p)))

    (defn get-brief-name [p]
      (:first-name p))

But if you were smart enough to do that, then you'd clearly be smart enough
to write a better data structure in the first place:

    {:full-name "Alice Beasley", :brief-name "Ms Beasley"}

Even if you accept the idea that APIs can protect against changing data
structures, you have to be aware what you're giving up. Encapsulation comes
at a significant cost: you need to write a custom API for every data
structure in your application.

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to