Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript fluent api vs options object

A javascripter I respect talks about how you should avoid large options objects in favor of a fluent api.

Why? What are the pros and cons?

Avoid large options objects

If you component truly only takes a few options, and are unlikely to change after the fact, then an options object may be suitable. I strongly suggest considering a fluent API, even if you provide an options object. This makes code considerably cleaner, as the fluent API can back each key in the options object, which otherwise would promote extremely large plugins. Remember build up to a user-friendly api, do not start there.

https://github.com/component/component/wiki/Building-better-components

like image 222
Harry Avatar asked Nov 12 '22 19:11

Harry


1 Answers

I feel like this is an "opinion question" with out a definite answer so here's my opinion.

It makes development easier. Modern Javascript IDEs can inspect Javascript sources and give code completion hints. Visual Studio, Aptana and WebStorm to name a few. If you have a fluent API, the IDE can list all the options methods which will allow you to enter code faster and it will also be able to show you the documentation for each option. If you just pass an options object, you have to know what the shape of that object should be and you also have to know what options you want to use and what they do since the IDE wouldn't be able to tell you that.

Matthew

like image 57
Matthew Avatar answered Nov 15 '22 10:11

Matthew