PHP: how to write readable code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP: how to write readable code. A successful development team requires every member of the team to abide by the code reuse rules, these regulations push code reusability to the extreme, but do not significantly reduce developers' creativity and a successful development team requires every member of the team to abide by code reuse rules, these regulations push code reusability to the extreme without significantly reducing developers' creativity and development efficiency. If developers who write and use code comply with the code and code annotation requirements of the Common Program naming conventions, the code reusability will be greatly improved. The starting point of these standards is at the system structure level. Your functional specifications should reflect these standards in the definition of classes, attribute names, function return values, and other key program elements. This article will provide some feasible suggestions on basic naming rules and annotations to help readers develop their own code reuse standards.


Case sensitivity


Before we start to discuss the correct naming methods for various program elements, let's first define the two most common methods for distinguishing the characters of elements in case. they are:

Pascal specifications-1st characters in upper case, and 1st letters of each word in the target name are also in upper case, such as InvoiceNumber or PrintInvoice. All other characters are in lower case.
Camel specifications-1st characters are not capitalized, but 1st letters of each word in the target name are capitalized, for example, invoiceNumber. All other characters are in lower case.
However, using case-sensitive characters may cause problems in case-insensitive programming languages. For example, because the C # language is case-sensitive, you can call the private variable employee. then, the common attributes of this variable can be used by the caller. These operations are completely legal. However, errors may occur for Visual Basic, because VB is case-insensitive and the above two elements are the same thing for VB. If you work in a hybrid language environment, you can only specify certain rules that require developers to read code developed by others in a rational manner using multiple languages.


Naming Standard
Let's assume that we use the above case sensitivity standards. now let's take a look at some simple naming suggestions for common program elements.

Class
Some classes are designed to simulate objects in the real world. for these classes, the names selected should reflect objects in the real world and have the format of singular nouns, for example, Employee, Invoice, or Timecard. For internal departments and classes, Pascal specifications can be used to make the result class named in the singular form, such as ThreadPool or CustomColor. Classes should be singular, so that their plural forms can represent the same set names, such as the Employees array.

Class Member
Developers using C # and other case-sensitive programming languages should use the camel standard to name class members. In this way, developers can easily distinguish the names of internal variables from those of public attributes ). Many VB developers prefer to use the Hungary naming method to Name class members, that is, to add a prefix before the Name to indicate the type of the variable. for example, sName refers to the Name variable of the string type. In my opinion, it is unnecessary to do so in an advanced development environment such as VS. NET, because in this case, the system mouse stays above the variable to automatically display the type of the variable. I personally like to add a prefix: lowercase letter m before the class member name. In this way, the internal variable saves enough internal class information: the internal variable mName exactly represents the public attribute Name.

Method
The method should be named in Pascal's specification, and their implementation behavior should be illustrated in a reasonable way. For example, the method for adding an employee to a database can be named AddEmployee, while the method for printing an invoice may be named PrintInvoice. If the method returns a boolean value, the method name should start with a verb so that the meaning of the method name is more obvious when the if statement is used. For example, If you have a method to determine whether an employee meets the company's 401k plan, you can call the IsEligible401k method in the If statement: If IsEligible401k then...

Method parameters, return values, and variables
All method parameters, return values, and variables should be named in Pascal format. The same method name should also reflect the meaning of parameters or variables. This is especially important for parameter methods, because when you call a method, intelligent awareness (intelliisense) will return the parameter name and parameter type. All developers using the method should use descriptive names and types for mutual understanding.

Widget
Naming controls is a controversial issue in the development field. Although most people agree that the default name of the control should not be used, such as TextBox1 or Label1, they also oppose naming the control as a variable or using a prefix to indicate the type of the control. I prefer to use a standard three-letter prefix to name the name of the control in the form. For example, the text box control that saves the last name and name may be named txtLastName and txtFirstName respectively. The command button for processing form data can be named cmdSubmit or cmdCancel. In fact, as long as you can ensure the consistency of the control naming and the standard is easy to understand.

Note
Code annotation is necessary for all developers. To teach the correct annotation technology, I often add the annotation code in my own demo. At the same time, to simplify the annotation process, we recommend that developers write annotations to describe the programs they want to write. First, I will write a comment to describe the process, class, or other program elements in the program, but I will not elaborate on the specific working principle of the program. Then I will write a series of comments to describe the elements of every major step or class in the process. After coding the definition class or description process code, I documented the elements accessed by various external variables, controls, opened files, and other processes, briefly describe the input parameters and return values.

If you are using C # to develop programs, the VS. NET environment already has built-in tools to help you convert internal C # annotations into external HTML documents. You can add special processing indicators in your documents to change the representation of external documents. For more information, see the internal help file ms-help: // MS. VSCC/MS. MSDNVS/csref/html/vcoriXMLDocumentation.htm in VS. NET.

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.