1

Is there a name for the style of API that reads like a sentence? For example, in google-guice

bind(TransactionLog.class).to(DatabaseTransactionLog.class);

or in Easymock

expect(mock.voteForRemoval("Document")).andReturn((byte) 42);

I want to program an api that looks similar to what I call the 'google style' api, i.e. I want it to look like:

RowStyle(RED_BACKGROUND).when(PROP_ERROR_MESSAGE).notNull();

and would like to know pros/cons to this type of API, if it has a name, and how you would go about implementing it.

Ben Page
  • 3,011
  • 4
  • 35
  • 37

2 Answers2

2

Those APIs are called "fluent API" and in our days some guys call them "internal DSL", but the first term is AFAICT more widely used (and is the older correct term).

Angel O'Sphere
  • 2,642
  • 20
  • 18
1

For me, this will only work if the sequence of those operations (style(), when(), notNull() ) is strictly defined. If you can call when() after notNull(), this won't make any sence.

Normally, you just define a method with several parameters:

public void rowStyle(String condition, boolean notNull)

, but these additional calls are the good way to specify optional parameters.

So, + if you have optional parameters, - if you don't have them; + if strictly defined call sequence, - if not.

weekens
  • 8,064
  • 6
  • 45
  • 62