Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the function design principles that are easy to be ignored in C language?

Shulou Source: shulou.com Published: 2022-05-31 20:01:19 09月13日 Update

This article mainly introduces the relevant knowledge of what are the function design principles that are easy to be ignored in C language, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on function design principles that are easy to be ignored in C language. Let's take a look at it.

Principle of function design

Function should be an independent functional module in a sense.

The function name should reflect the function of the function to some extent.

The function parameter name should be able to reflect the meaning of the parameter.

Try to avoid using global variables in functions

When function parameters should not be modified within the function body, a const declaration should be added

If the parameter is a pointer and is used only as an input parameter, the const declaration should be added as follows:

The type of return value cannot be omitted

If the function does not return a value, it should be declared as void

Type to check the validity of parameters

It is particularly important to check pointer parameters.

Do not return a pointer to Stack memory

The function body in the stack is automatically released at the end of the stack.

The size of the function body should be small and be controlled within 80 lines of code as much as possible.

The same input corresponds to the same output, avoiding the function with "memory" function.

Avoid having too many parameters in the function, and try to control the number of parameters within 4.

Sometimes functions do not need return values, but to increase flexibility, such as supporting chained expressions, return values can be attached

There is no semantic conflict between the function name and the return value type.

< ver2 * Returns 0 if ver1 = ver2 * Returns 1 if ver1 >

Ver2 * / int versionCmp (char* ver1, char* ver2) {char* dot1; char* dot2; int num1; int num2; dot1 = strchr (ver1,'.'); dot2 = strchr (ver2,'.'); num1 = atoi (ver1); num2 = atoi (ver2); if (num1 > num2) return 1; if (num1

< num2) return -1; if (dot1 && !dot2) /* x.y >

X * / return 1; if (! dot1 & & dot2) / * x < x.y * / return-1; if (! dot1 & &! dot2) / * x = = x * / return 0; return versionCmp ((char*) (dot1 + 1), (char*) (dot2 + 1)) } # endif / * AIX * / this is the end of the article on "what are the function design principles that are easy to be ignored in C language". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what are the function design principles that are easy to be ignored in C language". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Function parameter principle design language function pointer knowledge type same code memory content meaning article control check input excellent effective Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei MariaDB Shulou Technology NVidia