Is there a way to bypass functions that have a large number of arguments?

There is a function that I call once a day:

new SubmitLogs().mail(IP, date_time_UTC, date_time_IST , pageVisited , userCountry , userRegion , city , userAgent); 

Function arguments continue to grow. It originally looked like:

 new SubmitLogs().mail(IP, date_time_UTC, userAgent); 

and now he has 5 more arguments. More arguments are expected during the week. Now I don’t like it. Saving functions with so many arguments is not very good for me. Is there any work for this? I will never want to send 50 arguments to a function if it continues to grow. What makes the call is to email the details in the argument with a short message and a short subject.

+4
source share
6 answers

You have two options:

  • Try and group some of the parameters together into an object. This will bring similar things together. For example, you can put userRegion, userCountry and city together in a Location object

  • As an alternative, the Builder template is good. Josh Bloch Effective Java has a good chapter.

+10
source

It sounds like this:

  • encapsulation problem. Group your arguments together into one or more logical coherent objects.
  • problem with the area. Is your method trying to do too much? Or this is instance type information that you can configure on the containing object.

Once my friend told me.

If you have 10 function arguments, this is usually a sign that you have forgotten 5 more.

+3
source

It looks like you need more objects to represent groups of related objects.

 new SubmitLogs().mail(IP, date_time_UTC, date_time_IST , pageVisited , userCountry , userRegion , city , userAgent); 

It seems that there are several objects awaiting exit.

 public class UserLocation { private string userCountry; private string userRegion; private string city; // ... } 

Now you simplified it a little

 new SubmitLogs().mail(IP, data_time_UTC, date_time_IST, pageVisited, userLocation, userAgent); 

I don’t know your domain, but maybe you can find other data groups to bring them together.

Once you combine your data, you may find that the methods begin to naturally move to these classes. For example, UserLocation may have a method for writing this information to a stream.

+1
source

Create a value object and pass this value object. If it's a remote call or something else, be sure to implement Serializable.

 class MailParams { String ip; String dateTimeUTC; String dateTimeIST; // .... and so on } 
0
source

I would have only the required parameters necessary for sending email as arguments for this method, and all additional parameters would be provided through the card, where the key of this card will be Enum of, which has a list of possible optional parameters and their types and value - this the value of this property for this call.

 new SubmitLogs().mail(sender, receiver, subject, body, ImmutableMap.<SubmitLogMailParams, Object>of(<param1>, <value1>, <param2>, <value2>, ...); 
0
source

Provide various implementations of the method - mail () with 3 parameters, mail () with 10 parameters, etc., so you can use the most suitable one.

0
source

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


All Articles