[RUBY] Basic rules to be aware of to write easy-to-read code

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.

Why you need to write easy-to-read code

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.

Points to note when actually writing 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)

Variable and function naming

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.

Variable name example

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.

Function name example

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.

Logic simplification

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

When there is a description of nesting in nesting

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.

When the conditional expression is complicated

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.

Communicate accurate information in comments

You can leave a comment in the code by commenting it out. You can use this comment to make the description easier to understand.

Scenes where you should leave a comment

・ If you write complicated logic, leave an outline of the process and your thoughts in the comments. ・ Leave why such a description is made

Scenes where you should not leave comments

・ 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

Basic rules to be aware of to write easy-to-read code
To be aware of easy-to-read code
Things to be aware of when writing code in Java
Things to be aware of when writing Java
How to write test code with Basic authentication
Things to be aware of when using devise's lockable
[Java] Things to be aware of when outputting FizzBuzz
I want to be aware of the contents of variables!
How to write good code
[Java Silver] Things to be aware of regarding switch statements
Java 14 new features that could be used to write code
A collection of patterns that you want to be aware of so as not to complicate the code
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
Basic structure of Java source code
[Basic] How to write a Dockerfile Self-learning ②
Summary of how to write annotation arguments
Basic usage of enums and code examples
Write code that is difficult to test
5 things new programmers should be aware of
[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]
Summarize the life cycle of Java objects to be aware of in Android development
JDBC promises and examples of how to write
[Ruby] Basic key to be strong in refactoring
Write code that is easy to maintain (Part 1)
I tried to chew C # (basic of encapsulation)
[Ruby] Code to display the day of the week
[Java] Be aware of short circuits (short-circuit evaluation)
How to write code that thinks object-oriented Ruby
Write code that is easy to maintain (Part 4)
Java Servlet should be aware of multithreaded environment
Write code that is easy to maintain (Part 3)