Write parameter names in a method call

I would like to write down the names of the parameter methods when the method is called, and not just specify the parameter itself. Example:

public exampleMethod(boolean xWasDone) {} 

Known way to call:

 exampleMethod(true); 

Is there any way to call it with an explicit name? Something like that?:

 exampleMethod(xWasDone: true); 

I believe the latter is much readable and, therefore, interested in the correct way to write such calls.

+5
source share
7 answers

This syntax is not supported in Java.

It can be argued that it is not needed, since the order of parameters in function signatures is heavily used in Java.

In addition, you should remember that the overloading function is supported in Java, where several functions can have the same name but a different number of parameters. This means that you cannot omit any particular input to avoid inadvertently calling another overloaded function.

+6
source

I (had) this problem with booleans too. The syntax you suggest is not possible in Java, but Java allows something similar. You can switch to enum s

Enhanced readability: Use Enum instead of booleans

Define an enumeration

 enum Status { DONE, TODO }; 

Use it in your method

 public exampleMethod(Status status) { if (status == DONE) ... } 

And in your method call

 exampleMethod(Status.DONE); 

In addition to the obvious increase in readability, this also makes it easier to refactor and expand, for example, if a different status is required.

+4
source

If this is really just about readablitiy you could write a methodcall like this:

 someMethod(/*param1*/ true, /*param2*/ 133.7, /*param3*/ "foobar"); 

But it really is not necessary! Example: you have this method

 public void setPersonData(String gender, int age, String name) { ... } 

You can call the method as follows:

 String str1 = "male"; int int1 = 56; String str2 = "Rudolf"; setPersonData(str1, int1, str2); 

In this case, you are right, it is not very readable! But if you write it like this:

 String gender = "male"; int age = 56; String name = "Rudolf"; setPersonData(gender, age, name); 

You will not have problems with readability!

+4
source

In Java, this is not . However, you can define and initialize the parameter somewhere else, and then simply call it with this parameter. But I would not do it if there was no business logic behind it.

+3
source

If readability is your problem, consider using one of these approaches:

  • define a boolean variable that you will then use:

     boolean wasDone = true; exampleMethod(wasDone); 
  • define the constant that you then use:

     private static boolean WAS_DONE= true; ... exampleMethod(WAS_DONE); 
  • avoid having a boolean parameter, use an enumeration instead:

     exampleMethod(Status.WAS_DONE); 
+2
source

Java does not support named parameters what you are looking for.

However, you could program against an elegant idiomatic approach with named parameters, as described in this post, Named Named Parameter in Java

In Java8, you can access these parameters of the named_parameters_in_java_8 methods; but this is far from what you are actually trying to achieve, right?

+1
source

There are already many completely correct answers, so I will not repeat anything that has already been said, however, one of the methods that you may find acceptable is to make the variable a variable as a parameter:

 String name = "George"; boolean age = 57; boolean iq = 66; Person person1 = new Person(name, age, iq); Person person2 = new Person(name = "Beth", age = 65, iq = 69); 

A slightly simplified matter and, of course, not necessarily here, but in more complex cases (for example, with 5 similar buckers that you would need to keep in order), this would help:

 draw1(g2, false, true, false, true); 

or

 draw1(g2, outline = false, bold = true, italic = false, underline = true); 

Note. Ordering still matters (you can't switch in bold and italics, for example), but at least you can easily see which parameter does.

+1
source

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


All Articles