[erlang-questions] Re: The If expression

Richard O'Keefe ok@REDACTED
Thu Apr 22 07:56:59 CEST 2010


On Apr 22, 2010, at 4:59 PM, Jay Nelson wrote:

> Henning Deidrich offered one-armed ifs to rewrite:

[I haven't seen that one yet]

> > (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.
It is normal to have more than two levels of verbosity.
syslog has
      Emergency     (level 0)
      Alert         (level 1)
      Critical      (level 2)
      Error         (level 3)
      Warning       (level 4)
      Notice        (level 5)
      Info          (level 6)
      Debug         (level 7)

Which subset of these corresponds to 'true' and which to 'false'?
If you aren't using all of the syslog levels now, you will very
likely be using more than two of them in the not too distant future.

There is also a classic trap:  you talk about one of the verbosity
levels being "off".  Sooner or later, someone who reads about verbosity
being "off" will try verbose_echo(off, Format, Para) or perhaps
verbose_echo(on, Format, Para).  So why not use 'off' and 'on' as the
levels in the first place?  Better still, why not use level names like
'verbose' and 'laconic'?  And in the spirit of making broken code crash
it would be even better to write

verbose_echo(verbose, Format, Para) ->
     echo(Format, Para);
verbose_echo(laconic, _Format, _Para) ->
     nil.
>



More information about the erlang-questions mailing list