Discussion on the programming style 4

Source: Internet
Author: User
Tags goto

Iv. the robustness of the program:

1, the function return value specification:

For the return position of the function, try to remain single, that is, a function to achieve only one return position. (Single import and export).

We ask you to unify the return value of the function, and all the return values of the functions will be returned in encoded form.

For example, the encoding is defined as follows:

#define CM_POINT_IS_NULL CMMAKEHR(0X200)
:
:
建议函数实现如下:
long 函数名(参数,……)
{
long lResult; //保持错误号
lResult=CM_OK;
//如果参数有错误则返回错误号
if(参数==NULL)
{
lResult=CM_POINT_IS_NULL;
goto END;
}
……
END:
return lResult;
}

2, on the application of goto:

For the application of goto statements, we require as little as possible to use goto statements. The need to use the local requirements can only be transferred backwards.

3. Resource variable processing (resource variable refers to the variable that consumes system resources):

The resource variable must be assigned an initial value. The assigned resource must be released immediately after it has been exhausted and reassigned.

Cases:

long * plAllocMem;//定义一个分配内存的变量。
plAllocMem=(long*)calloc(40, sizeof( long ));//分配一段内存。
//处理分配内存错误
if(plAllocMem==NULL)
{
lResult=CM_MEM_ALLOC_FAILED;
goto END;
}
……
使用内存
……
//释放资源变量,并重新赋值。
if(pAllocMem!=NULL)
{
free(plAllocMem);
pAllocMem=NULL;
}

4, to the complex condition judgment, in order to the procedure readability, should use the bracket as far as possible.

Example: if ((szfilename!=null) && (lcount>=0)) | | (bisreaded==true))

V. Portability:

1, high-quality Code requirements can be cross-platform, so our code should take into account the support of different platforms, especially for Windows98 and WindowsNT.

2, because the C language transplant is relatively good, so the algorithm function requirements with C code, can not use C + + code.

3, for different hardware and software functions to do different processing.

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.