When to use - several methods, several parameters or parameter parameters

This question comes from a javascript point of view, but it certainly can be applied to other languages.

I have come across this more and more recently and wondered if there was a best practice, or at least a good design, for how to create your methods.

The obvious options I see are the following, along with a trivial example for each

  • Several methods:

    this.makeGetRequest = function(controller){...} this.makeSynchronousGetRequest = function(controller){...} this.makePostRequest = function(controller, data){...} 
  • One method with a lot of parameters:

     //data would be an optional parameter // this.makeRequest("friends", "GET", true); // this.makeRequest("friends", "POST", false, newFriend); this.makeRequest = function(controller, type, isSynchronous, data){...} 
  • One method with options option:

     this.makeRequest = function(controller, type, options); this.makeRequest("friends", "POST", {data:newFriend, isSync:false}); 

The example of HTTP requests just motivates the question, but it works for any public function with a variable number of settings / variables.

All three are obviously just as functional. But what good practice? Is there a standard or guideline that is generally respected?

+6
source share
3 answers

The advantage of the options parameter is that it allows unlimited possibilities without sacrificing clarity. When someone points out data: or isSync: it clearly shows which settings are populated. Remember that setting the 13th parameter in the false function is a recipe for confusion.

So, between the last two parameters, I use only a few parameters in cases where (1) there were only four parameters, (2) the sequence is logical and easy to remember (URL, then enter and then data), and (3) no more than one (last) is optional. In any case, this is the rule.

What about several methods compared to parameterization? Several methods - this is only an option when you have a small number of features (GET vs. POST, UseDefaults vs. DontUseDefaults or something else). Thinking about this, I will probably set up individual methods only when:

  • The difference is very important (a synchronous request behaves fundamentally different from an asynchronous request, and I want the developer to consciously choose what to apply. (Bearing in mind that GET versus POST is another property of your request - this is not the final solution if you do it wrong.)

  • There are no other parameters that you want to set in any case. If I need to remember first whether to call foo.get () or foo.post () and then remember to populate the options object anyway , I'm just forcing you to make decisions in two different places. I would prefer all the settings to be in one place.

+2
source

Obviously, with a few methods, you will need to have some kind of abstract super-method that they would call, otherwise you would repeat yourself, which is considered bad, but sometimes it’s good to have methods like aliases or abbreviations (see, for example, jQuery $.getJSON() for modified $.ajax() -Call)

One method with a large number of parameters is not very flexible if you have many special or optional parameters, for which you would use the options object, as you mentioned in your third example.

It really comes down to the use case and the necessary flexibility / reuse. Personally, I've always been with the One method, with a parameter parameter.

+1
source

How about having a "supermethod" with json parameters for maximum flexibility, and then maybe some wrapper methods with more strictly defined parameters in order to have easy access to the most commonly used functions.

+1
source

Source: https://habr.com/ru/post/920351/


All Articles