-4

Just a quick question regarding good programming practices,

Performance aside, how much of a good/bad idea could something like this be? I am oversimplifying the problem to get my point across, but is something like this totally wrong?

public void methodWithInputString(String data) {
    // do something with data
}

public void methodThatCallsTheAbove() {
    methodWithInputString(

        // lambda with no input and a string as output
        (() -> {

            if (this.conditionIsTrue)
                return "Condition is true";
            else
                return "Condition is false";

        }).apply();

    );
}

The alternative would simply be:

public void methodThatCallsTheAbove() {
    if (this.conditionIsTrue)
        methodWithInputString("Condition is true");
    else
        methodWithInputString("Condition is false");
}

I understand that the second is the obvious/correct approach but in a situation where using the first one makes my code cleaner is it still a nonsensical thing to do?

  • If you find yourself using a lambda in the same method it is defined, you might want to write a method instead. – Salem Feb 09 '17 at 17:05
  • Sure, *if* you feel it's cleaner in some cases. But the example you gave is very bad. First of all, it cannot compile. And, you could've used `?:` – ZhongYu Feb 09 '17 at 17:13
  • Readability suffers. A method name is virtually self-commenting. An empty lambda needs an actual comment, and doesn't provide for ease of growth. – Compass Feb 09 '17 at 17:15
  • @ZhongYu Yes doing something like this in the example i provided wouldn't make sense at all, the second one is obviously more readable. My example was simply to get my point across. Also, may i ask why it would't compile? – WiserTheBassist Feb 09 '17 at 17:17
  • 1
    It's a pointless idea to define a lambda that you immediately call. – Andy Turner Feb 09 '17 at 17:19
  • I agree with Andy. Why don't you just write the code that's in your lambda as code *before* the method call, assigning the result to a local variable, then use that variable as the parameter to the method call? It would clarify the code by separating the logic for building the value from the method call, and the variable name would hopefully help to document the meaning of the value. – Andreas Feb 09 '17 at 17:28
  • It doesn't compile, just try it:) I think I understand where the question comes from. Sometimes, within a method, we want a block of code written like a method body, with `return` and such. We can't do that in Java, but there are workarounds. – ZhongYu Feb 09 '17 at 17:30
  • Why not `methodWithInputString(conditionIsTrue? "Condition is true": "Condition is false");`? Or `methodWithInputString("Condition is "+conditionIsTrue);`… – Holger Feb 10 '17 at 14:01

1 Answers1

0

+1 for Andy Turner remark. Plus: Avoid object properties reference inside your lambda.

I guess a compilable version of what you are trying to show would be like this:

public class Demo {

    private static final Function<Boolean, String> GET_STRING_DEPENDING_ON_BOOLEAN = flagValue -> "Condition is ".concat(String.valueOf(flagValue));

    private boolean conditionIsTrue;

    public void methodWithInputString(final String data) {
        // do something with data
    }

    public void methodThatCallsTheAbove() {
        methodWithInputString(GET_STRING_DEPENDING_ON_BOOLEAN.apply(this.conditionIsTrue));
    }

}

The only advantage of doing this (using a lambda instead of a method) is to be able to re use this lambda and chaining something else with.

Example:

    methodWithInputString(GET_STRING_DEPENDING_ON_BOOLEAN.andThen(data -> data.concat("somethingelse")).apply(this.conditionIsTrue));
kij
  • 1,421
  • 1
  • 16
  • 40