Develop robust code with PHP: write reusable functions
Source: Internet
Author: User
In this article, you will learn how to use functions wisely in php (as a mainstream development language. In every advanced programming language, programmers can define functions, and php (as the mainstream development language) is no exception. The only difference is that you don't have to worry about the function return type. In-depth study of SyntaxHighlighter. all ();
In this article, you will learn how to use functions wisely in php (as a mainstream development language. In every advanced programming language, programmers can define functions, and php (as the mainstream development language) is no exception. The only difference is that you don't have to worry about the function return type.
In-depth research Functions can be used:
Encapsulate several lines of code into one statement.
Simplified code.
The most important thing is to use applications as the product of coordination between smaller applications.
For developers who have switched from a compilation language (such as C/C ++) to php (as the mainstream development language), php (as the mainstream development language) the performance level is surprising. In terms of CPU and memory resources, user-defined functions are very expensive. This is mainly because php (as the mainstream development language) is interpreted and loose.
Package or not Some developers simply wrap every function they use because they do not like the function name, while others do not like the packaging.
Packaging existing php (as the mainstream development language) functions without adding or supplementing existing functions is totally unacceptable. In addition to increasing the size and execution time, such rename functions may sometimes cause management nightmares.
Inline functions in the code can cause inexplicable code, or even greater management disasters. The only benefit of doing so may be getting a faster code.
The more sensible way is to define a function only when you need to use code multiple times and there is no built-in php (as the mainstream development language) function available for the task you want to implement. You can choose to rename or use it only when necessary.
The chart in figure 1 roughly shows the relationship between manageability and speed and the number of functions used. (I did not specify the unit here, because the number depends on the individual's and team's capabilities; this relationship is important visual data .)
Figure 1. manageability/speed Vs. number of functions
Naming functions
For effective code management, it is essential to always use public naming conventions.
The other two practices must be considered:
The selected name should be able to properly prompt the function.
Use the prefix indicating the package or module.
Assume that you have a module named user that contains user management functions. for functions that check whether a user is online, such as usr_is_online () and usrIsOnline () such a function name is a good choice.
Compare the above name with a function name such as is_online_checker. The conclusion is that using a verb is better than using a noun, because a function always performs something.
How many parameters? It is very likely that you will use the constructed functions. Even if this is not the case, you may want to maximize the scope of use of the code. To achieve this, you and other developers should continue to develop easy-to-use functions. No one prefers to use functions with obscure and difficult-to-understand parameters. Therefore, compile easy-to-use functions.
Selecting a name that describes the purpose of a function (and reducing the number of parameters used by the function) is a good way to ensure ease of use. What is the magic number of parameter quantity? In my opinion, more than three parameters will make the function hard to remember. Complex functions that use a large number of parameters can be split into multiple simpler functions.
No one prefers to use a function.
Compile high-quality functions Suppose you want to set the document title before placing the HTML document in the browser. The title is...All content between tags.
Assume that you want to set the title and meta tags. Without using the setHeader (title, name, value) function to execute all the work, it is a better solution to use setTitle (title) and setMeta (name, value) to complete the work respectively. This scheme sets the title and meta tags independently of each other.
Further, the title can contain only one title tag, but it can contain multiple meta tags. If you need to set multiple meta tags, the code must call setMeta () multiple times (). In this case, a better solution is to pass the two-dimensional array with name-value pairs to setMeta (), and let the function loop to execute the array-and execute all operations at the same time.
In general, such simultaneous functions are more desirable. The one-time call of a function with all the data to be processed by a function is always better than calling a function multiple times and provides data for it in incremental mode. The main idea when writing a function is to minimize the number of calls from other code.
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