Readability counts!

Last updated on

Many times I code and either read code written by others that is painfully hard to understand at glance, or I write things that are … unpronounceable, to say the least. Let me get this straight:

Readability counts

Yes, I know, you already read the title and the first paragraph. Why? because code is read more often than it’s written. But bear with me, let me give you some examples, to what extent this is important and it will impact the quality of your code, your job as well as your other workmates who code with you:

Let’s say you need to do some work on a list, in python. You can certainly do a list comprehension:

fish_tuple = ('blowfish', 'swordfish', 'catfish', 'octopus')

fish_list = [fish for fish in fish_tuple if fish != 'octopus']

print(fish_list)

Now, did you understood fast and clearly what I intend to do? Ok, lets rephrase it into this:

fish_tuple = ('blowfish', 'swordfish', 'catfish', 'octopus')

for fish in fish_tuple:
    if fish != 'octopus':
        fish_list.append(fish)

print(fish_list)

Do you agree that this is more pythonic? What about some beginner or mid-level python programmer? Do you think that it’s clear enough? You see, I now I jumped from 1 line to 3 lines. Again, think in readability vs shortened the code: is it useful to flourish the code using features just because you are able to?.

I believe code should be always readable, and maintain the door open to everyone who is starting to read code (and possibly writing it too).

Let’s take another example in javascript, a component is it clear this way:

export default function MyButtonComponent() {
  return <div>Component text</div>;
}

or better this way?

const MyButtonComponent = () => {
  return <div>Component text</div>;
};

export default MyButtonComponent;

This second is a more a nuisance than a clear sample, so give me a minute to explain: What you want to do here is essentially a function. On the second snippet, it’s an arrow function, not a basic function, so it’s actually not the same thing, but are you really going to use the const MyButtonComponent anywhere else? Do you see the usefulness of announcing functions as functions, and variables (as in space in memory values) as const vars? It makes the code clean, easy to understand, practical and way better to check with common sense.

So, please, the next time you need to do a list comprehension on python, or any other flourish function in any other language, re-check if it is absolutely needed

this post comes from github, view it here