[erlang-questions] Simple Erlang Recommendation (last returned value)

attila.rajmund.nohl@REDACTED attila.rajmund.nohl@REDACTED
Sat Jul 26 22:19:07 CEST 2008


On Sat, 26 Jul 2008, Hynek Vychodil wrote:
[...]
> Imagine this:
>
> X = tl(L),
> ... many messy lines ...
> *X = tl(X),
> ... another many lines ...
> *X = tl(X),
> ... another many almost unreadable messy lines ...
> sensitive_usage(X), % <--- buggy X value observed here
>
> And my question is, where Value of X came from? It goes from L, but how long
> it take and how many errors you can do when you will investigate?

Pretty easy with the right tools. However, check this example:
The original code looks like this:

X = tl(L),
% many messy lines using X
X2 = tl(X)

and should be changed to this:

X = tl(L),
% many messy lines using X
X2 = tl(X),
% many messy lines using X2
X3 = tl(X2)

but the coder ends up with this:

X = tl(L),
% many messy lines using X
X2 = tl(X),
% somewhere the coder updated X to X2, so the compiler won't save us
X3 = tl(X) % this one place the coder forgot to update X to X2

I don't know other IDEs, but vim can highlight a single variable and its
uses in a function, so it's pretty straightforward to find where the
value of X comes. On the other hand, catching this bug is harder.
Actually at one place I saw "X8" in the code...

> It is reason why once variable assignment is in Erlang and why it is good
> think. One advice for you: Don't change well proved language until you worth
> know it.

In my opinion this feature leads to the most unreadable erlang code,
exactly because I can't see the flow of operations on the same data,
because each operation has to change the variable name.

 				Bye,NAR
-- 
"Beware of bugs in the above code; I have only proved it correct, not
  tried it."



More information about the erlang-questions mailing list