Is it worth to use if-else statement as Java optional pattern? I came to a point while refactoring using if {} else {} code with Java optionals. While optimizing code it turned to a result to this:


Optional.of(myBoolean).filter(b -&gt; b).ifPresent(b -&gt; {/*my code if true*/});



I recognized filter(b -&gt; b) is not the way i wanted to use optionals thus i turned it to


If.is(myBoolean).then(b -&gt; {/*my code if true*/}).orElse(b -&gt; {/*my code if false*/});



The code for the If class I wrote to handle this is:

public class If {

    private boolean isTrue;

    public If(final boolean value) {
        isTrue = value;
    }

    public If then(final Consumer s) {
        if (isTrue) {
            s.accept(null);
        }
        return this;
    }

    public void orElse(final Consumer s) {
        if (!isTrue) {
            s.accept(null);
        }
    }

    public static If is(final boolean value) {
        return new If(value);
    }
}


Is this a good (whatever good is or should be) approach using the if {} else {} statement in Java?
My first instinct is that your code is a pretty “cool” way to rewrite if else statements, but I don’t think it gains you too much. If you have massive amounts of if trees and want to make them more readable, I would consider using polymorphism to your advantage or, when appropriate, strategy pattern. 

If you want some help refactoring a certain if tree please feel free to post the code and we can help. 

There are often, IMO, two big reasons to make a new abstraction. 

1) to decrease the mental load on the maintainers of this code or 

2) to increase the ability to add new features in the future. 

Both of these are, of course, related. My question is, and feel free to comment below, does your if abstraction decrease maintainer mental load through simplicity or brevity and/or does your if abstraction improve the ability to add new features in the future? If not then you need to consider why you are doing it in the first place.