PHP "refactoring-improving the design of existing code" Five simplified function calls _php tips

Source: Internet
Author: User
Tags error handling visibility
Mind Mapping


introduce

The previous series of articles, I am more concerned about the <php "refactoring-improve the design of the existing code" to rearrange your function; but I think I still did not make it clear, I also have a lot of do not understand the place, and this is my first article in this regard, there are many flaws, So I will make regular changes, if you have good opinion may wish to inform one or two.

Today we talk about "interface", this interface is not "Interface", but a general designation. We can generally call the interface for functions or URLs (generally used to provide data) for others to use. may have other meanings, after all, I still belong to the "rookie", if there is a mistake in understanding, please correct me.

We know that "easy to understand and be used interface" is the key to developing good object-oriented software. -This article introduces the refactoring approach of "making interfaces simpler and easier to use".

Digression:
If you think that my article is too long and seems to be troublesome, we suggest "just look at pictures and bold text".
Yesterday, "old" Bo friends to give me a message, I have not carefully considered, this time I also thought about it. Message content is:

I personally feel that a lot of things only we have to focus on, to know its value.
As for simplicity, the purpose of refactoring is to be simple and easy to understand.
As for persistence, I think in technology, we often need this obsession, even after you think you are wrong, but we will have some gains between this. We only have experienced many times the running-in (this kind of running-in has the right also to have the mistake), we can know its value, we can harvest the thing which we need.
As for the interests, "old" is not referring to the interests of the company, yes, indeed, many times we are in the process of coding, we need to catch up on progress, and we will have some errors in refactoring, so my suggestion is that at the beginning of development, you will be in the design and reconstruction, constantly running, do not feel wasted time, many times, A good structure can speed up your development.

Professional Terminology

Rename method

Condition: If the name of the function fails to reveal the purpose of the function, modify the function name.

Motivation:
One of the programming styles I strongly advocate is to decompose complex processes into small functions. But if the small function is not named well, it will make it difficult for you to figure out what these small functions are for.

A good way to name a function: Consider how to write a comment on the function-—— > think of ways to turn annotations into names of functions.

A good name is not easy and requires experience. To be a real programmer, the "name" level is critical.

If you see a function name that is not a good expression of its purpose, it should be modified immediately.

Example:

ADD Parameter

Status: A function needs to get more information from the caller, so add a parameter to the function to bring the parameter into the information required by the function.

Motivation:
1, Add Parameter is a very common method of reconstruction.
2. The modified function requires some information that was not in the past, so you need to add a parameter to the function.
3, in addition to add Parameter, whenever possible, other options are better than "Add Parameter", because it is possible that other options will not increase the length of the parameter column. --Too long parameter columns will keep programmers from remembering so many parameters.

Remove Parameter

Condition: The function ontology no longer requires a parameter, then the parameter is removed.
Motivation:
1. The parameter indicates the function information, and the different parameters represent different meanings. Function call this has to worry about what to pass for every argument. --If you don't remove the parameters, then pay a little more for each call.
2. If you find a lot of callers, you can do so in order to keep the caller from worrying, by setting the parameter to be removed to a default value (such as null), so that the caller only passes parameters that have no default values.

Separate Query from Modifier
Status: If a function returns both the state value of an object and the state of the object (side effects), then two different functions are created, one for the query and the other for the modification .

Example:

Parameterize method
Condition: If several functions do similar work, but contain different values in the function ontology, then a single function is established to express those different values in Parameters .
Motivation:
1, generally because there are a few different values, so set up a number of similar functions.
2. The separated function is replaced by a unified function, and the parameters are used to deal with those changes to simplify the problem.
3, remove duplicate code, improve flexibility. --You can use this parameter to handle other changes.

Example:

