[RUBY] Considering code naming from a liberal arts perspective

Introduction

I wrote it as an experience summary of the code convention when writing in Ruby.

Easy-to-read code definition

You must write your code with the goal of being able to finish reading the code in the shortest possible time so that the reader can make changes and find bugs.

What you can understand when you translate what variables, methods, and classes control

As a superficial improvement, consider the ease of understanding what can be written arbitrarily

Variables, methods, classes, and the parts of these codes that can be written and defined almost arbitrarily are not written carefully, or it is extremely difficult to clear the above-mentioned code definitions. To be honest, even if I wrote it intentionally, it can be misleading. It's annoying because it seems like I'm not thinking about anything unless I eliminate the possibility of misreading as much as possible.

Use clear words

Use simple verbs, nouns, adjectives, and prepositions. When using English words, it is not necessary to write according to the English grammar of ticking, but rather it may cause confusion, so it is possible to judge the situation so that it is convincing as a rule within the product or team You will need it. For example, of. There is also a way to translate A of B into B of A instead of A alone of B. Even when it doesn't matter which one you use, if many people are familiar with A in B, you will be warned.

Colloquial expressions often ignore grammar in the sense of emphasis, and it is a fact that even foreigners do not express with such care, but in coding, write overseas contracts. It seems that you need to be as smart as writing an official document in that area.

Perhaps if there is a word that is famous for being used as a noun or can be used as an adjective, it will be a warning target if it is used. For example, product cost. In short, it relies on a great deal of experience, familiar or unfamiliar.

Past participles are also confused with past verbs and should be avoided. Troublesome participles in reading comprehension also need to be judged context-sensitively and should be avoided as expressions. It seems better not to use grammars that require contextual judgment. Relative pronouns are grammars that are lengthy due to the nature of this syntax, so they will not be used. However, although there are many ways to judge when each grammar overlaps, it seems necessary to avoid all expressions that are left to the context in coding that requires expressions that require information to be inserted as short as possible.

There are not many grammars to be aware of

――When using verbs, you need to be aware of the five sentence patterns, so be aware of the proper use of intransitive verbs and transitive verbs. --Plurale and singular

So far, that's all I care about.

No need for self-explanatory words

For example, the variable name registered user can be user. This is because it is clear that they are registered when they enter the DB.

Avoid generic names and give specific names

Creating variables with a single noun can be misleading to the reader as the code grows. For example, if there are variables that have only data here and there, it will be difficult to read.

Avoid abbreviations

This is convincing. I don't know if get_user_data is gud or something like that ...

Method uses verb

It's the code that does the processing, so it's best to use verbs.

Use nouns for class names and variables

Be aware of what the class controls and what the variables are stored in

Do not use paraphrases

The code is not a sentence, so it is better not to paraphrase it. I tend to do it if I do it as if I were writing a sentence. Do not use things that have almost the same meaning, such as Memmber = user, but unify things that have the same meaning with the same word.

Deprecated notation

A nomenclature called Hungarian notation that puts information such as types and scopes into variable names and class names is currently deprecated and seems to be an anti-pattern in many cases. For example, if it is an array, you should avoid making it a variable such as arr_data.

Recommended Posts

Considering code naming from a liberal arts perspective
Re-study Docker from a system operation perspective