Next Level Programming: Clean Code Tips

Ahmed Selmi
7 min readSep 26, 2020

A very large group of differences that distinguish the programmer who has just entered the job market and the professional programmer behind which the big companies run, one of these differences is the clean code. Do not dream of a job that suddenly knocks on your door if you are known for your mysterious codes, we all write software codes and all of them work efficiently Perhaps, but if you want to work for large companies and write more professionally, you should consider writing clean code.
Regular code and clean code:
We know that poorly organized regular codes work, just as well as professionally written codes, and the poor way regular codes will not affect a single web page negatively. But if we want to talk about a website with thousands and millions of visitors, then here we must think a little and drink A cup of tea before coding. Bad code literally means losing thousands of visitors and venturing into buying and selling traffic, revenue, and even the development of the website and the company as a whole.

That is why large companies do not accept dealing with beginners, because they know that the time delay in opening the web page, not for a few seconds but for fractions of a second, means losing a customer, and losing one customer means losing thousands of potential customers according to the hierarchy, and this means a relative loss in annual profits Reaching thousands of dollars, not to mention going down the list of competition between similar sites.

In addition, to be more realistic, companies will not hire you until you start the project on your own, especially if you are taking your first steps in the software industry, but rather you will work on a huge project, perhaps if you are lucky with a large number of developers in an organized team that shares tasks, so you have to write software code that is understandable to everyone without The need to provide clarifications and explanations to a colleague, except in rare cases.

And not only that, where companies think much further than this, it is possible that you leave a task in the middle of it and suddenly take over it, and it is possible that you take over the task of another person at work, or you leave work in that company without returning, and here we understand from this The scenario is that vague and vague codes are completely rejected in the work environment so that the rest of the developers can work on developing the project and continue the work smoothly without the need for hours of research and work to decode the code and know the meaning that was in the poet’s heart, so to speak.

It is not just a matter of clarification, but also of the way of writing, the use of the latest methods in the programming language that you are working on, or at least the agreement between co-workers on the code that corresponds to the field of work, you cannot, for example, write a 10-line code and you can write it With 5 or even 8, using newer or better instructions, it must be noted that clean code does not depend on the rules of the programming language nor its type, but rather on what the community of developers for that language has gathered over time.

A clean code is not limited to form, not only on clarity and ease of reading, but goes beyond that further. Is it reasonable to write a code that needs 3 requests with the server, while you can write it in a way that can perform the same task with one request?
The topic may not be so easy, you may need to stay up for long nights to reach an idea that allows you to reduce the number of requests from the server, and you may need weeks to write a general function function through which you can shorten the work of dozens of other functions.

You are here not working alone on a web page or two pages or a desktop program for managing and a simple store, nor on a personal or simple website to manage some local sales. A specific code, and as we know the field of programming is developing very quickly, and we must keep abreast of the latest developments, otherwise we will remain in the ranks of amateurs.

And as long as you have continued reading to here, you are definitely one of the people of programming, and it is time to stop writing and organizing your codes professionally.

General rules for writing clean code:
We can list some general characteristics of clean codes and summarize them with the following points:

1 / The number of variables is the least possible
No two people disagree on the necessity of reducing the number of variables, functions, classes … etc, to reach easier code and simpler ideas.
2 / Non-repetition
This is something everyone knows, we have to cancel all the scattered lines that perform the same task, and try to combine them in one line or in one function that we call when necessary.
3 / Passing all tests
There are known test-driven development (TDD) environments for testing code, which are regular code that checks the code and the goal is to set general standards for the project.
4 / Clarity of purpose and simplicity
The task of each function must be clear, and the tasks should not diverge in it to perform many ideas. If a function has a counter function, for example, other tasks should not be included in it even if things go without errors and this applies to the code as a whole.
5 / Preserving the context
If you wrote a function using one method, and you had to write a similar function but for another purpose, use the same method you used for the first function.
How do we write clean code?
Now after this long introduction, how do you write clean code? If you want to write clean code, follow these steps:

1 Make the names meaningful
The names of the variables, functions, and everything in the code should refer to its work, so the variable that expresses a car must be called car, not x, and if you want to create a function that does the count, then call it count and not c, for example.

2 / Do not use abbreviations
As we said about the need to use expressive nouns, of course you should not use abbreviations, the code must be readable to everyone, so a variable named name should not be abbreviated to nm, and a variable called lastName should not be abbreviated to ln.
3 / Choose simple names
This is self-evident, except that you may feel confused sometimes when naming some functions, and you will get rid of this confusion if you follow the one-task rule for the function that we will mention later. Do not use your language skills to write names, as the function of the delete function can be called simply delete, not kill .
4 / Naming method
Choose the verb form to name the functions, that is, instead of taxCalculator, call it calculateTax. As for classes, use the adjective or noun with it instead of the verb.
Also, do not forget the well-known rules of using camelCase in compound naming, in which the first word begins with a lowercase letter and the second word with a capital letter, for ease of reading.
For functions and variables in general, they should start with a lowercase letter, and classes must start with a capital letter.
5 / Comments
Although I love writing comments, the rules of clean code say that it must explain itself by itself and it does not need comments at all! On the contrary, if the code was understandable, it would not need any comments in the first place! Of course, except for comments about copyright and use, or the program as a whole.
6 / Functions
Try to describe the functions with the following characteristics:
Small size. Try to keep the function no more than 10, 15, or 20 lines. These are examples you should keep it as short as possible.
With a name that expresses its mission.
The number of arguments is minimal. Three at most, otherwise you should consider writing it in class.
It has one specific task. Try to have the function one task so friends can understand what’s going on.
7 / Keep the lines as short as possible
Don’t let the reader scroll horizontally to read a long line.
In fact, the topic goes on and on, and the previous steps remain part of a fair amount of general rules for writing clean code. Perhaps you will learn them practically from the community of the programming language in which you work, but as a start, you can use the book Clean Code: A Handbook of agile software craftsmanship, which is considered one of The most prominent books in this field.

--

--