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
, andpath
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 itcreateRedirects
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