1

I really was questioning to myself where to ask this, as I could not decide myself. This is more of a software design question.

Context

I currently maintain a small Node.js module on NPM which basically acts as an API wrapper for an online service.

Issue

I have one function in the module which, when called with a string, makes one request to the service and receives the data, from a page, relevant to that one string input.

However, as a programmer and user of the service, I can understand that it might be useful to get the data for all possible input strings on that page. The way I have the module now, a programmer using my API wrapper would need to know every string, and call it every time, making a total of n requests, 1 for each string input, which is unacceptable if I'm trying to write a nice API wrapper for other developers (which I am).

* Possible Solutions/Question *

I thought to myself, I could write the function so that depending on the input string, if it's a special value (like null), I could retrieve all relevant data, but then I questioned whether this is good practice.

If I document the special input case nicely, is it good practice to have a specific parameter to a function which always produces a specific output? Is it better just to write another function that performs this procedure? I feel like having another function would violate the DRY principle because most of the code would be the same in the new function.

Thanks everyone! This really has bugged me for quite a while now.

  • 2
    Create a new function to handle the case. Take the code from the first function that is the same or very similar in both functions and make it into a third function. Use the newly created function in the other two that need this functionality. – toniedzwiedz Jan 11 '15 at 8:38
  • Talk about modularity, that is a great idea @toniedzwiedz! edit: Make that an answer so I can accept it! – CisBae Jan 11 '15 at 8:39
0

All of those are valid solutions, depending on what would be most intuitive to the users of your API.

If your users would expect getMagicData() to get all of the data by default, then do that, and let it accept an optional argument to specify a subset of the data.

But if that behavior would surprise and confuse your users, then make a separate getAllMagicData() method. It's not a DRY violation as long as both functions share the common code (either by making a third "private" method or by having getAllMagicData() simply call getMagicData() repeatedly).

Based solely on the description you've provided, I would probably find separate methods more intuitive.

  • Yeah I'm thinking that separate methods is the better idea too for clarity's sake as well, thanks for the answer and insight! – CisBae Jan 11 '15 at 11:40
  • Better than having getAllMagicData() call getMagicData(key) repeatedly is to have getMagicData(key) call getAllMagucData() and then filter the results (as long as this isn't substantially more inefficient than doing it directly), I suspect. – Jules Jan 11 '15 at 14:03

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.