A summary of the specification of writing good functions

Source: Internet
Author: User
Tags try catch types of functions

the reading summary of the third chapter of the Code neat way.

Write a good function:

1, Short! 20 rows or so is the upper limit.
2, just do one thing! This is the most important point. The way to determine whether a function has only done one thing is to see if a function can be disassembled again.
3, each function is an abstract level. Top-down Read code: down principle. Each function leads to the next function.
4,switch statements should be buried under the abstract factory. In the face of different types of functions to do different operations with polymorphism to achieve.
5, use a descriptive name. A long and descriptive name is best. Naming style (styles) should be consistent
6, function parameters: The best is 0 parameters, followed by a single parameter, again is two, should avoid the three parameters and above.
7, the form of a single-parameter function. Three types: one is that the function will "ask" some questions about the parameter, such as checking the status. The other is to manipulate the parameter. There is also an event type, with input parameters and no output parameters. For single-argument functions with operation parameters, it is best to use the return value instead of the output parameter. StringBuffer transform (StringBuffer in) is a good way to achieve
8, the identification parameters should be avoided. If you pass a Boolean value to a function!
9, verbs and keywords. Functions and parameters should form verb nouns such as writefield (name) and Assertexpectedequalsactual (expected,actual)
10, no side effects. That is to do only one thing, should not hide the other behavior, if you have to do, the last way is to disclose in the function name.
11, most of the requirements for the output parameters have disappeared, and the class method or return value should be used.
12, separate instructions and inquiries. If the return value of the setxxx instruction is not likely to be the detection parameter is not valid query value. Should be divided into two functions.
13, use exceptions instead of error codes. The error code can be extracted from the main path code to be simplified.
14, pulling away from the code body the Try catch code block, so that the body code to concentrate on things, another function to do possible error handling. Because, error handling is one thing.
15, avoid using the error code enumeration. Consider the case of adding a new error. The alternative is to use exceptions, because exceptions can derive subclasses.
16, avoid repeating the code! Repetition may be the root of all evil in software!
17, how to write the above code: from the rough first draft to the final refinement. No one can write a function that fits all the rules from the start!

A summary of the specification of writing good functions

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.