Name of idiom where objects are nested and read like English?

help

For example, the chai API:

foo.should.have.lengthOf(3);

Does this have a name? I can't tell if it's neat or stupid.

Posted Nov 18, 2018, 10:46 PM UTC

68
systoll•

That’d be a fluent interface.

6
mort96•

Is it though? All of the examples there are using method chaining, where each method returns this. What OP is talking about is different; foo.should.have.lengthOf(3) would mean something different than foo.should.not.have.lengthOf(3), and it's not written as foo.shouldHaveLengthOf(3) or foo.shouldNotHaveLengthOf(3).

3
systoll•

The example would be

foo.should().not().have().lengthOf(3)

Except that chai defines all of these argument-less methods to be getters, which (aside from should) modify and return this.

That use of getters might qualify as an idiom in and of itself, but it’s in service of a fluent interface.

5
kbielefe•

This is called a fluent interface. It's mostly used for domain specific languages.

2
penguingspe•

bdd

1
Dencho•

Prototypes? Chaining? Are these two even idioms?

0
stack-compressionOP•

Seems like an idiom.

Outside the javascript world, people almost always write such things as foo.shouldHaveLengthOf(3);

2
AlxandrHeintz•

This is not entirely true. You have similar paradigmes in other programming languages as well. I've used a few of them in C# for instance.

1