Simple tricks to make your code more readable
Preface
Good day, colleagues! In this article, I share my approach to code formatting, and techniques that will allow you, first of all, to better perceive your own code and navigate it, as well as other people who will work with this code.
I am Andrey Rick, a fullstack developer with 10+ years of commercial development experience, developed many startups in various teams, and developed several startups alone, including my own. I will demonstrate code examples on React/Next, at the same time, this approach can be applied to any of the programming languages and frameworks based on them.
In the process of implementing the concepts I have proposed, you are highly likely to encounter the fact that the code will look unusual to you, and your internal conservatism will push you to write the code “the old way”.
However, believe me, if you can afford to go through the adaptation, the code that does not correspond to these design concepts will disgust you, because it will resemble a stuck together porridge that is difficult to perceive. In addition, in the article I do not just describe the techniques, but also tell why it is necessary.
We design the code: how and why?
Orderly, neat, well-written and easy-to-read code helps to detect random errors faster, and allows the eyes to “parse” code, that in general speeds up task execution time. Besides this, there is another non-obvious but significant advantage:
When you write code neatly right away, you manifest a certain attitude through yourself – to your work activity, to the process, to yourself. Order in the code = order in the soul (the same applies to the workplace). This approach begins to spill over into all other areas of your life, helping you practically in everything (this is really true).
This forms a certain way of thinking that can be developed in oneself, even if it is not there initially. From a state of order (in the head, in affairs, and ultimately in one's own code), work goes easier and faster, bright ideas come more often, and the general state and perception of the world significantly surpasses work from a state of chaos.
This only works if you consciously independently write your code neatly, and do not rely on prettier, which is supposed to “do it for you” but in some cases It hinders rather than helps.
Don't be afraid to spend extra effort on order and neatness in your code. Once you've learned to write neat code, you don't need to put in any extra effort – it happens by itself, you don't know how to do it any other way. And nothing will make you write code the same way again.
Nesting in code:
All levels of nesting in the code are separated by tabs and empty lines.
What are nesting and nesting levels?
Nesting:
When we have an opening bracket and a closing bracket, everything between them is considered nested in them. When we have an opening and closing tag – everything inside is nesting of this tag. When we write tag attributes inside the brackets of the tag itself – the attributes are also nesting within the brackets of the tag.
Nesting levels:
Both brackets and tags can themselves be inside brackets or tags, in which case they still open a new level of nesting. There is no limit to the number of nesting levels.
![](https://habrastorage.org/getpro/habr/upload_files/f0b/f29/722/f0bf29722fc7960b0343dc4a8d2be13e.png)
This applies to layout, functions, styles, JSON objects, and all code in general.
![An example of separating nesting with tabs and blank lines An example of separating nesting with tabs and blank lines](https://habrastorage.org/getpro/habr/upload_files/404/82b/b50/40482bb503c0f35df23d671368990a99.png)
Inside the code the same level of nesting We only need an empty line in two situations:
When next line with code opens a new one nesting level, or previous line with code closes new nesting level:
![Example when you need an empty line within the same nesting level Example when you need an empty line within the same nesting level](https://habrastorage.org/getpro/habr/upload_files/80a/d65/1c9/80ad651c961f7f28acb53ce34b4a85be.png)
![Example when you need an empty line within the same nesting level Example when you need an empty line within the same nesting level](https://habrastorage.org/getpro/habr/upload_files/fbe/9b8/daa/fbe9b8daad4e12871a2dc8993999ef4e.png)
The second situation when we can add an empty line is to separate logical code groups within the same nesting level:
![An example of separating logical groups of code with a blank line within the same nesting level An example of separating logical groups of code with a blank line within the same nesting level](https://habrastorage.org/getpro/habr/upload_files/4ac/dad/9ce/4acdad9cedf7bc152e9fdf5b085b7eb7.png)
In this situation, when required separate with blank lines inside one and the same nesting level, end.
That is the concept not in thatso that after each “non-empty” line there is an empty line. Like in the screenshot below, do NOT necessary:
![This is NOT how you should do it! The concept is not to do it after each "not empty" line was a blank line. If the concept is not clear at this point, reread the contents of the article. This is NOT how you should do it! The concept is not to do it after each "not empty" line was a blank line. If the concept is not clear at this point, reread the contents of the article.](https://habrastorage.org/getpro/habr/upload_files/d18/b43/45a/d18b4345ad127a07ab02fd04c0499a3c.png)
Now let's look at when we should create visual nesting, and when we can place nesting on one line.
We can write nesting in one line if its contents take up little space and contain no more than 2, figuratively speaking, items:
![for such a case there is no need to move this nesting to a new line and separate it with empty lines for such a case there is no need to move this nesting to a new line and separate it with empty lines](https://habrastorage.org/getpro/habr/upload_files/b1d/134/61e/b1d13461ec559b7b05d0db1a4d03db92.png)
![Here, too, there is no need to move these nestings to new lines and separate them with empty lines - since the contents of the nestings do not take up much space and contain no more than 2 items. Here, too, there is no need to move these nestings to new lines and separate them with empty lines - since the contents of the nestings do not take up much space and contain no more than 2 items.](https://habrastorage.org/getpro/habr/upload_files/de1/684/49f/de168449fa5c39352761499b9452be1f.png)
However, when we have many elements in nesting, It will be much clearer if you place each element on a new line.
In what situations might this be relevant:
![When a component is passed many (more than 1-2) props (or HTML/JSX tag attributes), for clarity, you should not write them in one line! When a component is passed many (more than 1-2) props (or HTML/JSX tag attributes), for clarity, you should not write them in one line!](https://habrastorage.org/getpro/habr/upload_files/38b/344/79d/38b34479d16fd1e2052299f4ac9a1acd.png)
![When we have a very long className, it should not be written in one line, moreover, it can be broken down into logical groups/conditions, each on a new line. See for yourself how much clearer the code below is than the code above (the code above does not fit into the visibility field completely) When we have a very long className, it should not be written in one line, moreover, it can be broken down into logical groups/conditions, each on a new line. See for yourself how much clearer the code below is than the code above (the code above does not fit into the visibility field completely)](https://habrastorage.org/getpro/habr/upload_files/4fb/1ce/d00/4fb1ced002a4295e778d860fd43825db.png)
![When we destructure many properties from an object When we destructure many properties from an object](https://habrastorage.org/getpro/habr/upload_files/376/838/508/37683850803a2a6f01302b674dd313c2.png)
I have conveyed the main essence of the design of nesting, below I will give several examples of how, from my point of view, it is best to design the code of frequently encountered techniques:
Variations of the classic If else
![The optimal option takes up a minimum of space, while at the same time providing visibility of nesting The optimal option takes up a minimum of space, while at the same time providing visibility of nesting](https://habrastorage.org/getpro/habr/upload_files/36a/289/8f0/36a2898f0aa559d599917d3d3c8577d5.png)
If the conditions require one thing to be done, and it doesn't take up much space, you can write it like this:
![](https://habrastorage.org/getpro/habr/upload_files/410/b1b/562/410b1b56226c3e70a1927141c5e8bc10.png)
If you need, for example, to interrupt the execution of a function if some condition is not met, and the main logic should be executed if the condition is met, instead of creating unnecessary nesting with the if else construct, you can handle the failure of each condition with just one if, and continue writing the main logic at the same nesting level. It is easier to understand with an example – just compare these two approaches:
![The code on the right looks too voluminous and complicated, it distracts from the essence, and such code takes longer to perceive. The code on the left implements exactly the same thing, but looks concise and understandable. The code on the right looks too voluminous and complicated, it distracts from the essence, and such code takes longer to perceive. The code on the left implements exactly the same thing, but looks concise and understandable.](https://habrastorage.org/getpro/habr/upload_files/e78/2fc/89a/e782fc89a52765a65e9829d14a48acd4.png)
If a large number of conditions are checked in if, it makes sense to visually separate them, for example, like this:
![Visual complex logic Visual complex logic](https://habrastorage.org/getpro/habr/upload_files/5e7/e9a/1a9/5e7e9a1a948ef366437e1df4df15245a.png)
Thenar “if && then” and “if ? then : else then” conditions in JSX output
![This type of writing of the component output by condition gives both a clear understanding of nesting and takes up a minimum of space This type of writing of the component output by condition gives both a clear understanding of nesting and takes up a minimum of space](https://habrastorage.org/getpro/habr/upload_files/752/564/97a/75256497a90365317e34fb7e7dcd8ebd.png)
![This format of writing gives a clear understanding of where each condition begins and ends, where this or that result of the condition begins and ends This format of writing gives a clear understanding of where each condition begins and ends, where this or that result of the condition begins and ends](https://habrastorage.org/getpro/habr/upload_files/1fe/549/3af/1fe5493afe4331dcb5c73cd0e66cb44a.png)
It makes sense to write part of the condition in one line when either the entire expression is short, or we are dealing with a large main part that is output by the condition, and if the condition (true/false) should be output some small, insignificant part, and in this case we write the small part on top of the expression:
![](https://habrastorage.org/getpro/habr/upload_files/99f/7d2/5c7/99f7d25c7914183c44057102d954a10e.png)
Example of JSX output via map, and examples of useEffect
![Outputting JSX via map Outputting JSX via map](https://habrastorage.org/getpro/habr/upload_files/a21/480/463/a21480463aef6a929d8935a04fbb9560.png)
![useEffect example with deep nesting useEffect example with deep nesting](https://habrastorage.org/getpro/habr/upload_files/261/528/c23/261528c2353d062dad19fe0da8299df3.png)
![useEffect example with short nesting useEffect example with short nesting](https://habrastorage.org/getpro/habr/upload_files/2b3/a97/de9/2b3a97de97e63527f3f6485b79231dfe.png)
Separate functions, components, styles into different files
Don't write styles in the same file where the component code is located (relevant for styled-components). Thanks to this, when you explore the code and fall into functions/components (by clicking on their name with ctrl or ⌘ pressed), you will not lose focus from the current component, jumping to another part of the file, but instead another file will open, which you can drag into a separate section of the code editor, and work with both files simultaneously. Don't write several components in one file for the same reason.
Moreover, do not create components inside the code of other components – this not only increases the volume of code (and the complexity of its perception) inside one file, but also from the point of view of optimization is “not ok”.
If your page consists of several parts that can be separated into independent components – do this, do not write the entire “layout” in one file. Then call these components – and you will see how the code has become much easier to understand. Just compare:
![Here we just stuffed the entire layout into one file. So what? It works anyway! (don't do that) Here we just stuffed the entire layout into one file. So what? It works anyway! (don't do that)](https://habrastorage.org/getpro/habr/upload_files/8d5/68d/631/8d568d63193d3d4ad9f5d3b5c2b150fc.png)
![And here we have divided the parts of the page into components, and looking at this code we immediately understand what is where. We will easily be able to find and change the necessary section if necessary. And here we have divided the parts of the page into components, and looking at this code we immediately understand what is where. We will easily be able to find and change the necessary section if necessary.](https://habrastorage.org/getpro/habr/upload_files/a5b/247/60c/a5b24760ceb2b9de46ee914137915cb4.png)
Try to make components and functions universal and reusable. Think about how to cover and implement most of the project's functionality with less code, and when.
Do not duplicate code under any circumstances – create simpler functions/components, and expand them with functions/components that are based on the functionality of simpler ones (i.e. they use imported simpler functions/components, and logic is built on them, and this set in turn is a separate function/component). This is also a fairly broad topic, and to a certain extent is an art. If you are interested and relevant, you can write about it in the comments, maybe I will somehow upload a separate article on this topic.
Each file with a component of a certain type, function, or something else, should be in its own folder. That is, you should (for yourself) create such a file structure to clearly understand that if you need some component, where to look for it. This is a very broad topic for a separate article, for this reason I do not specifically cover it here, just take it as an idea.
But for React/Next I have already written an article about it, so anyone who develops on this stack can see in practical examples what exactly I mean, and how I personally approach organizing a clear file structure in the projects I work on.
Clean your code from “garbage”
Remove unused imports, variables, functions, extra blank lines (when there are two or more blank lines instead of one), blank lines at the bottom of the file, comments that are no longer useful to you, and commented-out backups of code that you will never need (we are talking specifically about cases in which this code really will not be useful).
Don't use “magic numbers”
It is better to create an “extra” variable and name it according to the meaning of the value contained in it. Just compare:
![What is that one on the left?? But on the right, everything is immediately clear, even when you first look at the code! What is that one on the left?? But on the right, everything is immediately clear, even when you first look at the code!](https://habrastorage.org/getpro/habr/upload_files/b89/444/6a1/b894446a117a683189a2776602842970.png)
![Above - "magic numbers"which don't say anything. From below - everything is immediately clear, and by falling into the MATERIAL_TYPES constant you can see what values there are and what they refer to. Above - "magic numbers"which don't say anything. From below - everything is immediately clear, and by falling into the MATERIAL_TYPES constant you can see what values there are and what they refer to.](https://habrastorage.org/getpro/habr/upload_files/146/03a/7fd/14603a7fdccad745e9e6bfbc92628855.png)
Good luck in developing your projects!
Let your code be clear and understandable for you and your colleagues, and let there be order everywhere – from your code, project structure, workplace, and ending with your head and all your affairs!
Even if it's not like that now, or not like that everywhere.. Take a step towards positive change. Start with code.
All of the above is my personal subjective opinion and vision of the situation. It does not necessarily have to coincide with your opinion and vision.
I have described perhaps the most important, from my point of view, aspects of code design and ensuring its clarity. Using this approach significantly affects my perception of the code, the speed of work, the feeling of harmony and pleasure from the development process. If the article is useful for you – I am very glad.
I will add to the article if I remember any more examples. In general, there is still something to share both on this topic and in general. See you in the next articles!