0
\$\begingroup\$

I'm working on a fluent interface builder that takes:

  • an array of strings, each of which becomes a key to an object that is a function that follows the fluent interface pattern
  • a terminator function which when called breaks the chain and does something else

I'm looking for some advice on how to improve this.

var Twitter = require('twit');

var secrets = require("./secrets/secrets");
var fs = require("fs");
var Twit = require('twit')

    var T = new Twit({
      consumer_key: secrets.consumer_key,
      consumer_secret: secrets.consumer_secret,
      access_token: secrets.access_token,
      access_token_secret: secrets.access_token_secret,
      timeout_ms: 60 * 1000,  // optional HTTP request timeout to apply to all requests.
    });
    function fluentBuilder(options, terminator) {
      let locals = options.reduce((opt, e) => {
        opt[e] = null;
        return opt;
      }, {});
      var methods = options.reduce((opt, e) => {
        opt[e] = (val) => {
          if (val) {
            locals[e] = val;
            return opt;
          } else return locals[e];

        }
        return opt;
      }, {});
      methods.locals = locals;
      methods["execute"] = terminator;
      return methods;
    }

    var geocode = fluentBuilder(["lat", "long"],
      function () {
        var self = this;
        return new Promise(resolve => {
          T.get("geo/reverse_geocode", self.locals, function (err, data, results) {
            resolve(data);
          });
        });
      });
      //calls to lat and long are stored on the self.locals variable which is passed to the Promise callback.
      geocode.lat("34.0406")
        .long("-84.2031")
        .execute()
        .then(data => {
          console.log(data);
        }); 
\$\endgroup\$
5
  • \$\begingroup\$ Can you post some example code using this fluent interface? \$\endgroup\$ Commented Aug 24, 2016 at 16:27
  • \$\begingroup\$ Also, can you edit your title to describe what this fluent interface is trying to accomplish (e.g. fluent interface for creating google maps)? \$\endgroup\$ Commented Aug 24, 2016 at 16:28
  • \$\begingroup\$ the call back here makes a post to the twitter geocode API, but the terminator function can be anything. The idea is to allow n number of parameters to be set (with n being the length of the array), capture these values in self.locals and when the user calls execute all of the parameters that have been set via the calls (lat, long) are passed into the callback. \$\endgroup\$
    – Mega Man
    Commented Aug 24, 2016 at 17:38
  • \$\begingroup\$ the geocode.lat("34").long("-84") creates a self.locals variable of {lat: 34, long: 84}. \$\endgroup\$
    – Mega Man
    Commented Aug 24, 2016 at 17:40
  • \$\begingroup\$ this is good information. Can you please add it to your code review? \$\endgroup\$ Commented Aug 24, 2016 at 18:08

1 Answer 1

2
\$\begingroup\$

This seems like a lot of complexity for limited/no value. Could you not just pass an object literal like {lat: 34, long: 84} to the geocoding function and be done with it, losing like 20 lines of code in the process?

A few other thoughts:

  • Why are you requiring the same dependency twice under different names?
  • Typically, javascript programmers like to use initial lowercase camel case. You have several variables named starting with capital letters which may not be desirable.
  • You have inconsistent use of let vs. var in fluentBuilder. Why have locals defined with let and methods defined with var when both variables are used within the same scope and there is no "inner" code block to which you are trying to limit visibility of locals. Either one works exactly the same in this case, but it would be good to be consistent.
  • Your indentation is inconsistent.
  • Why does fluentBuilder return methods?
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.