Function writing rules (1)

Source: Internet
Author: User

EveryProgramBoth members Write Functions and write many functions. However, writing a function is not that easy.

1. Separation of scheduling and implementation

The first thing to write a function is to locate the category of the function. Functions are classified into scheduling functions and implementation functions. The implementation function is a function that provides specific task implementation. The implementation function requires no dependency on any external environment. The scheduling function is a function that directly responds to user requests. It recognizes or has a large amount of external environment resources, and call the implementation function to respond to user requests. The separation of scheduling functions and implementation functions meets the principle of single responsibility, that is, the non-implementation of scheduling and the non-implementation of scheduling.

2. Implementation is independent of the external environment

The implementation function must not depend on any external environment, such as global variables, global functions, or single-piece mode instances. The implementation function can only depend on the input parameters when the function is called. Considering a Division task, the Division implementation function must consider the error handling problem when the division number is 0. Generally, there are three Processing Methods: one is to cause an exception, and the other is to call a globally defined error processing function, the third is to provide an error handling callback function (or a function like this) when calling a function ). For the first method, the calling programmer may respond or avoid exceptions, resulting in unstable program behavior. In fact, it is equivalent to implicitly relying on an external catch.CodeAnd the second method is to display the global function. Both methods violate the principle that implementation does not depend on the external environment. There is only a third way to satisfy this principle. At the same time, the caller cannot be an ostrich again. He must display the error handling problem when the Division is 0, and the quality of the program is easily guaranteed. Therefore, the prototype of a standard Division implementation function should be: Float divideimpl (float a, float B, error_cb perrorcb)

3. Scheduling cannot provide implementation

A scheduling function cannot provide function implementation. On the contrary, it must be delegated to the implementation function to complete specific functions. Scheduling functions generally possess or understand a large amount of environment information. This dependency makes the scheduling function very stable and may need to be reconstructed at any time. The business implementation is usually relatively stable and generally remains unchanged for 50 years, so it cannot be implemented in scheduling functions. The scheduling function requires that all the environment parameters required to implement the function be prepared, and finally the implementation function is called to satisfy the user's request. The scheduling function can call other scheduling functions to help complete tasks. However, other scheduling functions that can be scheduled must be at the same level and lower than the current scheduling function. If the general manager can schedule different departments and departments, the general manager is not allowed to schedule different teams.

4. query and command separation

The query function returns results without changing the status. The command changes the status and does not return results. For Network Access exceptions, see 'HTTP: // blog.csdn.net/hlqyq/archive/2005/12/02/541912.aspx'

 

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.