couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Davis <>
Subject Re: doctype vs. duck typing
Date Thu, 12 Nov 2009 21:49:53 GMT
On Thu, Nov 12, 2009 at 4:40 PM, Robert Campbell <> wrote:
> I'm reading CouchDB in Action and they make use of a doctype in their
> examples. This doctype attribute is used in your Map functions to
> easily filter out the specific types you want. Example:
> if (doc.doctype == "User") // we have a user...
> CouchDB: A Definitive Guide, on the other hand, makes use of duck
> typing. Essentially you determine that a document is a duck if it
> quacks and flies:
> if (doc.quacks && doc.flies) // Looks like a duck..
> What are the pros and cons of each approach? Which one do you guys use and why?


Doctypes give you less code at the expense of not making specific
checks on a given document. Ie, the often cited example of a business
card, if(doc.type == "business_card") means you still have to check
if(doc.fax) each time. On the other hand, ducktyping is more of an
assertion that the document has the expected form, and nothing else.
The down fall of duck typing is that it can lead to lots of weird
conditionals if your documents get fairly complicated.

There's also the difference between distribution style. If your code
is operating on data from multiple producers, the doctype could be a
horrible lie. As in, you assume doctype means the business card has a
name and email when it has neither.

Personally, I've been meaning to go back and find the link that Jan
posted in a tweet awhile back to a schema checker that was basically a
set of executable tests that were extensible, then checks can be
arbitrarily complex while maintaining some sanity in map guards.

Paul Davis

View raw message