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
-
\$\begingroup\$ I believe this is what they call "over thinking it" \$\endgroup\$FrigidDev– FrigidDev2015年02月12日 18:05:59 +00:00Commented Feb 12, 2015 at 18:05
-
\$\begingroup\$ Could you add the language tag to your question? :) \$\endgroup\$IEatBagels– IEatBagels2015年06月29日 19:50:57 +00:00Commented Jun 29, 2015 at 19:50
-
\$\begingroup\$ Done it friend :) \$\endgroup\$Dan– Dan2015年06月29日 19:52:13 +00:00Commented Jun 29, 2015 at 19:52
1 Answer 1
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)