Readable code

When you write code, it needs to be as close to natural language as possible.

You can do complex staff, but if it isn't readable put it in a function with a name that explains what it does.

For example let's look on the following code:

build_devices.py

No alt text provided for this image


Will it work? sure, but when someone tries to read it, it is really hard to understand what we are trying to do.

What is is this replace? why is it there? why is the full_name isn't enough?

Now let's look on the following code:

build_devices_refactor_1.py

No alt text provided for this image

Now we got clear intention, the hostname of the device is the fullname without the suffix, one line function made our code more readable, although the function is one line, when someone read out build_devices function he can understand, and if he wants to understand how we remove suffix, he can read our remove_suffix_from_device_name

Write the code you want to read, not the code you want to write.



I like the “Write the code you want to read, not the code you want to write” :)

To view or add a comment, sign in

More articles by Omer Shtivi

  • שמאל וימין בעבודה

    פוסט על עבודה על אף שהוא ישמע פוליטי. (ואל תשליכו על דעתי הפוליטית עקב פוסט זה) ישנו ציטוט המיוחס לצ׳רציל (לצערי אני לא…

    3 Comments
  • Python Classmethod

    I'm a big fun of Python Classmethod, I'm not going to explain what is it I think it is well covered here. Let's start…

Explore content categories