I’m speaking in reference to a file called server1.erl
. The code, as-presented in his book, compiles, and the modules do what they are supposed to do inside erl
.
The code I’m interested in:
File: server1.erl
...
start(Name, Mod) ->
register(Name, spawn(fun() -> loop(Name, Mod, Mod:init()) end)).
...
File: name_server.erl
...
add(Name, Place) -> server1:rpc(name_server, {add, Name, Place}).
...
erl-repl:
server1:start(name_server, name_server).
I remember experiencing alarm bells the first time I saw that. Why is he doing that? Then I realized that I could say:
server1:start(ns, name_server).
And be able to send messages to a shorter-name atom (“because it looks cool”).
But if I do that, then I’d also have to edit my client code, because the client code is hard-coded to use “name_server” as the atom to which it sends messages.
So… I edited client and server to not use variable Name, and it still works. Then I started to recognize a few things about the original and the new that I wasn’t seeing before. I’ll stop there.
At first I thought it was a ‘mistake’, because if I recall correctly (50% chance at best), the iterations he does with server2, server3, server4, all use that same serverN:start(Name, Mod)
signature.
Then I thought, or maybe I could figure out a way to parameterize the reference to “name_server” inside the client, then I could correctly say serverN:start(ns, name_server)
.
My final thought was that he might have done it on purpose. Because it was (to me) so very obviously ‘not right’, that a part of me assumes I must be the one who is mistaken.
Why do you think Mr. Armstrong made the decision to write the code as he did?