Neal Ford, www.thoughtworks.com
2007 May 8, 10:50
Tuesday
Slides at http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-1589.pdf
I had thought this session was about constructing domain specific programming languages and using supporting tools to generate Java code or even byte code (see bison).
Unfortunately, this is "Language Oriented Programming". All that is involved is building API so that programming reads like a sentence.
Ford's main point is that with traditional APIs, context is always provided, ie the client needs to know too much. With Domain Specific Language (DSL), context is implied. DSL allow higher abstraction and declarative programming. This is great. Declarative programming is the way to go, whenever possible.
Ford achieves language oriented programming by having all methods return this and allowing chaining, as in JQuery.
Eg,
Car API
Car car = new CarImpl();
MarketingDescription desc = new
MarketingDescriptionImpl();
desc.setType("Box");
desc.setSubType("Insulated");
desc.setAttribute("length", "50.5");
desc.setAttribute("ladder", "yes");
desc.setAttribute("lining type", "cork");
car.setDescription(desc);
Car Fluent Interface
Car car = new CarImpl().withMarketingDescriptionOf(
new MarketingDescriptionImpl("Box", "Insulated” ).
andAttributeOf("length", "50.5").
andIncludesA("ladder").
andAttributeOf("lining type", "cork"));
This is probably for small code, but one thing I've learned over the years, is don't try to code in a style that isn't explicitly supported by the language. I've seen C code that uses macros to simulate OO, and I've seen C++ code written in a Lisp style. In both of these instances, despite the coders being talented, the results were disastrous.
There is an example that using factories and immutable instances where this applies really well, but I can't see this style working for large programs.
No comments:
Post a Comment