Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 95036 invoked from network); 12 Nov 2009 21:50:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 12 Nov 2009 21:50:37 -0000 Received: (qmail 77539 invoked by uid 500); 12 Nov 2009 21:50:36 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 77452 invoked by uid 500); 12 Nov 2009 21:50:36 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 77442 invoked by uid 99); 12 Nov 2009 21:50:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Nov 2009 21:50:36 +0000 X-ASF-Spam-Status: No, hits=-2.6 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of paul.joseph.davis@gmail.com designates 209.85.211.186 as permitted sender) Received: from [209.85.211.186] (HELO mail-yw0-f186.google.com) (209.85.211.186) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Nov 2009 21:50:33 +0000 Received: by ywh16 with SMTP id 16so2552899ywh.13 for ; Thu, 12 Nov 2009 13:50:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type; bh=ZVRyFUCkX8SoxZOlyGhzDFn8MPI1V2cKTwKz96NlNL4=; b=cL+XUxFaAYPoqlOJVQYU4sWzzmfTZNWAa1nIuj+CVhd4hRDMNxAzO5bSHrfVaCasYE tN6XMycSajdW0HxjMG6ikV1134aJerVaGcA8rDLASyM/oAhPoD3oLdV3Xv8uJP7UBSG7 y8HPLZMTG1os3vKcZ5doXB5qIUGWynhDZ+QRA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; b=gdSahZKLci+GBK1RthXOr+7+s9b1iZd4wj1xtnm5Bo0oCWHJQAYo80Ebk7bFjYlQv+ PWs4s+3zDtyWHc7tybii+cQ6s/J5xzOFZKn/0DliqwB6Ip2X4aSj9VQeeZMsDVioRbro H+W43fNd99wpnnvjH43fVEacDew1Oqs5R5ktg= MIME-Version: 1.0 Received: by 10.101.208.35 with SMTP id k35mr3742495anq.132.1258062613106; Thu, 12 Nov 2009 13:50:13 -0800 (PST) In-Reply-To: References: From: Paul Davis Date: Thu, 12 Nov 2009 16:49:53 -0500 Message-ID: Subject: Re: doctype vs. duck typing To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 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? > Robert, 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. HTH, Paul Davis