by king_magic · 11y ago · 0 comments
> Use threading. Instead of naming each intermediate value, thread the value through the process without naming it.
What does that even mean?To be honest, I'm not even really sure what the author's point is. Naming is hard? Naming is bad? Don't name things? Name everything? It just feels like an abstract nonsense piece.
Here is an example of the main part of a Haskell program (using the "pipes" combinators). It starts two concurrent pipelines. One reads user commands from a queue, encodes them, then writes them to an AMQP server. The other reads results from the AMQP server and runs them through a more complicated pipeline, the details of which aren't very interesting.
runEffectsConcurrently
(contents commandQueue >-> encoder >-> writeTo amqp)
(readFrom amqp >-> decoder
>-> skipEmpty
>-> logToFile
>-> forever (decodeMsg state)
>-> toChannel eventQueue)
What I like about this style has to do with how it avoids naming intermediate results. Instead of stuff like loops, anonymous callback functions, and variables, it approaches a declarative specification of the program's structure & behavior. I still had to give names to each component, but I didn't need to come up with names for the values that flow through the pipelines.Beyond that, though - the above code looks great - until you need to debug it.
Personally, I'd rather pay a 'naming tax' on things for the sake of readability / debugability over what the original author is shooting for any day.
I have to say, minimizing intermediate holding names makes debugging and writing clear code much easier. 97% of the time, I never miss the "naming tax".
It means don't do this:
int x = k + 3;
int y = v * x;
int z = y - foo();
return z;
Instead, do this: return (k + 3) * v - foo();Also, for the record, I totally disagree with the author on this. This:
return (k + 3) * v - foo();
works great, until you need to debug it. Good luck then. (~> (sha1 in)
(bytes->hex-string)
(bytes-append #".")
(displayln))
In pseudo-Python this would be: displayln(bytes-append(bytes->hex-string(sha1(in)), #"."))
http://www.greghendershott.com/2013/05/the-threading-macro.h... A = max(3, 5)
B = max(A, 7)
C = max(20, B)
C = max(20, max(max(3, 5), 7))