8 Replies - 469 Views - Last Post: 14 March 2019 - 09:43 PM

#1 jeffindenver   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 186
  • Joined: 07-August 15

How about them optionals?

Posted 28 January 2019 - 11:22 AM

I just discovered optionals. Late to the party, but this is great. :wub:/>

Old code:
    private String getSummary(List<String> source) {
        String summary = "";
        int lastLine = list.size() - 1;

        //The line with Grand Total should always be last
        for (int i = lastLine; i >= 0; i--) {
            if (list.get(i).contains("Grand Total:")) {
                summary = list.get(i);
                break;
            }
        }
        return summary;
    }




Now with optional
    private String getSummary(List<String> source) {
        Optional<String> summary = source.stream()
                .filter(s -> s.contains("Grand Total:"))
                .findFirst();

        return summary.orElse("");
    }



Is This A Good Question/Topic? 0
  • +

Replies To: How about them optionals?

#2 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,837
  • Joined: 13-June 14

Re: How about them optionals?

Posted 28 January 2019 - 01:51 PM

I'm glad that Java finally got an option type, even if I don't do Java any more (I do Scala and being functional in nature, it probably always had an option). I did write a tutorial on Optional a while ago. Personally, in your example, I'd find it more meaningful for the method to return the Optional and let the caller decide what to do if it was empty (though I don't know what context this is in, so perhaps the empty string makes sense here).

I wonder if they'll ever introduce a result type (like Scala's Try or Either).

This post has been edited by ndc85430: 28 January 2019 - 01:52 PM

Was This Post Helpful? 1
  • +
  • -

#3 jeffindenver   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 186
  • Joined: 07-August 15

Re: How about them optionals?

Posted 28 January 2019 - 02:29 PM

Thanks ndc. I hadn't thought about changing the return type, but I can see the value in that. The tutorial is good too, and I bookmarked it for the suggested reading.

I'm reading through Clean Code (again), and finding that my code is like Michael Jackson, Bad. On the plus side, fixing it up the project is fun. And I'm running into cool stuff like Optional. Optional is perfect.
Was This Post Helpful? 0
  • +
  • -

#4 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1354
  • View blog
  • Posts: 4,180
  • Joined: 12-May 09

Re: How about them optionals?

Posted 28 January 2019 - 10:12 PM

View Postndc85430, on 28 January 2019 - 03:51 PM, said:

I wonder if they'll ever introduce a result type (like Scala's Try or Either).


And here I am, still waiting for real union types in Scala.
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,837
  • Joined: 13-June 14

Re: How about them optionals?

Posted 31 January 2019 - 01:17 PM

xclite, what kind of functionality do you think is missing from Scala at the moment?

View Postjeffindenver, on 28 January 2019 - 09:29 PM, said:

I'm reading through Clean Code (again)


Good book!
Was This Post Helpful? 0
  • +
  • -

#6 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1354
  • View blog
  • Posts: 4,180
  • Joined: 12-May 09

Re: How about them optionals?

Posted 01 February 2019 - 11:51 AM

A lot of my complaints are that it has too much functionality :) But I like where 2.13 is headed with the collections changes. I'd like more succinct union and disjoint types (Type1 & Type2, Type1 | Type2). I know I can somewhat get there with `typedef` and `Either`, but it's a little rough.
Was This Post Helpful? 0
  • +
  • -

#7 ccdan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 09-December 12

Re: How about them optionals?

Posted 10 March 2019 - 02:31 AM

View Postjeffindenver, on 28 January 2019 - 08:22 PM, said:

I just discovered optionals. Late to the party, but this is great. :wub:/>/>/>/>

Old code:

Now with optional
    private String getSummary(List<String> source) {
        Optional<String> summary = source.stream()
                .filter(s -> s.contains("Grand Total:"))
                .findFirst();

        return summary.orElse("");
    }


You're comparing apples to oranges, as your old code processes the list in reverse order.

Besides, your old code can be reduced to this:

    private String getSummary(List<String> source) {
        
        //The line with Grand Total should always be last
        for (int i = source.size() - 1; i >= 0; i--) {
            if (source.get(i).contains("Grand Total:"))
                return source.get(i);
        }
        return "";
    }


And if normal order is ok, you can have something like this(and also check for null):

	public static String getSummary(List<String> source) {
		for (String s : source)
			if (s != null && s.contains("Grand Total"))
				return s;
		return ""; 
	}

This looks a lot cleaner to me. If you want the list reversed, you could just use Collections.reverse(list)

As for your "functional" code with Optional, if you get a null value in the list, a NullPointerException will be thrown. Besides, streams operate on duplicate data, which will impact performance & memory usage.

In my opinion the introduction of Optional is a mistake, as people tend to misuse/overuse this class and in most cases it just introduces additional, non-necessary complexity(and unfortunately the same applies for about 80-90% of the "functional" stuff introduced in java 8.) As you can see, it doesn't solve the problem of checking for nulls, you just push the problem around and you get additional complexity. It's been intended for very limited uses, when you need an alternative way to express a "missing" value or a "no result" (people usually use "null" for this, but in some cases null is just a valid value) ... here are some clarifications from Brian Goetz: https://stackoverflo...328555#26328555
Was This Post Helpful? 0
  • +
  • -

#8 jeffindenver   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 186
  • Joined: 07-August 15

Re: How about them optionals?

Posted 11 March 2019 - 08:59 AM

Quoting Brian Goetz:
There's nothing wrong with Optional that it should be avoided, it's just not what many people wish it were, and accordingly we were fairly concerned about the risk of zealous over-use.


I'm pretty happy to have the option to use optional.
Was This Post Helpful? -1
  • +
  • -

#9 ccdan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 09-December 12

Re: How about them optionals?

Posted 14 March 2019 - 09:43 PM

View Postjeffindenver, on 11 March 2019 - 05:59 PM, said:

Quoting Brian Goetz:
There's nothing wrong with Optional that it should be avoided, it's just not what many people wish it were, and accordingly we were fairly concerned about the risk of zealous over-use.


I'm pretty happy to have the option to use optional.

If you code for yourself in your spare time. Otherwise, your happiness will be other people's headaches.
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1