Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With / Without Example Missing #1

Open
haimivan opened this issue Oct 25, 2017 · 1 comment
Open

With / Without Example Missing #1

haimivan opened this issue Oct 25, 2017 · 1 comment

Comments

@haimivan
Copy link

Hi,

the benefits of null4j do not come to my easily.

If I compare your documentation with the lombok documentation, it would be great if you had (like lombok) a comparison:
"code looks like with null4j" against "code looks like without null4j"

Thanks!

@Michael-Zinn
Copy link
Contributor

Michael-Zinn commented Dec 8, 2017

Hi haimivan,

Good idea, I'll add it to the ReadMe for the next version. In the meantime, here's an example for let:

With null4j:

return let(person,
    Person::getAddress,
    Address::getStreet,
    Street::getStreetName);

With Java 8 Optionals:

return Optional.ofNullable(person)
    .map(Person::getAddress)
    .map(Address::getStreet)
    .map(Street::getStreetName)
    .orElse(null);

Raw Java:

if(person == null) return null;
Address address = person.getAddress();
if(address == null) return null;
Street street = address.getStreet();
if(street == null) return null;
String streetName = street.getStreetName();
if(streetName == null) return null;
return streetName;

Here's one for requireNonNullElse:

With null4j:

return requireNonNullElse(
    getLastName(),
    getFirstName(),
    "No Name!"
);

Without null4j:

String lastName = getLastName();
if(lastName != null) return lastName;
String firstName = getFirstName();
if(firstName != null) return firstName;
return "No Name!";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants