Design of functions

Source: Internet
Author: User
Tags coding standards


How do you understand the process orientation? What if I understand the concept of a function?
As we all know, C language is process-oriented. How can we embody these processes in the process of writing code using C? In my understanding, we use functions to reflect various processes. A complete C program completes all its work through one function call after another. If the function design is poor, wait for the person who maintains it to scold me.
The function design is related to whether the program can be correctly understood for further maintenance. One typical defect is that a function carries too many tasks (I have seen a function that is bloated with thousands of rows in a project. Obviously, this is an arduous task that ordinary people cannot maintain .)
Many classic textbooks and enterprise coding standards once again explain that a function can only complete one or two tasks, and the best function is only one thing. A good function encapsulation gives us a clear picture of what this function is used for and what input and output it requires.

The function uses parameters for input and return values for output. Is there more parameters, the better? I don't agree. The more parameters, the more things that need to be done by the function, and the less compact it is. In addition, we often make mistakes in the parameter location, but bring an error that is not easy to find. If this problem stops the compilation warning completely, we will sacrifice your holiday for debugging.
It is better to control two to three parameters. There are too many parameters, which are inconvenient to use. It is easy to use and wrong without instructions (some guys will always like to make SPAM code without leaving any comments ). If there are too many packages, just package a struct with an address.
For example, the select function,
Before using such functions, I advise you to carefully read their long instructions and a series of macro definitions. Because you have directly copied other people's code, it may be a bunch of errors.
Int select (int nfds, fd_set * readfds, fd_set * writefds,
Fd_set * required TFDs, struct timeval * timeout );

Int pselect (int nfds, fd_set * readfds, fd_set * writefds,
Fd_set * contains TFDs, const struct timespec * timeout,
Const sigset_t * sigmask );
The parameters of most standard library functions are as follows, which are easy to understand and easy to use.
The function body should be fully displayed on a single screen rather than pages. These are pleasing function encapsulation.
Do not make your functions stinking and long.
A good function encapsulation is like a clean and tidy beautiful girl standing in front of you, which makes you bored. A bad design is like a dirty and ugly woman in your eye, make you feel desperate for the world.

Finally, I would like to give you a poor negative example. Good luck!
Void vMErrReport (
SINT4 vlSrvId,
SINT2 vnErrLevel,
SINT8 vnErrPos,
Const SCHAR * vsapModuleName,
Const SCHAR * vsapFileName,
Const SCHAR * vsapErrTitle,
Const SCHAR * vsapErrDetail,
SINT4 vlReturnVal,
SINT4 vlSysError)

 

Related Article

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.