Designing code: fluent interfaces and your domain
Posted on 03. Jan, 2011 by Guilherme Silveira in agile, design, ruby
Together with coworkers, we have been talking about the influence of Test First in our design: it is not only about testing, or testing all the time. The discussion is on how developers should learn from understanding feedback from every type of tests (unit, integration and so on) and act accordingly. We have begun a series of screencasts showing how most design practices can fit together.
In this first video, we show how to enhance the amazing Cells gem by providing a simple DSL on top of it, leaving only your domain language. In Rails code, that is translated to “no appearances of the words render, partial or alike”. Testing first and trying to bring some of the domain language into the code.
This is a 6 minute video going through adding a new feature into the gem, it should be straight forward even for non ruby developers.
This implementation makes use of method_missing, which envolves dispatching the invocation twice. It should be no user perceived performance bottleneck in a typical scenario – we are using an interpreted language with IO through the network in two endpoints (to the browser and to the database). For those willing to contribute, the next improvements would be to only respond to method invocations that are really a cell.
Another one is to dinamically add the methods instead of using method_missing.
Send in any suggestions for the next episodes: open source suggestions with any of the following characteristics:
- code to be refactored
- great apis that can be improved with domain specifics
- legacy code without tests, waiting to be tested
- libraries with integration tests, with no unit ones
- libraries with unit tests, without integration ones
SUBSCRIBE TO OUR RSS
Ricardo Herrmann
03. Jan, 2011
“write someone a program and you solve their problems for a day; write them a DSL and they can solve their own damn problems” http://twitter.com/#!/donsbot/status/21126064650588160