0
\$\begingroup\$

I have a function below that does the following uses a bunch of smaller CRUD operations which I call create, retrieve (instead of read), update, delete. Because this function does a bit of everything it can be hard to name it.

Here's a bit about its behavior:

  • pass in array of redirects (objects with url, and path properties)
  • retrieves all existing redirects
  • checks to see if requesting redirect path exists
  • if existing redirect with path exists and url is the same returns existing
  • if existing redirect with path exists updates redirect
  • if existing redirect with path does not exist creates new redirect

Here are some possible candidates:

  • retrieveAllRedirectsUpdateExistingRedirectsCreateNewRedirectsDoNothingForRedirectsWithNoChange
  • retrieveAllUpdateExistingCreateNewRedirects
  • retrieveUpdateCreateRedirects
  • ensureRedirectsExist
  • ensureRedirects
  • overwriteRedirects // implies will delete redirects not passed to it
  • createRedirects
  • redirects

Here's the function:

Shopify.prototype.ensureRedirects = function(redirects){
  return this.retrieveAllRedirects().then(function(existingRedirects){
    return Promise.map(redirects, function(redirect){
      return Promise.resolve(existingRedirects).then(_).call("findWhere", {
        "path": redirect.path,
      }).then(function(match){
        if(match && match.url == redirect.url) return match
        if(match) return this.updateRedirect(match.id, redirect)
        return this.createRedirect(redirect)
      }.bind(this))
    }.bind(this))
  }.bind(this))
}

What naming convention lends itself to the most flexibility and follows the best pattern?

Other functions that I have in this library include but aren't limited to:

  • retrieveRedirects (paginated)
  • retrieveRedirectsCount
  • retrieveAllRedirects (all pages)
  • createRedirect
  • updateRedirect

This also begs the question should this function above be createRedirects as an alias of createRedirect that detects argument type object v.s. array and creates the redirects accordingly, making the createRedirect function more versatile to handle argument types? Then what do you name the function and argument? Convention for naming a function or variable as both plural and singular

\$\endgroup\$
  • \$\begingroup\$ I believe this is what they call "over thinking it" \$\endgroup\$ – FrigidDev Feb 12 '15 at 18:05
  • \$\begingroup\$ Could you add the language tag to your question? :) \$\endgroup\$ – IEatBagels Jun 29 '15 at 19:50
  • \$\begingroup\$ Done it friend :) \$\endgroup\$ – Dan Pantry Jun 29 '15 at 19:52
1
\$\begingroup\$

Abstractions

When working with collections, there are three layers of functions - each essentially justifying a module:

  h(url[], url[]) -> url[]
  g(url, url[])   -> url[]
  f(url1, url2)   -> url

The dependencies are h <- g <- f. The functions at f are more reusable than those at g are more reusable than those at the highest level h.

Complected.

I'll reflect my bias: If a function is so complex it is hard to name, it might need to be refactored.

The place to start is with a function [let's call it g] that takes one item from redirects and modifies existingRedirects atomically. Then the existing function ensureRedirects can focus on operations at the level of the collection.

Conceptual mangling of the three levels - processing a collection of possible changes versus processing one possible change versus comparing two items - is clear from the description of the ensureRedirects's behavior in the question. A lot of it is at the level of processing a single URL against a collection of URL's even though ensureRedirects operates on collections. Some of it is about dealing with two URL's.

The naming should flow from smaller pieces to larger pieces. Or h could just be redirects.foreach(g)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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