Replace Parameter with Explicit Methods
condition: You have a function that depends entirely on the value of the parameter and takes a different reaction, so a separate function is established for each value of the parameter .
Motivation:
1, if a parameter has a discrete value, and the function of the conditional check these values, and according to different parameter values to make different responses, then you should use this refactoring.
2, can obtain the benefit: "Compile time code Check", "The interface is clearer" (if uses the parameter value to decide the function behavior, then the function user not only needs to observe this function, but also must judge the parameter whether "legalize". --and the legitimate arguments, rarely mentioned in the document, must pass through the context to be judged.
3, regardless of the "compile-time Test" benefits, in order to obtain a clear interface, we also deserve to do so.

Example:

Preserve Whole Object
Status: If you take a number of values from an object and use them as arguments in a function call, change (pass) the entire object .
Motivation:
1, the parameter column is more stable;
2, improve the readability of the code;-long parameter columns are difficult to use because both the caller and the callee must remember the purpose of these arguments.

Example:

Replace Parameter with Methods
Condition: If the object calls a function and takes the result as a parameter and passes it to another function (the function that accepts the parameter also has the ability to invoke the previous function), let the parameter recipient remove the parameter and call the previous function directly .
Motivation:
1. If the function obtains the parameter value by other means, then it should not get the value through the parameter.
2, too long parameter column will increase the comprehension difficulty of the program reader, so we should shorten the length of the parameter column as much as possible.
3, the method: To see if the "parameter accept" can be "with the caller side of the same calculation" to obtain the parameter carrying value.
4. If the function call side computes the parameter through another function within the object, and "Never references other parameters of the caller" in the calculation, the calculation process can be transferred to the callee, thereby removing the parameter.
Example:

Introduce Parameter Object
condition: Some of the parameters are always naturally occurring at the same time, so replace them with an object .
Motivation:


1, a group of parameters may have several functions to use at the same time, these functions may belong to the same class, may also belong to different classes. --such a set of parameters is called the Data Clump (Mud Regiment).
2, we can use an object to wrap all this data, and then replace data clump with objects. --it is worthwhile to do so even if it is to organize the data together.
3, the value of this refactoring is to "shorten the length of the parameter column." In addition, the access functions defined by the new object (accessors) can also make the code more consistent. This further reduces the difficulty of understanding and modifying the code.
4, this refactoring can also bring you more benefits. When you organize these parameters together, you can quickly find the behavior of "porting new classes". --Reduce duplicate code.
Example:

Remove Setting method
Status: a range in your class that should be set when the object is established, and then no longer changed, removes all the setup functions (setters) of that range.

Motivation:
1, if you provide a set function (setter) for a range of domain, this implies that the range can be changed.
2. If you don't want to have a chance to change this range after the object is set up, don't provide it with a setup function. This will make your intentions clearer and eliminate the possibility of their values being modified.
Example:

Hide method
Status: If there is a function that has never been used by another class, set this function to private.
Motivation:


  1, refactoring often prompts you to modify the "visibility of the function." -Always check for functions that can be hidden.
2, often check whether it is possible to reduce the visibility of a function (make it privatized).
--> When you remove a call to a function in another class, you should check it.
--> specifically perform the above checks on setter functions.
  
Replace constructor with Factory method
Status: If you want to create an object that is not just a simple widget action for it, replace the __construct (constructor) with factory method.
Motivation:
Replaces the type code with factory method during the subclass process. You may often need type code to create the corresponding object.
Example:

Then come:

Replace Error Code with Exception
Status: If a function returns a specific code (special codes) to indicate an error condition, use an exception (Exception) instead.
Motivation:
Clearly separating "normal" and "error handling" makes the program easier to "understand".

Example:

Conclusion
I share every harvest with you, if you have so little harvest, also let me happy unceasingly. There is also a mistake in the article, look for guidance one or two.
I do not know whether to find the wrong place, there are blog friends said "blog Park in the main popular C #", see people are not mainly PHP programmers mainly? There are very few people who leave messages to me, and few people point out the mistakes in my article (Are there really no mistakes in my article?). Yesterday, "The four-eyed masked man" left a message for me, I was in the conversation with him a lot, but also thanks to his criticism, but also hope to communicate with you.

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.