Friday, February 28, 2025

The Go language creator mindset


  1. Let's take C and move the argument type to the back, because pointers to pointers are cumbersome to read. This makes the syntax of a defined array: var primes := [6] int {2, 3, 5, 7, 11, 13}
  2. While we're moving things to the back, let's also move the function's return arguments to the back too. That way, all those C programmers start to appreciate their dyslexia.
  3. Let's have pointers, but we're all about safety, so no pointer arithmetic.
  4. Since we don't have pointers to arrays, let's create slices. You update one slice and it updates all the slices of that array! But hey, no pointer arithmetic, so we're preventing programmer created bugs.
  5. And we want all the goodies from a Python list, so let's make slices dynamically sized. For some reason, we still need arrays.
  6. And we love Python so much, we'll add the ability to iterate over a range and have a dictionary type called map.
  7. Let's rename typedef as type, because we save 3 characters and we're all about efficiency. But let's leave struct in, we're not just changing keywords for the sake of it are we? And struct members are accessed  with a dot even when the variable is a pointer to a structure. Let's add arbitrary syntax since we're just making things up a this point.
  8. And let's get rid of breaks in case blocks, because we're all about efficiency.
  9. While we're at it, let's introduce a "naked" return that magically returns all the named arguments. Readability guy took the day off when this was added.
  10. And let's start time 0 on the day Go went open source - acknowledging the momentous day that must take precedence over UNIX's, Python's and every other major language's well established start time.
  11. Let's import "import" from Python, but let's add the option to import multiple modules using a parenthesis, because ... efficiency.
  12. Let's take the package concept from npm, but let's add our nifty touch to it by having a "tidy" keyword to updated dependencies.
  13. Let's solve those pesky multiple returns in a function by adding a defer keyword, because readability is important, but not that important. And did the readability dude resign or something?
  14. The parenthesis around for need to be DOGE'ed too - programmers can save a whole of 3 seconds per day skipping the typing. It's OK to spend a few minutes deciphering the for intent - makes you understand the code better. And while was such a burden on C programmers - let's just get rid of it. After all, what could while do that for couldn't?
  15. C++ is admittedly a complex language. Let's avoid classes in Go and instead add a receiver that lets one add a method to a type. And let's make the syntax: func <pointer to receiver> <method> (<method arguments>) <return type>. See, the syntax in #1 now makes total sense. 
  16. And breaking with the random logic that drives the rest of the syntax, let's constrain the receiver restricted to the same file as the method. The "pointer to receiver" part is important, without which the method behaves as a function that operates on a value and not the reference.
But Go isn't alone in creating garbage syntax. Here's some incomprehensible JS code:
net.createServer(stream => {
  stream = toPull.duplex(stream) //turn into a pull-stream
  //connect the output of the net stream to the muxrpc stream
  //and then output of the muxrpc stream to the net stream
  pull(stream, server.stream, stream)
}).listen(8080)
//connect a pair of duplex streams together.

const stream = toPull.duplex(net.connect(8080))

pull(stream, client.stream, stream)

Let's deconstruct:

First, the problems as seen by a C programmer

  1. stream is used before it's defined
  2. The anonymous arrow function takes one argument stream and returns a closure that uses and initializes the same argument, stream.
  3. The function pull has 3 arguments out of which 2 are the same. This is reasonable - if stream is input at one spot and output at another. 
Here's why it works:
  1. The function call inside createServer is a CALLBACK. It is not immediately called.
  2. The listen on port 8080 starts immediately.
  3. Once the client connects using toPull.duplex, the server calls the function inside createServer with stream being initialized to the server side of the connected stream.
Callbacks are the bane of programming. ISRs are perhaps the only legitimate usage for callback constructs. For all other purposes, I would recommend using OS IPC primitives for asynchronous code.

Well, we started with Go and ended up with callbacks. Clearly, I got into the Go spirit of things too deeply.


Wednesday, February 26, 2025

My Uncle's Magical Printer

My uncle's health and happiness was the envy of the extended family and neighbors. Growing up, I would wonder how one man could be clever, strong, funny and creative! The kids would sing the songs he'd write, the grown ups would watch the movies he was in, and on and on.

It turns out, the reason he was so happy, all the time, is that he had a magical printer at home. It printed crisp color photos and laser sharp documents like any other printer, but the real magic was when it printed green. Whenever he needed to buy something, he'd simply make up a number, print the number in green and that's it - he could use that printed sheet to buy anything in the world!

Over time, I could see that age was catching up with my uncle. He had become less dashing, his steps appeared unsure, his machismo was lost. He was softer, kinder and sadly weaker.  But now, he's passed on and I have inherited the printer. I start to examine it closely. From external appearances, the printer doesn't seem magical. It's designed by regular Americans - not just coastal elites. It needs an electrical motor, that's made in Germany. It needs ink that I get from Japan. The paper itself comes from Canada. The software is written in India. And the whole thing is assembled in China. Pretty much like any other printer I can buy from BestBuy. The exceptional thing about my printer is that I pay for all that it needs, and all that I need, with the same printed sheet that comes from my printer! That's some real magic right there.

Now you might say that such perpetual machines always turn out to be fake in the mechanical world. But this is different in a very subtle way. This printer is powered by a source that's endless. Well, it's lasted for approximately 250 years already! Sure, the printer has been abused by past generations and its power source has dimmed and flared, but so far it has all held up reasonably well.

Now you might say, "What's the catch? Why let the world on to this secret? Just let the good times roll."

Well, here's the problem, I don't feel good. People don't laugh at my jokes like they used to when my uncle cracked them, they don't ask my opinion on any movie they're making, I'm mocked and ignored all the time, ... I think that if I fix the printer, I'll restore myself to my uncle's past glory. 

Today, every time I print, I need to follow the user manual. This 4,543 word-long manual doesn't have simple steps like "insert paper in tray and hit the print button." Instead, it has complex instructions that amount to getting permission from regular people. Not directly - I talk to their reps, who're already in my pocket, but still... I have to write down exactly why I need to print, provide details of how the money will get spent, blah, blah, blah... All this due process really slows me down. Without that permission slip, somehow the magical green sheet stops working. It's as if the entire world needs to know that this pesky crowd, albeit 300 million strong, has endorsed this sheet. It's time to make changes, big changes.

First, I'm going to buy Bitcoin with the green sheets that still work. Since I don't understand money and a whole lot of people think Bitcoin is going up, it seems like a good way to diversify my holdings. Then, and this is my most genius idea, I'm going to stop swapping my magical green sheets for finished goods and material from countries around the world. I'll force them to buy goods that I make, because I make the best stuff. As for all those pesky rules about getting permission - I'm going to ignore them and let my friends take over the printer while I own it. Let's see how it goes. See you in 2028, or 2032, or never. Heh, heh.