DEV Community

Rafi Panoyan
Rafi Panoyan

Posted on

Code is beautiful

Today's post will be a bit different. First of all because, at the end of the article, we will have the opportunity to share some code. Then because we won't talk about technical details of a specific programming language, instead we will take a step back and look at the shape of what we write everyday.

Why do we write code ?

We often look at languages as tools that enable us to build software, and at the end of the day, it's exactly what they are. We solve real-world problems with software. Sometimes people's lifes are at stake. Programming is a serious task.

Our code must be robust, maintanable, concise, performant. To achieve that, we define clean code guidelines, clean architectures, development methodology like TDD, CI platform to control the entire pipelines, etc...

Programming is a serious task

But there is more to that.

Take a breath

Every day, in a file, characters become keywords at the tips of our fingers.

We associate these keywords in a very specific way, particular to each programming language, to define the behavior of our software.

And we look at these files, we look at these functions, properties, classes throughout the day.

Of course we do that to understand what real-life rules they are trying to express, we look at them because we might want to refact these pieces of code, or we want to test them. But we definitely look at them, A LOT.

I love your shapes

Consequently, the looks of our code matters. And the appareance of a piece of code is ruled by the programming language we are using.

This is one of the reason I really like learning new programming languages, they don't look the same as the one I'm used to write every day. Different keywords, syntax, special characters... Different shapes.

I will now show you some snippets of a very basic function with different languages, just to admire the shape of the syntax.

The code

The code you will read is certainly not optimized, we could surely write these functions with less lines, with more advanced concepts, but we don't care here.

FizzBuzz is a trivial function that takes an integer x and returns either "fizzbuzz" if it's divisible by 15, "fizz" if it is by 5, "buzz" if it is by 3 and x otherwise.


fun fizzbuzz(x: Int): String =
    when {
        x % 15 == 0 -> "fizzbuzz"
        x % 5 == 0 -> "fizz"
        x % 3 == 0 -> "buzz"
        else -> "$x"

The first language is the one I use the most as an Android Developer. Kotlin introduced a new syntax to develop on the JVM and it has been some fresh air from Java !

Clean, readable


fizzbuzz :: Int -> String
fizzbuzz x
    | x `mod` 15 == 0   = "fizzbuzz"
    | x `mod` 5 == 0    = "fizz"
    | x `mod` 3 == 0    = "buzz"
    | otherwise         = x

This purely functional language is very comparable to pure mathematic in its syntax. I really love this facet, you can tell it by the way the signature of the function is declared.

Pure, rational


(defn fizzbuzz [x]
    (let [d { 
        :by15 (= 0 (mod x 15))
        :by5 (= 0 (mod x 5)) 
        :by3 (= 0 (mod x 3))}]
        (:by15 d) "fizzbuzz"
        (:by5 d) "fizz"
        (:by3 d) "buzz"
        :else x )))

Controversial choice here but... what a beauty ! A LISP language, where absolutely everything is a list.

(element1 element2 element3 ...)

That's it. Each and every piece of code you write is exactly this, a list. Either you declare or call a function, compare two values, create a conditional branch... Consistency.


Now what ?

You might not have learnt anything new reading this post, but I really hope you stopped for a moment to think about the shape of what you write everyday.

That being said, I'm sure that caring about the "physical" beauty of our code can help us write it better overall.
More concise, readable, enjoyable to read... you certainly don't want this 30+ lines' method in your class !

Our job can seem like art sometimes, and syntax is just one the many facet of it. Architecture, algorithm, even project structures, are aspects we should look at with art in mind.

Share !

I would really appreciate if you could share a piece of art with your own FizzBuzz function, written in whatever language you find good-looking !

If you are curious about the languages above, here are some links :

Top comments (2)

ahmadhassoun profile image
Ahmad hassoun • Edited

Nice and fresh article

I found Clojure syntax little bit complicated and not simple. So I don't think we can call it beautiful, but as we say beauty is relative

rafipanoyan profile image
Rafi Panoyan

It's complicated at first, but become natural the more you write it :)