What is a Beautiful Code?

ZebraX Engineering
ZebraX
Published in
4 min readApr 7, 2022

What do you think is a beautiful code? Is the code neatly organized? Is the code colorful and easy to read? There will be many opinions about this.

The traditional idea of beauty is based on the physical appearance of things. To be beautiful is to be visually appealing. However, when it comes to code, the word “Beautiful” is more complex than you would think. Beautiful code has to be easily comprehensible and also syntactically correct.

So basically, beautiful code is a code that can make everyone understand what this code is for, even when the code is read again after years and read by other people. This is something that is very helpful for the smooth distribution of this code understanding.

I’ve had a really good experience seeing code samples like this. This happened the first time I did my internship at ZebraX. I’m able to understand the code faster than I expected. I can understand every logic quickly with just every component in it.

There are a lot of things that contribute to what makes a code beautiful.
Here are some criteria for beautiful code

Simple
Every code needs a reason to exist, otherwise, it has to be refactored or deleted and every piece of code must have an unambiguous purpose. In other words, simply reducing the code while maintaining functionality

Simply written
Simplifying complexity makes the code easier to understand.

Example for Simply Written

Self-documenting
Beautiful code needs relatively few comments because it’s so clear that you can read it like a book

Example for Documenting

Modular
Modularization allows you to reuse parts of your code, to achieve it you need to consolidate a few things which are:

  1. DRY (Don’t Repeat Yourself)
    A principle aimed at reducing repetition of software patterns, it’s mandatory to generalize and avoid repetition to avoid redundancy.
  2. YAGNI (You Aren’t Gonna Need It)
    Minimize the number of entities like functions, classes, modules, etc. A programmer should not add functionality until deemed necessary. If you have a lot of nearly identical functions, the best practice is to refactor or combine them.
  3. SDOT (Should Do One Thing)
    A function will be difficult to generalize and reuse if it’s doing multiple things
  4. Try to use fewer arguments per function
    In many cases, it’s more effective to use fewer arguments. The reason is not only it’s easier to read and understand but also makes the code more efficient to work with.

Error Handling
Handles errors consistently, clearly, correctly and in a way that aids debugging

Example for Error Handling

Does beautiful code impact your quality as a developer?

Your quality as a developer is determined by how robust, effective, efficient, and easily comprehensible your code is. Writing beautiful code would also reduce or even avoid technical debt. Beautiful code will be much easier to maintain and read which can affect your project in the long run.

Conclusion

Beautiful code is one of the important things to implement because it’s very helpful to deliver understanding between developers in a project. It makes the code more efficient and reusable. Maybe it looks like a simple thing but in reality, beautiful code is very useful to practice.

Author:

  • Aryasatya Bhagaskara (Software Engineer)
  • Randa Alfarisyi (Software Engineer)

Source:
https://medium.com/@fahdmekky/writing-clean-and-modular-code-eb1eb8bba87f
Beautiful Code Principles | by Pavle Pesic | Medium
What makes code beautiful and why it matters (tabnine.com)

--

--