Code neat way - Function

What good is a function or method call, as long as the function to make a clear expression of its intent, allows the reader to see at a glance what is a function that receives any parameters, return any results, do anything. Can do it, you can probably count as a function of the good, it looks very simple. So the question is, if you do it?

1. Short

Imagine a beat complex function and a function of only a dozen lines of flight, people can see at a glance which of its intentions and understand their behavior? Clearly the latter. Of course, most languages ​​can be hundreds of lines of code written to one line, so there is no code format only difficult to understand.

2. The code block and the indented

For if, else other statements, the code block that contains only one row of the best, and this line should be a function call statement, so that a reader can look out their intentions

3. function name

A good name is very important function, how to define a good function name it? It can be felt. If a function, you see only the function name, you can understand what he is doing, what the return, then it is a good name of the function.

4. Do one thing

Function should do one thing and do this thing enough. This we all recognize it, and is already a consensus, but easier said than done but it is not so simple. So how do you ensure that the function of only one thing? We can try this again split out a function in the function, of course, can not just split out the function is to move past the code, it should have its own responsibility, we can play a good function name for the new function. If you can not split out such functions, then he should be fine.

The function parameters

Ideally the number of parameters is 0, followed by increments, three parameters has been a lot.

In terms of function test, test function with no parameters simply a piece of cake, if a parameter, we need to test a variety of combinations, each one more parameter after the number of combinations tested are exponentially.

On the caller, the function has no parameters can be called directly, and with the parameters you need to understand what each parameter is also increasing the time the caller.

If there is a Boolean value function parameters, sorry, this is not recommended, because it explicitly tell the caller if true would do, if it will do that is false. Better approach is to break it up into two functions.

Meanwhile, if a function requires more than three parameters, the package can be considered some of the parameters into classes. Such as depicted in Cartesian coordinates x, y.

6. No side effects

Side effects are a function of the lie, the function name promises only one thing, but he secretly do something.

For example, a checkOrderStatus function, it is evident in check order status, but if it is carried out while checking the status of the modification, it will make people very confused, even when an error in the investigation, will not see such a function point see inside. Better practice is to split it into two functions.

7. Each function is a level of abstraction

That is simple, when we read the code, often using the top-down view of the order, if each function only where their processing logic levels, very comfortable to read and maintain.

Although I think this makes sense, but I did not understand ah.

. . . and many more


So the question is, how to write code that meet these specifications it?

In fact, no need to start writing rules to follow when you write, so very easy to disrupt the idea, and even less effective. After writing can return to carefully polished, dismantling function, change the name, the elimination of duplicate code and so on.

Guess you like

Origin www.cnblogs.com/hujingnb/p/11568593.html