[erlang-questions] Some functions must return values other may do so...

Loïc Hoguin essen@REDACTED
Sun Aug 16 16:10:36 CEST 2015


On 08/16/2015 04:05 PM, Fred Hebert wrote:
> On 08/16, Loïc Hoguin wrote:
>> It's not just about what you return!
>>
>> Imagine you are validating input.
>>
>> There are two ways to go with validating input:
>>
>> * You detect all errors and display a nice message to the user
>> * You crash on the first error and stop there (no need to do more if
>> you do machine to machine)
>>
>> The functions do the same thing, validate input, take the same
>> arguments, and return the validating input in a usable format.
>>
>> And you don't have to reimplement everything twice! It's just the
>> *interface* that has two functions. You can have a special argument
>> for this (or a special value in place of your error accumulator)
>> inside the private function that implements the input validation.
>>
>
> Yeah, there I tend to pick one and document what it does. How often do
> you end up validating at multiple points in the program and do it in
> different ways every time?
>
> If you need to re-validate input multiple times or places, I'd instead
> recommend to move your validation to the edge of the system where it an
> be done once and for all.
>
> I guess I can't understand the need as in ~6-7 years programming Erlang,
> I have never encountered the need to do that in a way that couldn't be
> fixed by that way of doing things.

Well I *do* write Cowboy, which is typically at the edge of the system, 
and is used for a large number of different use cases, so picking one 
over the other, for input validation or otherwise, is a pretty difficult 
exercise...

-- 
Loïc Hoguin
http://ninenines.eu
Author of The Erlanger Playbook,
A book about software development using Erlang



More information about the erlang-questions mailing list