Jonathan Boccara's blog

Coding Styles With Exotic Constraints

Published February 25, 2020 - 0 Comments

This article is NWH, standing for Not Written Here. The concept of NWH is inspired from the NIH (Not Invented Here) syndrome which consists in refraining from using existing code from outside the company and reinventing the wheel every time. Just like it is good practice to look out for solutions developed elsewhere, we’re going to look at an article written elsewhere. Or said differently, an article that is NWH.

The NWH of today is a blog post called Alternative code styles, on Samuel Walladge’s blog.

In that post, Samuel explores various exotic coding styles, that aim at writing “beautiful” code. Check it out, it’s fun and easy to read.

Is beautiful code more expressive than code with a default layout, all other things being equal? You can answer this in the comments section, if you happen to have an opinion on it.

The benefits of extreme constraints

One of the coding styles that Samuel mentions, the Fibonacci indentation, can have some benefits in terms of code quality, on top on the pure visual aspect of the code.

Indeed, Fibonacci indentation makes deep nesting difficult, for the simple reason that you won’t be able to see the code soon enough, when it gets out of the screen (or worse, if the line gets wrapped).

This would discourage a person from adding an extra level of nesting, in code that is already nested.

We’ve seen that nesting is not always a bad thing, but deep nesting is better to avoid, because it tends to make code hard to reason about.

Fibonacci indentation my code is an extreme constraint, that can be interesting in a code experiment.

This is not the only type of extreme constraint that you can try. For example, there is a classic experiment that consists in coding without using the keyword if (and without resorting to macros to hide it!).

It seems like an impossible thing at first, but conducting this experiment allows to discover other coding practices, that you can use in production code. Indeed, the point of the experiment is not to stop using if in your production code, but rather to have alternative solutions in your programmer’s tool belt.

Another one is programming without raw for loops. This is a great way to practice using STL algorithms.

Beautiful code, for the beauty of it

Apart from best practices, some of the coding styles that Samuel Walladge shows are purely aesthetic. If you’d like to see more of this, check out code-poetry.com (which he mentions), and the incredible Piet programming language.

Don't want to miss out ? Follow:   twitterlinkedinrss
Share this post!Facebooktwitterlinkedin