Return atom or tuple when input has not changed

I’m figuring out the best way to solve the following problem in two different ways.

https://gist.github.com/hl/c70ce2560aaaa7fc0ebf5fe796a1a3d3

Version 1 (greeter1.ex - line #4)
:ok <- validate(name) do

Version 2 (greeter2.ex - line #4)
{:ok, name} <- validate(name) do

I believe version 1 is the best, I’m only checking to see if my value is valid and not changing it, though version 2 is more consistent with the rest of the code. (always returning a tuple)

What do you alchemists think?

2 Likes

I would go with the second option, seems more consistent with other functions returns, and even consistent with itself if you decide do return {:error, "the message"} when the validation fails.

1 Like

validate/1 will either return :ok or {:error, "Too short"} in the first version but I can’t shake the feeling that returning {:ok, name} in validate/1 (second version) is kinda senseless.

1 Like

Well, I share your opinion here, it’s kinda senseless in this case, but I really think that maintaining the same return type for the functions is good.