你与分配内存功能遵循什么命名约定? [英] What naming conventions do you follow with functions that allocate memory?

查看:102
本文介绍了你与分配内存功能遵循什么命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这里有两个函数做几乎同样的事情。

So here are two functions that do almost the same thing.

你会如何命名每一个,如果你要包括在您的项目?

void strToLower1(char* str)
{
    int len = strlen(str);

    int i;
    for (i=0; i<len; i++)
        str[i] = tolower(str[i]);
}

char* strToLower2(const char* inputStr)
{
    char* str = strdup(inputStr);
    strToLower1(str);
    return str;   // must be freed
}

编辑:我修改了上面的例子中为code-正确性(啧)

I modified the above example for code-correctness (sheesh)

推荐答案

我真的很喜欢的 Taligent公司编码标准,尤其是的命名约定的。了解使用拷贝特殊名称的约定,创建和采用程序可以在这里申请:

I really like the Taligent Coding Standards, especially the naming conventions. The convention about using special names for copy, create, and adopt routines may apply here:

http://pcroot.cern.ch/TaligentDocs/TaligentOnline/DocumentRoot/1.0/Docs/books/WM/WM_67.html#0

使用特殊的名字复制,创建,
  并采用程序

该例程分配,管理或
  承担存储有责任
  特殊的名字,恪守
  以下准则:

Routines that allocate, manage, or take responsibility for storage have special names and abide by the following guidelines:

该例程
  使一个新的对象,调用者必须
  删除与创建...开头

Routines that make a new object that the caller must delete begin with Create...

这是复制现有对象例程,
  其中,主叫方必须删除副本,
  开始复制...的成员函数
  该副本的对象应该是
  复制()。

Routines that copy an existing object, where the caller must delete the copy, begin with Copy... A member function that copies an object should be Copy().

这放弃的对象和例程
  通过缺失,责任到
  来电者与孤儿开始...

Routines that abandon an object and pass deletion responsibility to the caller begin with Orphan...

这是接受对象的例程
  主叫方分配,并采取
  为最终删除的责任
  它采用...开始(这种风格
  编程是很容易出错;躲开它
  如果可能的话)。

Routines that accept an object the caller has allocated and take responsibility for eventually deleting it begin with Adopt... (This style of programming is error prone; avoid it if possible.)

采用不能遵循惯例
  在previous规则(如
  构造函数)开始的名字
  参数与采用...

Adopting routines that cannot follow the previous rule (such as constructors) start the name of the argument with adopt...

[目录] [previous] [下一页]点击
  图标邮寄疑问或
  有关这种材料的更正
  Taligent公司工作人员。版权所有©1995
  Taligent公司,公司。保留所有权利。

[Contents] [Previous] [Next] Click the icon to mail questions or corrections about this material to Taligent personnel. Copyright©1995 Taligent,Inc. All rights reserved.

在此之后,第一种方法可称为 createLowerCaseStr() copyAsLowercaseStr()。领先的关键字创建复制表示必须由主叫方进行管理新的内存。

Following this, the first method could be called createLowerCaseStr() or copyAsLowercaseStr(). The leading keywordcreate and copy indicate new memory that must be managed by caller.

就个人而言,我会叫的第二功能 transformIntoLowercase() mutateIntoLowercase(),但是我倾向于对冗长名。虽然没有Taligent公司规定,我看到了领先的关键字变换突变为就地进行改造提示。

Personally, I would call the 2nd function transformIntoLowercase() or mutateIntoLowercase(), but I tend toward lengthy names. While not specified by Taligent, I see the leading keywords transform and mutate as hints of transformation done in-place.

这篇关于你与分配内存功能遵循什么命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