Premise
I just started programming (1~This is a summary of what I learned in 2 months).
There may be things that don't work or are wrong in the actual field,
If you notice it, please let us know in the comments.
I'm a programming beginner,
Need to write easy-to-read code
How can I write easy-to-understand code?
I summarized what I learned about.
There are three main reasons to write easy-to-read code. ・ Increased productivity of individuals and organizations ・ Maintainability is improved ・ A flexible development system can be built Since development by programming is basically done by multiple people, productivity will reach a plateau if you only need to know yourself, and you will be far from a flexible development system.
** Refactoring ** is being done to achieve these three. What is refactoring? It is to improve the source code to make it easier to read without affecting the implemented functions. This refactoring makes it easier for co-developers to understand and modify the implemented code.
Variables are named in consideration of others (variable and function naming) Easy to read without complicated logic (simplification of logic) Comments are included and you can read the code while grasping the whole picture (accurate and straightforward comments)
When naming variables and functions, avoid abstract names and give them concrete names. For variable names Define the variable so that you can see what role it has. For function name Define what to do in the form of verb + noun so that you can understand what to do.
bad example
const one = 1;
Good example
const userId = 1;
In a bad example, the name of the variable one does not tell us what the value 1 is and what role it has. In a good example, you can see at a glance that the variable is userId and the value 1 is the user ID.
bad example
const add = () => {
processing
};
Good example
const addTask = () => {
processing
}
In a bad example, I know I'm adding something, but I don't know what to add. In a good example, Task is attached, so you can understand at a glance that it is a process to add Task.
There are two main factors that make the code more complicated. When there is a description of nesting in nesting When the conditional expression is complicated
What is a nest? Nesting is a nested structure. If you compare it with a conditional expression, you might say that the nesting is deep when you write if in if, and if in that if. It means that it contains a lot of nested structures.
As you can see from this example, the more structures there are in one structure, the more complicated the structure becomes. This means that it is easier to read the code if you write as few nests as possible during nesting. Specific countermeasures -Return the return value at an early stage and subdivide the process itself. -Use logical operators.
Even if the conditional expression is long, it will be difficult to understand because you have to read it from the beginning to the end to understand the contents of the process.
If the description of the conditional expression becomes long, you can make the code more concise and easy to understand by writing the conditional expression as a function and calling the description.
Even so, it is complicated because there are multiple conditional expressions in the description. In that case, the description can be simplified by decomposing multiple conditional expressions and dividing them into multiple ifs.
You can leave a comment in the code by commenting it out. You can use this comment to make the description easier to understand.
・ If you write complicated logic, leave an outline of the process and your thoughts in the comments. ・ Leave why such a description is made
・ Supplementary comment -"Auxiliary comments" that supplement code that is too complicated to process Comments are left as annotations in the first place, so reading a lot of those comments is not a concise code. On top of that, the process may be too complicated to cover with a single comment. In these cases, it is important to look at modifying complex source code rather than relying on comments.
These are the basic rules that I should be aware of when writing easy-to-understand code that I learned as a programming beginner. If you have any additions or corrections, please leave a comment.
Recommended Posts