-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function name streamlining #2
Comments
Thanks @BPJandree for all the suggestions for the package. On naming of functions, I’ve posted a suggestion in #3. A few observations for discussion:
|
See 7e21297 for an example how a generic function could look like. It works for all types except for scripts. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
intro
In the package, I see that currently
catalog_entries.R
. This seems to be mostly on the "request" side of things. Maybe some simplifications can be made, but I have not yet carefully reviewed this.datasets.R
. I don't immediatly see a common theme across the functions in here but thecreate
functions seems to be important and most of my comments below have to do with this "push" side of the package.external_resources.R
, I have not yet carefully looked at this.create
function, most of my comments are on this. All of these functions followtype_add
syntax.On naming convention, classes and dispatching methods
Suggestion 1: naming of functions that share a common goal
Looking at the code, the current naming convention of "add" methods (that wrap around create) follows:
My suggestion is to adopt the following:
This allows creating a generic
add
method that dispatches different functions depending on the "class type" of the supplied input (the metadata scheme). I will explain this simplification in more detail below.Suggestion 2: Harmonized use of a single main function - wrapper versus dispatch
Not suggested: wrapper with if-else approach
In the current package we have different
type_add
functions. We could write one mainadd
function with atype=
field (this is essentially taking current situation, and adding one wrapper function with a lot of if-else statements):The main
add_ifelse
function takes inputsx
andtype
, checks thetype
field using an if statement, and passes onx
to the appropriatetype_add(x)
function. For the user, this is slightly easier than having to figure out him/herself whichtype_add
function to use out of a possibly long list of variations. In particular, in the current sitaution, each time a new metadata scheme gets added, a new type_add function need to be added to the package and the user manual grows and the user needs to write new code that calls these different functions. With a single wrapper, everything stays the same for the user who only makes use of the mainadd_ifelse
function written above.The problem with this "if-else" approach is that the complexity of the
add_ifelse
grows over time with more and more statements needing to be added. It would be nice if R could take care of this "type-checking" automatically. With the current approach, it is also easy to make mistakes like the following:Which would apply the
b_add
function to a metadata scheme of type "a". This is likely a situation that should not be possible at all and lead to API errors that can be difficult to debugg.Suggested: dispatch methods using classes
Using the function naming convention
add.type
I proposed, the code above could be simplified and made idiot-proof as follows:of course the
oldClass() <-
parts can be automated inside some function so that the user does not need to this. For example, if certain metadata schemes have unique fields that distinguish them from others, this could also be inferred in a "smart" way. For example, if reports are always documented by metadata scheme "document", and reports always carry a field "report number" then this is easy to detect. I am not familiar enough with the schemes yet to make smart suggestions. Maybe each scheme simply has a field that says which type of metadata scheme it is?Suggestion 3: Simplified documentation with dispatchable methods
When using the suggested dispatch appraoch, the package description can also be drastically simplified. In particular, since the user only makes use of the
add
function we only need to document the "add" function in the manual. Effectively, instead of documenting each individualtype_add
e function in the package we can simply choose to "hide" them as follows. In the current package, eachtype_add
function is followed bywhich exports the function to the namespace fo the package. Since the
add.type
functions will automatically be dispatched for objects of classes "type", the user does not need to make explicit use of any of theadd.type
functions. Hence, we can remove the#' @export
line after each add.type function so that the individual methods do not show up in the package documentation. This allows us to add, remove, merge, edit etc. any of theadd.type
functions int he future without the package manual needing to be changed (drastically).backwards compatibility
For full backwards compatibility, one could add the following code to the package:
and
#' @export
these individual functions. In this case we can just simply write something along the lines "deprecated, available for backward compatibility. see add function for suggested use" as description for these functions.Suggestion 4: removing redundant code using dispatch
Taking a closer look at the
type_add
functions, there is a common pattern. Essentially, all of these functions perform the following four tasks of which some are optional.create
functionFirst, item 1 can be turned into a
check_api
function rather than a scripted check. This avoids the possibility we make a typo mistake in one of thetype_add
functions. Also, if at some point we want to make changes to the api check we just edit the single function rather than going back to all the individualtype_add
functions and editing all the different pieces individually.Item 2, using the distpatch technique explained above, we can create one single
check_scheme
function, wich dispatchescheck_scheme.type
depending on the type of metadata scheme at hand. In the current case, this would perform the actions of lines 47-53 inscripts.R
to a metadata scheme of type "scripts", or do nothing at all for metadata schems of type "image". So for image, the function would just becheck_scheme.image(x){return{x})
which maybe we want to change later.Item 3, all functions do this part (same goal) but this is also where each function differs in how they do it (
type
field). Using the dispatch methods above, we could actually have onecreate
function with differentcreate.type
methods, which would be the safer version of the currenttype=
field approach.Item 4, like item 2 this could be a single
check_response
with differentcheck_response.type
.Overall, we could then replace all the different
type_add
functions, with the following single function:In which case the package development focuses on building out the
check_scheme.type
library and thecreate.type
library.The text was updated successfully, but these errors were encountered: