Sunday, November 4, 2007

RubyConf 2007: Cleanliness Is Next to Domain Specificity

Ben Scofield is a senior developer at Viget Labs (blog)

There really has been no discussion of why you would want to create a DSL.

Part 1: Linguistics

Categories
  • Language (for the sake of discussion, it is Vocabulary and Grammar)
  • Dialects
  • Jargon
  • Pidgins and Creoles
Regional Dialects Example: Generic Names for Soft Drinks
Regional Dialects Example: Little Bunny Foo Foo
Jargons Example: Waitress-speak
Cants Example: Class languages in D&D
Pidgins and Creoles: linguistic hacks and evolved linguistic hacks, respectively

Most "DSLs" are not different languages, because they only have a different vocabulary, and not a different grammar.
  • ActiveRecord
  • RSpec
Who cares? DSLs intimidate and frighten.
Write a Parser? No, thanks (Matz did it already).
API vs. Dialect

Sapir–Whorf hypothesis (and programming languages)
  • Linguistic Determinism (the strong version; it's crap)
  • Linguistic Relativism (the weaker version; influence instead of determine)
The Great Eskimo Vocabulary Hoax (Wikipedia)

Color Perception
  • Direction of Causality (language and thinking)
  • Degree of Influence (were you able to distinguish the colors before being given the words)
RSpec loves Sapir-Whorf. Specifications come first. RSpec leads you in the right direction. Domain Specific Dialects (DSDs) are built on Linguistic Relativism. Keep your head in the domain (foo/bar/baz vs. roll/pitch/yaw).

Part 2: Refactoring

Proviso: when dealing with domain-specificity, tastes vary.

Finding a ticket. Some sites became travel search site aggregators (kayak.com, sidestep.com). Kayak provides a search API and a Ruby sample. Someone from the audience: It tastes like burning. It looks like PHP. Strategy when writing a DSD: start at the end.

I want to find flights from CLT to RDU leaving today and returning in one week.

find :flights, :from => :CLT, to => :RDU, leaving => Date.today, returning => Date.today + 7

What is sent from their API to their web service? Set up expectations (mocks). When writing domain-specific code, you will never be done (modeling reality problem). There is always room for improvement.

Tips
  • use symbols (reads like English; quotes would distract you)
  • optional parenthesis (looks like a sentence)
  • blocks for all
  • splat operator (* arrays from anything)
  • optional braces (not that common; Rubinius guys don't like this)
Keys
  • Start modestly
  • Stay in the domain
  • Get better
http://www.extendviget.com/
http://www.culann.com/

We throw around the word language too easily and don't use it precisely, as Ben does (with his linguistic background).

Domain-Driven Design

No comments: