[Ruby] Consider code naming from a literary perspective

3 minute read


It is written as a summary of the experience of code rules when writing in Ruby.

Easy-to-read code definition

Must write code with the goal of being able to finish reading it as quickly as possible so that the reader can make changes and find bugs

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

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

▽ It is extremely difficult to clear the above code definition unless you carefully consider and write variables, methods, classes, and parts of these codes that can be written and defined almost arbitrarily. To be honest, even if this was written intentionally, it can be misleading. It’s frustrating because it seems like we’re not thinking about anything unless we eliminate the possibility of misreading.

Use clear words

Use simple verbs, nouns, adjectives, and prepositions. When using English words, it is not necessary to write in accordance with Kachikochi’s English grammar, and it may cause confusion, so there is a situation judgment that is convincing as a product or a rule in the team. You will need it. For example, of. The translation of A of B is not B’s A alone, but there is also a usage of A’s B. Even if there is no problem in using either, if there are many people familiar with A of B, you will be warned.

Colloquial expressions often ignore grammar for the purpose of emphasizing, and it is a fact that even overseas people do not care so much about expression, but in coding, writing overseas contracts It seems that it is necessary to be as sharp as writing an official document about that.

Maybe there is a word that is famous as a noun or that can be used as an adjective, but if you use it, it will be a warning target. For example, product cost. In essence, it relies on the tremendous empirical thing, familiar or unfamiliar.

Because past participles are confused with past tense verbs, you should avoid them. Conflicting participles in text comprehension also need to be determined depending on the context, so it should be avoided as an expression. It is better not to use a grammar that requires contextual judgment. Due to the nature of this syntax, relational pronouns and the like are grammars that are extremely long, so they will not be used. However, there are many ways to judge when each grammar overlaps, but it seems that in coding that requires expressions that require information to be inserted as shortly as possible, it is necessary to avoid any expressions that depend on the context.

There is not much grammar to be aware of

  • When you use verbs, you need to be aware of 5 sentence patterns, so be careful about the use of transitive and transitive verbs.
  • Plural and singular

So far, this is the only thing I care about.

No need for obvious words

For example, the variable name called 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

If you create a variable with a single noun, it will be misleading to the reader when the number of codes increases. For example, if there are variables such as data here and there, it will be hard to read.

Avoid abbreviations

This is convincing. I don’t know if get_user_data will be gud as expected.

methods use verbs

Because it is a processing code, it is best to use a verb.

Use nouns for class names and variables

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

Do not use paraphrasing expressions

Because the code is not a sentence, it is better not to paraphrase it. If you do something like writing a sentence, you tend to do it. Don’t express by using things that have almost the same meaning such as Memmber = user, but unify those that have the same meaning with the same word.

Deprecated notation

ㆍHungarian notation is a naming method that puts information such as type and scope into variable names and class names, which is currently deprecated and seems to be an anti-pattern in many cases. For example, if it is an array, you should avoid using arr_data as a variable.