The way to clean code -- naming conventions

Naming code is really a headache. For naming rules, some basic coders will know to use camel case naming, such as userName, designPattern and other camel case naming, which is very cool, and don't use it when naming What names like int a, int b are used, but otherwise? What else do I need to do when naming it? In the book "Clean Code", there are other things we need to learn about naming conventions, let's take a look

 

1. Naming should be meaningful

How to express the concept of how many days, int day? This seems to be a good choice, at least I like to name this way for a long time, but in fact it is not good to name it this way, because the concept of day is a broad one Based on the concept, many days can be extended according to business scenarios, such as elapsedTimeInDay (delayed days), daysSinceCreation (creation days), etc. At the beginning, you may need a field in your class to record how many days a plan has been executed. You record this field int day. In the later period, you need to add other things such as timeout, and you need to add a day representation, but because there is already a day field, it can only be defined as an overtimeDay, so in your method, There is one day and one overtimeDay, which is a disaster for code readability. If you want to modify one of the variables, you have to modify the code that uses this day variable, the database field name, and so on, such as naming Don't arbitrarily name it for the convenience of the diagram, and consider later expansion.

2. Avoid misleading

l, o In the editor, it is easy to be mistaken for 1 and 0. You should avoid using them to define variables and define class names. The name of the class should avoid overlapping with the names of some commonly used API classes, which will lead to inconsistencies in use. Clear which is your own and which is the API

3. Use readable words as variable names

The words we use as names should be words that can be read. Sometimes we need to describe the affairs, and it may take several words to express our meaning, such as (sending information simulator) Sending information simulator, it is not recommended to compress the naming The length of the code is abbreviated as sis or siSimulaor in this format. The long name of the word is named more than the short word that loses its meaning. It can express the meaning of the code.

4. Use constants instead of fixed characters

int salary = 0;
        for(int i=0; i<21; i++){
            salary += 190;
        }
        System.out.println(salary);

Look at the following simple code, calculate the salary for 21 days and output it. It can be understood at a glance here, because there is very little code and no context interference, but if it is in a complex business logic, when you see this paragraph The two numbers in the code, 21 and 190, do you immediately know what that means? How can I improve this code to make it easier to understand?

The use of constants instead of fixed matches is helpful for understanding, as well as for full-text searches.  

ACTUAL_WORKING_DAYS is also easier to find than 21

5. Avoid using member prefixes

In the book "The Way to Clean Code", the author does not recommend using member prefixes, such as IService and m_apple. I means that this class is an interface, and m_ means that this is a member variable.

6. Stick to your name and don't be influenced by others

Not everyone will strictly follow the naming convention. Maybe your predecessors are too lazy for the progress of the project. In a function, list1, list2, and list3 appear at the same time. After you take over, don’t have any code. , then I also define a list4, list5, don't have such a mentality

7. The naming should be rigorous, no jokes

Maybe you think the code is so boring, you want to use some stalks as a name, such as robberyShop (robbery), defined as zeroDollarPurchase (zero yuan purchase), don't do this, it may confuse people who receive your code later

Note: Zero-dollar purchases are generally used to ridicule some people in the United States for robbing stores in groups during riots;

8. Class and method names

The class name should be a noun and the method name should be a verb

9. Use proper nouns for the fields involved

The software we develop involves various fields. Some nouns in some fields are not simply translated word for word. They have their proper names. For example, if you are making a music-related software, there is a name in it, the eighth note.

If the foreign name is Eight note, then you should not define it as an Eight music symbol. The naming of proper nouns should be carefully confirmed, not taken for granted.

10. Don’t use synonyms

Vehicle, and car both have the meaning of car, date and time have the meaning of time, organization and department have the meaning of organizational department, don't use this word for a while when you write the code name, and then another Name it with another word. If both car and vehicle appear in a function, I hope the brother who takes over your code will not scold you.

Summarize

I can summarize these 10 items. I think they are more useful and more suitable for actual production. They should be 1, 2, 3, 4, 6, 7, 10

Ok, I've finished summarizing, and then I'll summarize about the function code specification

Guess you like

Origin blog.csdn.net/qq_45171957/article/details/122275614