How PHP writes readable code _php tips

Source: Internet
Author: User
Tags lowercase readable
Successful development teams require each member of the team to comply with code reuse rules that push code reusability to the extreme without significantly reducing developer creativity and development efficiency. If the developer who writes and uses code adheres to the common program naming code and code annotation requirements, then the reusability of the code can be greatly improved. The starting point for these standards is system-structured. Your functional specifications should reflect these criteria in the class, the name of the attribute, the return value of the function, and the definition of other key program elements. This article will provide some practical advice on basic naming rules and annotations, intended to help readers develop their own code reuse standards.


Case standard


Before we start talking about the proper way to name the various program elements, let's define two of the most common ways to distinguish the character case of elements:

Pascal specification-The 1th character is capitalized, and the 1th letter of each word in the target name is capitalized, such as InvoiceNumber or Printinvoice. All other characters are lowercase.
Camel specification-The 1th character is not written, but the 1th letter of each word in the target name is capitalized, for example, InvoiceNumber. All other characters are lowercase.
However, using character capitalization to differentiate elements can cause problems in programming languages that are not case sensitive. For example, because the C # language is case-sensitive, you can call the private variable employee, and then the public attribute employee it has can be used by the caller. These operations are completely legal. However, for Visual Basic, there is an error, because VB is not the case of letters, the above two elements in VB seems to be one thing. If you work in a mixed-language environment, you can only specify rules that require developers to read code that other people develop in a reasonable variety of languages.


Naming standards
Assuming we use the above capitalization criteria, let's now take a look at some simple naming recommendations for generic program elements.

Class
Some classes are designed to simulate real-world objects, and in these classes the names chosen should reflect real-world objects, with singular nouns such as employee, invoice, or timecard. For internal classes, the Pascal specification can be used to give the result class A singular name, such as ThreadPool or Customcolor. Classes should be singular, so that their plural form can represent similar sets of names, such as employees arrays.

Members of a class
Developers who use C # and other case-sensitive programming languages should use the name of the camel specification for naming class members. Doing so will make it easier for developers to distinguish between the name of the internal variable and the name of the public property (name). Many VB developers prefer to use Hungarian nomenclature to name class members, that is, prefix the first name with the type of variable, such as sname refers to the string type name variable. I think it's not necessary to do this in a high-level development environment with vs.net because in this case the system mouse hovers over the variable to automatically display the type of the variable. I personally like to prefix the class member name: The lowercase letter M. This internal variable saves enough internal class information: The internal variable mname represents the public property name.

Method
Methods should be named with Pascal specification, and their implementation behavior should be explained in a reasonable way. For example, the method of adding an employee to a database can be named AddEmployee, and the method of printing the invoice may be named Printinvoice. If the method returns a Boolean value, the method name should begin with a verb so that it is more obvious when used in the IF statement. For example, if you have a method that determines whether an employee meets the requirements of the company's 401k program, 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 by Pascal Specification, and the same method name should be able to reflect the meaning of parameters or variables represented. This is especially important for parametric methods, because IntelliSense (IntelliSense) returns the parameter names and argument types when you invoke the method. All developers using the method should use descriptive names and types to facilitate understanding of the meaning of each other.

Control
Control naming is a frequently contentious issue in the development field. While most people agree that you should not use the default name of the control, such as TextBox1 or LABEL1, they also object to naming the control as a variable or prefix to the type of the control. I prefer to use the standard three-letter prefix to name the controls in the form. For example, the text box control that holds the last name and first name may be named Txtlastname and txtFirstName respectively. command buttons that work with form data can be named Cmdsubmit or Cmdcancel. In fact, as long as you can guarantee the consistency of the control naming and the standard is easy to understand.

Comments
Annotation code is necessary for all developers. To teach the right annotation techniques, I often add annotation code to my demo program. Also, to simplify the annotation process, I recommend that developers first write a note explaining the program they want to write. I will first write a note describing the process, class, or other procedural elements in the program, but not the specifics of how it works. I then write a series of annotation codes describing each major step of the process or the elements of the class. After writing the code that defines the class or the description process, I document the elements accessed by the external variables, controls, open files, and even other procedures, and briefly describe the input parameters and return values.

If you are using the C # development program, the VS.NET environment already has built-in tools to help you translate internal C # annotations into external HTML documents. You can change the way an external document is represented by adding special processing indicators to your document. For more information on this, refer to the vs.net internal Help file: Ms-help://ms. Vscc/ms. Msdnvs/csref/html/vcorixmldocumentation.htm.

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.