[erlang-questions] Re: The If expression

Jay Nelson jay@REDACTED
Thu Apr 22 19:25:02 CEST 2010


>> > (2)
>> >        verbose_echo(Verbose, Format, Para) ->
>> >
>> >            % Don't print if verbosity is off (false).
>> >         >>> if Verbose -> echo(Format, Para); true -> nil end.
>>
>> verbose_echo(true, Format, Para) ->
>>    echo(Format, Para);
>> verbose_echo(_, _Format, _Para) ->
>>    nil.
>
> This is a TEXTBOOK example of where using Boolean is wrong.

When using the 'if' statement, the issue wasn't quite so apparent.

Using the function clause approach it sticks you in the eye, because  
you have to write a function that matches something (_ instead of  
true) and returns something. Your hand hovers over the keyboard  
wondering what to type.

Another code smell is a two branched function with true and _:

foo(true) -> ...;
foo(_) -> ...

Why _?  Why not just let it crash?  If there is only one value, why  
is it true?

jay



More information about the erlang-questions mailing list