Building a knowledge system-the first show of PHP code specifications! ----- In the previous blog "building a knowledge system" on PHP code standards, I mentioned a lot of detailed issues. next I will discuss the above blogs in detail: everyone, the specifications of each company may be different. I just want to talk about my specifications! Improve code readability: add comments to the code and read a piece of code without comments. it is like building a knowledge system-PHP code specification
Debut! ----- PHP code specification
The previous blog "building a knowledge system" mentioned a lot of detailed questions. next I will discuss the above blogs in detail:
Each person and company may have different specifications. I just want to talk about my own specifications!
Improve code readability:
- Add comments to the code and read a piece of code without comments. it is like reading the scriptures by Tang sengxi, which is hard to understand without passing through!
- Add blank lines to the code. the code that is good at using empty lines is like music with a sense of rhythm. there are too many lines and too few lines!
- 30-second rule: other programmers fully understand your member function code, which cannot exceed 30 seconds. Otherwise, this code is difficult to maintain.
- A line of code should not exceed 120 characters; otherwise, it is better to display a function within the size of a screen, and the maximum length is no more than three screens.
- One line of code only does one thing. Similarly, a function is only responsible for one function.
- When the loop is large, do not use a meaningful name like $ I.
- While, if, else nesting should not exceed three layers, or how to read it in 30 seconds?
- The least comments of a function should be useful, all parameters, and all returned values. it is best to comment on the author (Accountability)
- All indentation are 4 spaces rather than tabs, and all linefeeds are unix linefeeds. to ensure that 100% is correct, you can enable the IDE function to display blank and invisible characters.
Improve code efficiency and robustness:
- If DB-type public processing is not used, use mysql_escape_string to escape the query string before querying the SQL statement.
- Define the constant containing the absolute path in the first introduced File. All other introduced file paths start with constants.
- All class constants, static variables, and member variables should be stated in advance, and their purposes should be annotated in the declaration, and their values should be initialized to a security value.
- If DB-type public processing is not used, use mysql_escape_string to escape the query string before querying the SQL statement.
- To call external interfaces and functions, place them in try/catch blocks, check the returned values, perform robust error processing, and record logs.
- PHP files are not closed at the end of the PHP file to avoid unnecessary empty lines from being output !!!
Naming rules:
- Naming should be meaningful and Annotated. when other programmers read it, they can understand it at a glance, read it, and remember it.
- All case-insensitive PHP keywords use lowercase letters, for example, true false null break.
- All variables, class member variables, function names, and class member function names are named in the hump format.
- All constants, class constants, and static variables use uppercase letters and underlines to separate words.
- Static class variables, class member variables, and class member function names of the private and protected attributes start with an underscore
- The file name consists of letters, numbers, and underscores. The first character must be a letter. if the file is a class, the file name should be "class. php ", such as" DBConfig. class. php ", otherwise all file names are in lower case, separated by underscores (_), for example," post_list.php"
- The class name adopts the Pascal naming rule, that is, all the first letter of a word is capitalized, and the parentheses are separated by a space after the class name.
- All class constants are capitalized and words are separated by underscores
Debate
- All "{" left braces are followed by spaces and cannot be independent rows.
- For Associated arrays, we encourage code to be divided into multiple lines, and the keys of each line are indented to alignment, "=>" left and right
There are a lot of unwritten code. I hope we can provide more suggestions and make our code more and more standardized.
--- This specification is subject to the code specification of Fuzhou Cheyou Network Technology Co., Ltd.