Naming rules for variables, functions and classes

Time:2019-10-6

1, naming is worthy of the name.

When naming, we must consider its display meaning and context, and do not think that it is a waste of time to spend on variable naming. Of course, if your program is very short, you don’t need to think so much about it. Just bury your head in writing code. If your project is large and has a lot of files, it’s best to spend time naming, because your code is not just for you.

2. Avoid misleading:

That is to say, do not name other platform keywords, for example, I use the JavaScript language, and I designed a variable for int, which makes people very headache. Why do we need a int variable, is this code C++? That would be misleading. If you want an integer variable, you can do this, var intNumber;

3. Make a meaningful distinction:

Variable: Never use it as the name of a variable, table never as the name of a table, etc. Instead of using similar names, meaningful distinctions must be made, such as: there is a set of variables, some children’s shoes will be named like this, a1, a2, a3…, and if we name them according to their role, the effect will be much better.

4. Use easy-to-read names:

It’s best to use an English or English phrase, not to mention some Chinese, some English mixed, or simple Chinese. For example, we want to write a function to switch the language: qieHuanYuYan, so that people can read one by one to spell, and finally not necessarily be able to spell successfully, if changed to: change Language effect will be much better.

5. Use searchable names:

I suggest that when you use numbers, you can best use the uppercase and underline definition at the very beginning of the code. For example, the 50 degree is the maximum power crossing value of a human body, so we can name it: PEOPLE_MAX_VOLTA; of course, my English ability is a bit bad, but basically it is this principle. If you simply use 50, people will not know what the standard is, and if they become variables, they will be easy to read. And you’d better not use a single English alphabet, because it can cause considerable trouble, not only search, but also difficult to understand.

6. Avoid coding:

Don’t use prefix names, such as adding an m_, which can be named many times, but it’s hard to understand.

7. Avoid thinking mapping:

Avoid a single name, be clear is the kingdom, do not think that your single name others do not understand, you are very compelling. If the code looks like a novel, it’s bull-punch.

8. Class name, method name

Class names should be nouns or noun phrases. For example: Customer, WikiPage, avoid using words with actions such as Maneger and Perocessor; method names, as far as possible to reflect the action of this method, so it is best to use verbs or verb phrases, such as postMail, deletePage, and so on.

9. Each concept corresponds to a word:

Choose a word for each abstract concept and use it habitually, such as get, method, getMsg (), getUsername (), getUerId (), etc.

10. Don’t use puns:

Avoiding the use of the same word for different purposes, such as add, is not very good, and you can replace it with append.

11. Add context:

It is to give some variables or functions some context, such as: this variable, firstName, lastName, street, state, these things seem to be understood by us, but we all do not know what he describes, if add address, change to; addressFirstName, addressLastName, addressStreet, addressState, this is clearer and clearer.

When naming variables, functions and classes, we must not be afraid of their long term. As long as we can understand them, of course, if the short term can be described clearly, we must choose the short term. I mean, if we want to describe clearly, we should not be afraid of the long term naming of variables, functions and classes.

Recommended Today

The method of obtaining the resolution of display by pyqt5

The code is as follows import sys from PyQt5.QtWidgets import QApplication, QWidget class Example(QWidget): def __init__(self): super().__init__() self.initUI() #Interface drawing to initui method def initUI(self): self.desktop = QApplication.desktop() #Get display resolution size self.screenRect = self.desktop.screenGeometry() self.height = self.screenRect.height() self.width = self.screenRect.width() print(self.height) print(self.width) #Show window self.show() if __name__ == ‘__main__’: #Create applications and objects app […]