[erlang-questions] Use of logical "not" in `case`
Robert Carbone
erlang@REDACTED
Fri Feb 9 11:44:15 CET 2018
I am with Loïc, I'd be tripped—
*A flurry of thoughts · · · *
"When writing a case statement based on a boolean expression"
· As we're only working with a boolean(), why use the case at all?
That may be where the confusion is coming form.
The big thing happening here is the true or false, I believe using
variables makes that clearer.
foo(Status, Thingy, Assignments)
->
TF = maps:is_key(Thingy, Assignments),
if
TF =:= false -> {Status, Assignments}
; TF =:= true -> Assignment =
fomulate_assignment(Thingy),
NewAssignments = maps:put(Thingy,
Assignment, Assignments),
{assigned, NewAssignments}
end.
· I tend to put the shorter clause one near the top(see above), so the
reader can get take a breath quicker.
· I am have no problem, mentally, saying "otherwise" when I see a ";
true ->" because of the way Erlang treats 'if' expressions. So it can
come second just as easily.
· The other day I read that the brain can handle complex data types way
better than it candle handle complex logic... I guess anything to does
not add to the confusion, not unlike the 'not', isn't that terrible. ;)
· What Dave Thomas said at last year's conference may apply here:
Programming is Nested Composable Transformation
Rules should be:
1. Work out what should be true ->
2. Make it true ( establishing initial conditions )
3. Keep it true ( during transformations )
/// Also, mentioned,/
/// · Ask yourself, how does the language encourage you to
think about things?/
/// · If you make it easier to think about things in terms
of reducers,
//////the quality of programming will increase.
Cheers,
Rob Carbone
scriptculture.com
/
On 2/9/18 4:40 AM, Loïc Hoguin wrote:
> On 02/09/2018 10:31 AM, zxq9@REDACTED wrote:
> [...]
>> foo(Status, Thingy, Assignments) ->
>> case not maps:is_key(Thingy, Assignment) of
>> true ->
>> Assignment = fomulate_assignment(Thingy),
>> NewAssignments = maps:put(Thingy, Assignment, Assignments),
>> {assigned, NewAssignments};
>> false ->
>> {Status, Assignments}
>> end.
> [...]
>> This isn't a big deal either way of course (I find them both equally
>> readable), but I am curious if anyone has a strong opinion on the
>> issue and why. Which would you prefer to encounter when reading a
>> project to understand how it works on github?
>
> Oh boy. I would definitely get tripped by this. Or at the very least
> have some eyebrow action.
>
> I have no problem with it in something like C because there's usually
> no 'else' clause in these cases and it's therefore very easy to read
> the intent.
>
> But in Erlang's case what you effectively do is invert the boolean
> value below and that requires a little more gymnastic. And I don't
> think it matches what one would say in a conversation because why
> would you mention the case where nothing happens at all?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180209/dfdda4fe/attachment.htm>
More information about the erlang-questions
mailing list