logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Spooky-code-at-a-distance.md (3147B)


  1. ---
  2. title: Spooky action at a distance
  3. date: 2021-01-19
  4. ---
  5. Einstein famously characterized the strangeness of quantum mechanics as "spooky
  6. action at a distance", which, if I had to pick one phrase about physics to be my
  7. favorite, would be a strong contender. I like to relate this to programming
  8. language design: there are some language features which are similarly spooky.
  9. Perhaps the most infamous of these is operator overloading. Consider the
  10. following:
  11. ```
  12. x + y
  13. ```
  14. If this were written in C, without knowing anything other than the fact that
  15. this code compiles correctly, I can tell you that x and y are numeric types, and
  16. the result is their sum. I can even make an educated guess about the CPU
  17. instructions which will be generated to perform this task. However, if this were
  18. a language with operator overloading... who knows? What if x and y are some kind
  19. of some Vector class? It could compile to this:
  20. ```
  21. Vector::operator_plus(x, y)
  22. ```
  23. The performance characteristics, consequences for debugging, and places to look
  24. for bugs are considerably different than the code would suggest on the surface.
  25. This function call is the "spooky action" — and the distance between the
  26. "+" operator and the definition of its behavior is the "distance".
  27. Also consider if x and y are strings: maybe "+" means concatenation?
  28. Concatenation often means allocation, which is a pretty important side-effect to
  29. consider. Are you going to thrash the garbage collector by doing this? *Is*
  30. there a garbage collector, or is this going to leak? Again, using C as an
  31. example, this case would be explicit:
  32. ```
  33. char *new = malloc(strlen(x) + strlen(y) + 1);
  34. strcpy(new, x);
  35. strcat(new, y);
  36. ```
  37. If the filename of the last file you had open in your text editor ended in
  38. `.rs`, you might be frothing at the mouth after reading this code. Strictly for
  39. the purpose of illustrating my point, however, consider that everything which
  40. happens here is explicit, opt-in to the writer, and obvious to the reader.
  41. That said, C doesn't get off scott-free in this article. Consider the
  42. following code:
  43. ```
  44. int x = 10, y = 20;
  45. int z = add(x, y);
  46. printf("%d + %d = %d\n", x, y, z);
  47. ```
  48. You may expect this to print out `10 + 20 = 30`, and you would be forgiven for
  49. your naivety.
  50. ```
  51. $ cc -o test test.c
  52. $ ./test
  53. 30 + 20 = 30
  54. ```
  55. The savvy reader may have already figured out the catch: add is not a function.
  56. ```
  57. #define add(x, y) x += y
  58. ```
  59. The spooky action is the mutation of x, and the distance is between the apparent
  60. "callsite" and the macro definition. This is spooky because it betrays the
  61. reader's expectations: it looks and smells like a function call, but it does
  62. something which breaks the contract of function calls. Some languages do this
  63. better, by giving macros an explicit syntax like `name!(args...)`, but,
  64. personally, I still don't like it.
  65. Language features like this are, like all others, a trade-off. But I'm of the
  66. opinion that this trade is unwise: you're wagering readability, predictability,
  67. debuggability, and more. These features are toxic to anyone seeking stable,
  68. robust code. They certainly have no place in systems programming.