Guide to High quality C++/C Programming-6th Chapter-Functional Design (1)

Source: Internet
Author: User
Tags printf reference

function is the basic function unit of C++/C program, its importance is self-evident. The subtle drawbacks of a function design can easily cause the function to be used incorrectly, so it is not enough to make the function correct. This chapter focuses on the interface design of functions and some rules of internal implementation. The two elements of a function interface are parameters and return values. In C, the parameters of a function and the return value are passed in two ways: value passing (pass by value) and pointer passing (pass by pointer). The C + + language has multiple reference passes (pass by reference). Because the nature of the reference pass like a pointer pass, but the use of the way is like value transfer, beginners often puzzled, easy to cause confusion, please read section 6.6 "Reference and pointer comparison."

6.1 Rules for parameters
L "Rule 6-1-1" the writing of the parameter should be complete, do not covet the convenience of write parameter type and omit parameter name. If the function has no arguments, it is filled with void.

For example:

void SetValue (int width, int height); A good style

void SetValue (int, int); Bad style.

float GetValue (void); A good style

float GetValue (); Bad style.

L "rule 6-1-2" parameters should be named appropriately and in a reasonable order.

For example, write a string copy function Stringcopy, which has two parameters. If the parameter name is STR1 and str2, for example

void Stringcopy (char *str1, char *str2);

Then it's hard to figure out whether to copy the str1 to the str2 or just the reverse.

The parameter names can be made more meaningful, such as strsource and strdestination. This can be seen from the name of the strsource should be copied to the strdestination.

There is also a question, these two parameters that one should be the first one should be in the back? The order of the parameters follows the programmer's habits. Generally, you should put the destination parameter in front and the source parameter in the back.

If you declare a function as:

void Stringcopy (char *strsource, char *strdestination);

When used, others may write the following form without hesitation:

Char str[20];

Stringcopy (str, "Hello World"); Parameter order reversed

L "Rule 6-1-3" if the parameter is a pointer and is for input only, you should add a const before the type to prevent the pointer from being accidentally modified in the function body.

For example:

void Stringcopy (char *strdestination,const char *strsource);

L "Rule 6-1-4" if an input parameter passes an object as a value, it is advisable to use the "Const &" method to pass it, which eliminates the construction and destructor of the temporary object, thereby increasing efficiency.

2 "recommendation 6-1-1" to avoid the function has too many parameters, the number of parameters as far as possible to control within 5. If there are too many parameters, it is easy to mistake the parameter type or order in use.

2 "recommendation 6-1-2" Try not to use type and number of uncertain parameters.

The C standard library function printf is a typical representation of an indeterminate parameter, and its prototype is:

int printf (const chat *format[, argument] ...);

This style of function loses a rigorous type of security check at compile time.

6.2 Rules for returning values
L "rule 6-2-1" do not omit the type of the return value.

C language, all without the type description of the function, all automatically according to the whole type processing. There is no benefit in doing this, but it is easily misunderstood as void type.

The C + + language has a very strict type of security check that does not allow this to occur. As C + + programs can invoke the C function, in order to avoid confusion, specify that any c++/C function must have a type. If the function does not return a value, it should be declared as a void type.

The l "rule 6-2-2" function name is not semantically conflicting with the return value type.

The typical representative of violating this rule is the C standard library function GetChar.

For example:

char c;

c = GetChar ();

if (c = EOF)

...

It is natural to declare variable C as a char type according to the GetChar name. Unfortunately, GetChar is not a char type, but an int type with the following prototype:

int GetChar (void);

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.