2

I recently found out about the Optional type in java 8 and started using it in my latest project. I used it like:

public class User{
    private int id;
    private String email;
    private Optional<String> mobile;
    public User(int id, String email, Optional<String> mobile){
       ...
    }
    public int id(){...}
    public int email(){...}
    public Optional<String> mobile(){...}
}

Today i was exploring more about Optional when i came across posts which clearly stated that using Optional at places other than return type is a bad practice. After verifying this from multiple sources i changed my code like:

public class User{
    private int id;
    private String email;
    private String mobile;
    public User(int id, String email, String mobile){
       ...
    }
    public int id(){...}
    public int email(){...}
    public Optional<String> mobile(){
        return Optional.ofNullable(mobile);
    }
}

Is this an accepatable solution? Specifically i changed some method signatures taking in Optional to T and then inside the method I converted T to Optional.ofNullable(t) so that i can use methods like ifPresent, orElse, filter, map etc.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
gladiator
  • 722
  • 1
  • 9
  • 16
  • What sources do you have saying that having Optional as a field is bad practice? And what are the drawbacks? – aioobe Dec 25 '14 at 11:46
  • @aioobe Please visit https://stackoverflow.com/questions/23454952/uses-for-java8-optional?rq=1 The argument is that it is a bad java practice, we should use method overloading, constructor overloading instead of taking in Optional parameters. – gladiator Dec 25 '14 at 11:49
  • Yeah. That answer makes a lot of sense. – aioobe Dec 25 '14 at 11:51

1 Answers1

2

I think your alternative implementation of returning Optional.ofNullable is good.

I had to make this change myself when making a class Serializable. As Stuart Marks says in his answer, it adds little value to make the field optional (you can always use mobile() instead of mobile to get the Optional view of the field).

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I was led astray initially because i referred to this tutorial http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html which clearly uses optional fields. What are your thoughts about this? – gladiator Dec 25 '14 at 11:59
  • I don't have that much experience, but I can't see why it would be bad practice. If your class has many null-checks an optional field may improve readability/maintainability. On the other hand, if one has many null-checks, then perhaps one should split the class in two. If you, in your `User`/`mobile` example had several statements such as `if (mobile == null) {...} else {...}` and the logic in fact depended on whether mobile was null or not, it would perhaps make sense to have two `User` subclasses: `UserWithMobile` and `UserWithoutMobile`. – aioobe Dec 25 '14 at 12:30
  • I'm not sure I agree that this is bad practice, now internally it's possible to access the the `mobile` field and generate a `null pointer`. So why not make it impossible by storing it as an optional. – mogronalol Mar 24 '16 at 10:30
  • @mogronalol, refer to Stuart Marks answer over [here](http://stackoverflow.com/a/23464794/276052). – aioobe Mar 24 '16 at 10:45