• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Which is more readable?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following functions that operates on two option types, which one from your point of view is readable?



OR



From my point of view the first one is more readable while the second one if more elegant! What do you think?
 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess second one is more elegant as well as readable.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Khattri wrote:I guess second one is more elegant as well as readable.



Only if you already have some experience in Scala to understand what a flatMap does. If not the first version is always readable from my point of view!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it easier to see what this method does in the first version: it takes two options and a function f and applies the function only when the two options are both not empty.
 
Marshal
Posts: 5996
417
IntelliJ IDE Python TypeScript Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find the first one easier to read, but that's probably only because I don't naturally 'see' how flatMap functions. Otherwise it's likely that the second would be the preferred choice.

But, even the first option looks a little odd to me. I would write it with the cases the other way round, which would negate the need for the OR operator. For example:
 
Tim Cooke
Marshal
Posts: 5996
417
IntelliJ IDE Python TypeScript Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, the function name 'mapFn' is a terrible name. If we're on the topic of readability then a meaningful name would be the most effective improvement to make in this example.
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another approach is to use a for comprehension over your two input Options:

This avoids the map stuff but is a lot easier to read than the explicit case checks on the Options. YMMV.

[Edit:]
In case you're curious:

  • This works because you can apply a for-comprehension to any class that provides map, flatMap, withFilter and foreach functions (see Chapter 23 of "Programming In Scala (2nd edition)" by Martin Odersky).
  • Here we are looking at Options, and Options extend the Iterable trait, which supplies these functions. That's partly why Options behave in many ways like a collection that contains precisely one item or none.
  • So you can run a for-comprehension over an Option and it will yield Some(result) for Some(input), or None if the input Option was None.

  • I really like this trick!
     
    chris webster
    Bartender
    Posts: 2407
    36
    Scala Python Oracle Postgres Database Linux
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tim Cooke wrote:I find the first one easier to read, but that's probably only because I don't naturally 'see' how flatMap functions. Otherwise it's likely that the second would be the preferred choice.

    But, even the first option looks a little odd to me. I would write it with the cases the other way round, which would negate the need for the OR operator. For example:


    You can make the default case even simpler because we don't care what we received if it wasn't a pair of Some options:
     
    Joe San
    Ranch Hand
    Posts: 10198
    3
    Mac PPC Eclipse IDE Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Wow! It was worth the discussion!
    reply
      Bookmark Topic Watch Topic
    • New Topic