21

I would like to write down the names of parameter methods while calling a method, not just giving the parameter itself. Example:

public exampleMethod(boolean xWasDone) {}

The known way of calling this is:

exampleMethod(true);

Is there a way to call it with explicitly naming the parameter? Something like this?:

exampleMethod(xWasDone: true);

I consider the latter much more readable and would thereby be interested in a correct way of writing my calls like that.

paulamoskreiner
  • 318
  • 1
  • 3
  • 7
  • 2
    you could save the value of xWasDone in its own variable (boolean xWasDone = true) and call the method like that: exampleMethod(xWasDone) – Rhayene Nov 11 '15 at 14:23
  • 1
    No, it cannot be done in Java. – Maroun Nov 11 '15 at 14:23
  • I fyou have multiple parameters you can use a method signature taking a(n immutable) `Map`. – Mena Nov 11 '15 at 14:24
  • If you insist, in this particular case, maybe `Boolean.TRUE` is more readable for you. But it's ugly for me (and it's an object not a primitive). – Maroun Nov 11 '15 at 14:25
  • 1
    You could put comments in your code `exampleMethod(/*xWasDone*/ true);`. – khelwood Nov 11 '15 at 14:25

8 Answers8

20

This syntax is not supported in Java.

It can be argued that it is not needed as the order of parameters in function signatures are strongly enforced in Java.

Also, you should be mindful that function overloading is supported in Java, where multiple functions may have the same name but different number of parameters. This means you cannot omit any specified input, in order to avoid calling another overloaded function unintentionally.

Alex
  • 8,093
  • 6
  • 49
  • 79
Ling Zhong
  • 1,744
  • 14
  • 24
  • 1
    Oracle PL/SQL has overriding too and allows referencing parameters by name all the same, along with optional parameters. What problem do you see in that? – Jiri Tousek Nov 11 '15 at 14:36
  • I'm not familiar with Oracle PL/SQL. Is referencing parameters by name mandatory? And how are the optional parameters specified? – Ling Zhong Nov 11 '15 at 15:09
  • Referencing by both parameter order and name allowed there, even mixed approach (`call(1, 2, c => 3)`). Omitted parameters are default-ed. But I realize it might not be a good example as the result is rather confusing. – Jiri Tousek Nov 11 '15 at 15:23
8

if it's really only about readablitiy you could write the methodcall like this:

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

But it really shouldn't be necessary! Example: you have this method

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

You could call the method like this:

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

In this case you're right, it's 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 any problems with readability!

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
  • I believe the most important reason for passing arguments by name is not readability, but correctness. When the function takes 5+ parameters, the coder is likely to accidentally misplace them producing a logical mistake. – Klesun Dec 31 '21 at 17:12
  • C# supports this feature for exactly this reason. – Henry Apr 26 '22 at 03:33
6

I have (had) this issue with booleans too. The syntax you propose is not possible in Java, but Java allows something similar. You can switch to enums

Increase readability: Use Enum instead of booleans

Define an enum

enum Status { DONE, TODO };

Use it in your method

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

And in your method call

exampleMethod(Status.DONE);

Besides the obvious increase in readability, this also allows easier refactoring and extendability, for example if another status is needed.

Community
  • 1
  • 1
Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
3

This is not possible in Java. However, you could define and initialize the parameter somewhere else and then just call it with this parameter. But I would not do that if there is no business logic behind it.

Rufi
  • 2,529
  • 1
  • 20
  • 41
2

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

  • define a boolean variable that you'll then use:

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

    private static boolean WAS_DONE= true;
    ...
        exampleMethod(WAS_DONE);
    
  • avoid having the boolean parameter, use enum instead:

    exampleMethod(Status.WAS_DONE);
    
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
2

There are a lot of completely valid answers already so I'm not going to repeat anything anyone has already said however one method that you may find acceptable would be to do a variable assignment 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);

Slightly oversimplified case and definitely not necessary here but in more complicated cases (like with 5 similar booleans that you'd have to keep in order in your head) this would help:

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

or

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

Note: Order is still relevant (you can't switch bold & italic for example) but at least you can easily see which parameter does what.

geowar
  • 4,397
  • 1
  • 28
  • 24
1

Java does not support named parameters, that's what you're looking for.

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

In Java8 you can access the methods' parameters like so named_parameters_in_java_8; but this is far from what you're actually trying to achieve, right?

Community
  • 1
  • 1
Filip
  • 1,214
  • 10
  • 19
0

This is probably a late response but I just came across something similar to this today and wanted to let new readers explore this.

Even though java does not support this, I do believe that this increases readability of code. If you are really interested in using this, you can explore IntelliJ IDE which by default supports this.

Eclipse discussion on introducing this feature : https://bugs.eclipse.org/bugs/show_bug.cgi?id=529011

Link where I came to know about this : https://youtu.be/yVzi3wuTUE4?t=946

Happy Hunting.

Update me if any corrections needed.

  • I believe you are referring to IntelliJ IDEAs Inlay Hints: https://www.jetbrains.com/help/idea/inlay-hints.html. I didn't watch much of the video, and I didn't find any mention of it in the comments of the video quickly scanning it, so I thought it would be worth providing the name and a link to the documents explaining it. Love this feature, by the way. It can provide more information besides just parameter names. – Ashaelon Jun 15 '22 at 19:07