How do “Hello World” programs affect the way I think about programing

Tzafrir Ben Ami
AppsFlyer Engineering
4 min readNov 6, 2019

--

I’ve recently watched this great talk by Scott Wlaschin on YouTube. One of the insights I took from the talk was a quote from Alan Perlis:

A language that doesn’t affect the way you think about programming, is not worth knowing”.

But it didn’t occur to me that writing a simple “Hello World” program will actually make me think about programing…

Whether you’re an experienced developer or just taking your first steps in the programing world, you are probably familiar with the iconic “Hello World” programs. When learning a new language, an “Hello World” program is commonly used to demonstrate basic syntax, and help you setup your development environment (the tour of Go tutorial, for example, starts with this “Hello, 世界” example).

AppsFlyer Engineering is always hiring, just like our product’s always-on, 24/7 SLAs. >> Learn More

In this post, which I’ve only recently discovered thanks to this great newsletter (both written in Hebrew), the writer decided to write an “Hello World” program in as many programing languages as she can. But to spice things up, a small twist was added —the program should print the “Hello World” string diagonally:

H
e
l
l
o

W
o
r
l
d

I can hear the wheels turning in your head: there is no need to be an experienced developer to write this program (and you’d be right, of course). What is there to think about?

The basic “out of the box” idea would be to iterate the “Hello World” string while holding some kind of temporary variable to help us print an incremental number of spaces before each character. On each iteration we will update our temporary variable value to create the diagonal.

And indeed (as expected), when I was reading through the different programs written in different languages, they all felt the same and they have all implemented, with some minor variations, this basic algorithm. I’ve never learned Python, yet I could easily follow the writer’s Python solution:

The Python solution form this repo, you can check other solutions written in different languages

The similarity between different solutions made we wonder about writing this program in Clojure (the primary backend language at AppsFlyer). If you are not familiar with this language, I will only state that Clojure is a functional dialect of Lisp. And one of the key concepts of functional programing is immutability, which makes the “out of the box” solution irrelevant since we cannot update variable values during our iterations…

(Note that you can follow the Clojure code bellow even if you are not familiar with Clojure. After all, this is the purpose of “Hello World” program… Just don’t be alarmed by the syntax and parentheses, despite the fact that it does look a bit awkward if you’re not used to it).

So without further ado, let’s look at my initial solution, which was basically just a portion of the Python code. True, due to immutability we cannot update the spaces counter value, but we can overcome it (well done, me) by converting the “Hello World” string into an indexed vector and use the index value to recreate the spaces string on each iteration.

Believe it or not, this function will print a diagonal “Hello World” string to screen (and if you don’t, check it online for yourself).

But this solution, despite working, didn’t feel right. It especially didn’t feel like a functional solution. To begin with, this function has side effect (TL;DR a side effect creates a change outside of the current code scope, like printing to screen). We cannot completely avoid side effects when the purpose of our program is to print to screen, but we should try to narrow and isolate these as much as possible.

So back to the drawing board, let’s rewrite the hello-world function to return a diagonal “Hello World” string instead of printing it (we still need to print the result of this function, but in itself this functions has no side effects).

Again, if you don’t believe me that it works just check it online for yourself…

But even after narrowing down the side effects, this function still feels like we can do better. But wait, one might ask, if the purpose of our program is to diagonally print “Hello World” and we have accomplished that, why are you feeling that its still not a “good enough” solution?

In my opinion, recreating the spaces string on each iteration results in a code that is not simple and readable. Our code is written for humans to read, and potentially change, which makes it super important to be “easy to follow”.

I wanted to find a way to increment the spaces string instead of recreate it. And surprise surprise, there is a way to do so with Clojure. Please welcome a recursive solution:

If you still don’t believe that it works on the third time, you can see for yourself.

Who thought that writing a trivial “Hello World” program (sure, with some twists!), would force me to think about my code. I could easily get away with using C#, Java, Go, Node.js or any other programing language I’ve used in the past, and I would probably end up writing code similar to the Python code we’ve seen… But to go back to the quote I’ve started this post with “a language that doesn’t affect the way you think about programming, is not worth knowing”.

If this example feels a bit artificial to you, you’re correct since it is. But, and I hope I was able to emphasize this, using Clojure will force you to think about the correct way, and even possibly the most elegant way (and not just the fastest way) to write your code.

--

--

Tzafrir Ben Ami
AppsFlyer Engineering

a "Full stack technology leader", trying to solve complex business problems with technology - mainly on the Web and large-scale systems (but not just)