# Which of these uses of Option do you prefer and why?

**URL:** <https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403>\
**Category:** General\
**Tags:** beginners\
**Created:** [November 8, 2023, 1:53pm UTC](https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403 "2023-11-08T13:53:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GarryP](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@GarryP](https://forums.fsharp.org/u/GarryP)\
**Post date:** [November 8, 2023, 1:53pm UTC](https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403/1 "2023-11-08T13:53:43Z")

</div>

I have some code which used to be:

```auto
type Something = Something of int 

let fromInt something = Something something 

let asOption something = 
 if something = 0 then 
   None 
 else 
   Some (something |> fromInt)

```

…but then I changed ‘asOption’ to:

```auto
let asOption something = 
 if something = 0 then 
   None 
 else 
   something |> fromInt |> Some

```

Which do you think is ‘better’?

I kind of like the way that None/Some start the lines in the first example, makes it more obvious it’s going to be an Option if you only look at that line, but the pipeline seems more idiomatic for F#.

Is there an even better way to do this?  
Am I worrying too much about this?

---

<div class="post-metadata">

**Author:** ![gauthier](https://avatars.discourse-cdn.com/v4/letter/g/919ad9/32.png) [@gauthier](https://forums.fsharp.org/u/gauthier)\
**Post date:** [November 8, 2023, 9:27pm UTC](https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403/2 "2023-11-08T21:27:52Z")

</div>

```fsharp
let asOption = 
    function 
    | 0 -> None 
    | value -> Some(Something value)

```

or

```fsharp
let asOption something = 
    match something with
    | 0 -> None 
    | value -> Some(Something value)

```

I know in my early F# days I overused the pipe operator; in your code, I don’t feel it is needed to use the operator. It is better to use it for things that are laid out on several lines, or if you are doing things like in your code on a single line, it would be to keep a structure similar over several bindings that are next to each others.

> Am I worrying too much about this?

Probably, if you are starting with F#, take it easy and just get your code to do what you need it to, there will come a time where you’ll revisit and do adjustments based on your journey with the language 🙂

---

<div class="post-metadata">

**Author:** ![GarryP](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@GarryP](https://forums.fsharp.org/u/GarryP)\
**Post date:** [November 9, 2023, 9:49am UTC](https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403/3 "2023-11-09T09:49:18Z")

</div>

Thanks for your reply.

I was starting to think that, by analogy, I was building a town and worrying more about the typeface to be used on the street signs than about whether the streets took people to places they wanted to go.

I was having the same sort of concern with Results, e.g.

```auto
try
 …
with 
 | exn -> Error (SomethingError.UnhandledError exn)

```

…versus…

```auto
try
 …
with 
 | exn -> exn |> SomethingError.UnhandledError |> Error

```

…so I thought I’d ask in case it was important. (I don’t want to learn too many ‘bad habits’ at this early stage.)

I think I’ll go back to doing it my original way unless I have some good reason to do otherwise.

Cheers.

---

<div class="post-metadata">

**Author:** ![altbodhi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/altbodhi/32/1253_2.png) [@altbodhi](https://forums.fsharp.org/u/altbodhi)\
**Post date:** [November 10, 2023, 3:52am UTC](https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403/4 "2023-11-10T03:52:23Z")

</div>

[F# Code I Love – Don Syme (advices how to right write code)](https://youtu.be/1AZA1zoP-II)

---

<div class="post-metadata">

**Author:** ![GarryP](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@GarryP](https://forums.fsharp.org/u/GarryP)\
**Post date:** [November 10, 2023, 12:25pm UTC](https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403/5 "2023-11-10T12:25:19Z")

</div>

Thanks for the link.

I’ve already watched that video but I’d lost the link to it somehow, so now I can bookmark it for watching it again (it’s a very interesting video).

---

<div class="post-metadata">

**Author:** ![realparadyne](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/realparadyne/32/169_2.png) [@realparadyne](https://forums.fsharp.org/u/realparadyne)\
**Post date:** [November 13, 2023, 5:18pm UTC](https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403/6 "2023-11-13T17:18:09Z")

</div>

> [@GarryP](#):
>
> ```auto
> try
> …
> with 
> | exn -> exn |> SomethingError.UnhandledError |> Error
> 
> ```

Now I prefer the pipeline here, and if Don disagrees so be it. I like that it shows the transformation in the order it happens from left to right. And the compiler should optimise the pipeline operators away. (if it doesn’t, it should)

In the other form it is completely backwards from the sequence that is intended and even needs brackets because otherwise it wouldn’t be unambiguous.

If it were simple enough that brackets weren’t needed e.g. `exn -> Error exn` then it makes me feel less icky and I’ll go with it.

But that’s just my preference.

---

<div class="post-metadata">

**Author:** ![GarryP](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@GarryP](https://forums.fsharp.org/u/GarryP)\
**Post date:** [November 15, 2023, 9:47am UTC](https://forums.fsharp.org/t/which-of-these-uses-of-option-do-you-prefer-and-why/3403/7 "2023-11-15T09:47:23Z")

</div>

Thanks for giving an alternative viewpoint.

While I like the idea of having the ‘eventual type’ at the start of the line (easier to see what’s going to happen eventually) I also like the idea of using the pipeline to show the ‘encapsulation flow’ of types within types (read to the end to see what happens).

A ‘hybrid’ style – ‘up-front’ when only one pipe would be required (e.g. Error exn) and pipeline otherwise (exn |\> MyError |\> Error), may also be an option.

I think I’ll probably end up ‘flip-flopping’ between different styles as I (hopefully) become more proficient in F# and eventually come up with a style that I like, maybe without realising that I’ve made a decision.
