[erlang-questions] Question about using case statements
Kostis Sagonas
kostis@REDACTED
Sat Aug 28 09:24:54 CEST 2010
James Aimonetti wrote:
> Hey list,
>
> Wondered what the list thought of this usage of case statements: I
> have a file path and a proplist with some values, one of which tells
> me what to do with the file. I want to be sure the file exists before
> I do something with the file but didn't want to get too deep into
> nested case statements, so I have something like this ->
>
> ...
> Prop = get_proplist_from_somwhere(),
> File = get_file_from_somewhere(),
> case filelib:is_regular(File) andalso get_value(key, Prop) of
> false ->
> handle_missing_file_error(File);
> specific_value_1 ->
> do_specific_value_1_action(File, Prop);
> ...
> _Val ->
> io:format("Unhandled file action value ~p~n", [_Val]) % to be
> removed after testing
> end,
> ...
>
> Is this common / acceptable use of the andalso short-circuit boolean?
It's something you can do alright, but it's not common.
I personally think that in this particular case it is unnecessary. The
following code expresses the code's intention much clearer, IMO:
...
File = get_file_from_somewhere(),
case filelib:is_regular(File) of
false ->
handle_missing_file_error(File);
true ->
Prop = get_proplist_from_somwhere(),
case get_value(key, Prop) of
specific_value_1 ->
do_specific_value_1_action(File, Prop);
...
_Val ->
io:format("Unhandled file action value ~p~n", [_Val]) % to be
removed after testing
end
end,
...
and it is not much more verbose.
Kostis
More information about the erlang-questions
mailing list