Sign in

Useful design pattern at functional way at Java


Imaging you have to visit a doctor. You call to emergency service and make appointment to visit therapist tomorrow. Next day you go to hospital and face with trouble. There is only one door and no nameplate on it! It makes you very confuse. You have no choice and open the door and ask for therepist. Doctor tell you that he is a surgeon. So you ask where to find therepist. Unfortunately, he don’t know. It is not my responsibility what he said. You can see at next door he added at last. You notice another door at the room…


Hey! I have a function:

* you can read about 'def' method in my article at medium

But my teammate write such code:

And code execution output:

Exception in thread "main" java.lang.NullPointerException
at com.github.darrmirr.fp.contract.Main.lambda$main$0(
at com.github.darrmirr.fp.contract.Main.main(

Something goes wrong and NullPointerException (NPE) is thrown while main method is executing.

My function and I unexpect that someone pass `null` object as function input value. Hopefully, I have another function that could protect my `cubeVolume` one. Contract is function’s name.

Contract function is function that applied to input value before one is passed to target function. Where target function is…


What is more expressive and shorter Java function definition?

Function<Integer, Function<Integer, Integer>> sum = x -> y -> x + y;


var sum = def((Integer x) -> (Integer y) -> x + y);

In my opinion, the second definition is more expressive and shorter. If you agree with me I recommend to read further.

You can feel free to move to Conclusion part in order to read `def` method implementation.

Problem description

Lambda expression has introduced at Java 8. And It looks like this:

Function<Integer, Integer> square = x -> x * x;

Looks pretty enough, is it? …

Darr Mirr

Teamlead and software developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store