Novice on the road, please give more advice and welcome communication. Some examples in the article may not be very good, welcome to correct; This article discards the order temporarily, and then arranges it when free
This article can find a complete text description in code Daquan 2. This article just briefly records my reading experience
The code examples in this article may have errors. After all, they are written directly
Focus on expressing meaning. It's probably good when the meaning comes
Reading rules and requirements: undetermined, simple C#, lua grammar knowledge, or even not
1. The declaration of variables should follow some naming rules
There are two common naming methods. One is Pascal naming method, which refers to the capitalization of the first letter of each word;
The other is camel nomenclature (hump nomenclature). Camel nomenclature means that the first word is lowercase and the first letter of each word is capitalized from the second word.
Pascal nomenclature: StudentName, StudentClass
Camel nomenclature: studentName, studentClass
**Naming depends on how it is used in the project**
2. When declaring a variable, you need to initialize its value and initialize it near the position where the variable is used for the first time
Initializing a variable near where it is used for the first time can make you more intuitively understand what the variable does and what it involves
//**Nonstandard example * *, sum variable int sum = 0; //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum //Omit N lines in the middle of the code that has not used sum sum = GetArrarySum(intArrary);
//**Specification example * * sum variable //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum //Omit N lines of code that has not used sum int sum = 0; sum = GetArrarySum(intArrary); //Or it can be written as int sum = GetArrarySum(intArrary);
Benefits: you can directly see its value, which is easy to read (it can even be said to be easy to maintain...)
3. Pay attention to the life cycle of variables and shorten the survival time of variables as much as possible
For example, 2
Purpose: short life cycle means that your variable involves less other code and logic, that is, your variable has stronger purpose and less coupling with other statements
4. Each variable is only used for a single purpose
//**Examples of non-standard** int sum = 0; int tempSum = 0; for(int i = 0; i < 10; i++){ tempSum += i; } sum = tempSum; //The above is a location for tempSum //Here is the second use location of tempSum //Swap two values int valueLeft= 5; int valueRight = 10; tempSum = valueLeft; valueLeft= valueRight; valueRight = tempSum;
Disadvantages: it does not comply with rules 2 and 3
//**Specification examples** int sum = 0; int tempSum = 0; for(int i = 0; i < 10; i++){ tempSum += i; } sum = tempSum; //Swap two values int valueLeft= 5; int valueRight = 10; int temp = valueLeft; valueLeft= valueRight; valueRight = temp;
Benefits: variables involve less areas, so you can quickly know the role of variables, etc
5. Make variables self explanatory
Good variables should be self explanatory. They can even make you do not need comments, or you don't need to think too much when reading this code. Who am I, where am I, and why
//**Nonstandard examples, including the naming of functions, may be too exaggerated** int a = daqlwijwd(hdstgsg); //Solution: what is a, What does daqlwijwd this method do
//**Specification examples** int sum = GetArrarySum(arrary); //Solution: what is sum, What does the getarraysum method do
6. The longer name is applicable to rarely used variables or global variables, while the shorter name is applicable to local variables or cyclic variables
Similarly, readability increases directly, making it a conditional reflection. When you read this naming convention, you can know what it does
For example, the name used in the project: UPDATE_QUEST_RED_DOT
As soon as I see this, I know that this is a global data about task red dot events
7. Variable naming uses the antonym -- boolean type
Boolean types can only have two values, true and false
Therefore, using antonyms can greatly increase the readability of your code
//See if you can see what value does at a glance bool startDownload = true; bool endDownload = false; bool startWalk = true; bool endWalk = false; //It shouldn't be difficult
8. Name the temporary variable to increase its readability
--This example uses lua code for i,v pairs(xxx) do print(v) -- Ask v What is it? end -- for id,name pairs(xxx) do print(name) -- Ask name What is it? end
9. Use positive boolean variable names
Using positive boolean variable naming is more in line with people's habit. Although double negation can be understood, it is not direct and simpler than single negation after all
--**Examples of non-standard** local notFound if not notFound then --todo something end --**Specification examples** local found if not found then --todo something end
10. Extract the same attribute or similar variable, and then write it as an enumeration type
//Define four seasons const int spring = 0; const int summer = 1; const int autumn = 2; const int winter = 3; //It might be more appropriate to change to season enum Season{spring, summer, autumn, winter};
11. Avoid using mysterious numbers, such as 100 or 123456
Readability!!!
Easy to maintain!!!
//There is a method to pass in the int parameter N greater than 0 as the number of days and calculate the number of seconds in N days int GetSecondsByDay(int day){ return day * 24 * 60 * 60; }
What are the disadvantages of writing like this
1. Readability, what is 24? What is 60?, What is the second 60?
2. Maintenance problem. In case the next day becomes 25 hours, do you want to come here alone and change 24 to 25? Or 23 hours?
//Define hours, minutes and seconds somewhere //All places share this hour, minute and second to synchronize all data //When changing, just change here //(but this is definitely not the case in the project. Here is just a simple example) const int Day = 24; const int Hour = 60; const int Minute = 60; //There is a method to pass in the int parameter N greater than 0 as the number of days and calculate the number of seconds in N days int GetSecondsByDay(int day){ return day * Day * Hour * Minute ; }
12. Use Boolean variables to simplify complex judgments
Applicable to if (condition){
//todo
}
The condition statement is too long
bool IsPlayerDie(HP, whether to die directly){ if(Life value <= 0 && No big moves && No resurrection armor || Direct death) //death } //Change to bool IsPlayerDie(HP, whether to die directly){ bool Ordinary death judgment = Life value <= 0 && No big moves && No resurrection armor if(Ordinary death judgment || Direct death){ //death } }
13. You can use structures to simplify the parameter list
When too many parameters are passed, try to optimize the parameters to make a structure?
//Print student information void PrintStudentInfo(int id, string name, int classNumber, string address../*Ignore n attributes*/) { print(id); print(name); print(classNumber); print(address); ... } //Rewrite: void PrintStudentInfo(Student student) { print(student.id); print(student.name); print(student.classNumber); print(student.address); ... }
14 do not write local data as global data
Obviously, it is a variable used in a method, which is not used elsewhere. Why should I write it outside the method?
15. When using global data, you should use naming to highlight the role
For example, 6, I can see what this variable is used for and where it is about. It's not because I think the code is powerful, but because the code written by others is standardized
Maybe not