Name of idiom where objects are nested and read like English?
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
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
This is called a fluent interface. It's mostly used for domain specific languages.
bdd
Prototypes? Chaining? Are these two even idioms?
Seems like an idiom.
Outside the javascript world, people almost always write such things as foo.shouldHaveLengthOf(3);
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.
That’d be a fluent interface.
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 thanfoo.should.not.have.lengthOf(3), and it's not written asfoo.shouldHaveLengthOf(3)orfoo.shouldNotHaveLengthOf(3).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.