简体   繁体   English

对于分配内存的函数,您遵循哪些命名约定?

[英]What naming conventions do you follow with functions that allocate memory?

So here are two functions that do almost the same thing.所以这里有两个功能几乎做同样的事情。

How would you name each one, if you had to include both in your project?如果您必须将两者都包含在您的项目中,您会如何命名每一个?

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
}

EDIT: I modified the above example for code-correctness (sheesh)编辑:我修改了上面的代码正确性示例(sheesh)

I really like the Taligent Coding Standards , especially the naming conventions .我真的很喜欢Taligent 编码标准,尤其是命名约定 The convention about using special names for copy, create, and adopt routines may apply here:关于使用特殊名称进行复制、创建和采用例程的约定可能适用于此处:

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

Use special names for copy, create, and adopt routines使用特殊名称复制、创建和采用例程

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...创建调用者必须删除的新对象的例程以 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().复制现有对象的例程,其中调用者必须删除副本,以 Copy 开头... 复制对象的成员函数应该是 Copy()。

Routines that abandon an object and pass deletion responsibility to the caller begin with Orphan...放弃对象并将删除责任传递给调用者的例程以 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.)接受调用者分配的对象并负责最终删除它的例程以采用...开始(这种编程风格容易出错;如果可能,避免它。)

Adopting routines that cannot follow the previous rule (such as constructors) start the name of the argument with adopt...采用不能遵循先前规则的例程(例如构造函数),参数名称以采用开头...

[Contents] [Previous] [Next] Click the icon to mail questions or corrections about this material to Taligent personnel. [内容] [上一页] [下一页] 单击图标将有关此材料的问题或更正发送给 Taligent 人员。 Copyright©1995 Taligent,Inc.版权所有©1995 Taligent, Inc. All rights reserved.版权所有。

Following this, the first method could be called createLowerCaseStr() or copyAsLowercaseStr() .在此之后,第一个方法可以称为createLowerCaseStr()copyAsLowercaseStr() The leading keyword create and copy indicate new memory that must be managed by caller.前导关键字createcopy表示必须由调用者管理的新内存。

Personally, I would call the 2nd function transformIntoLowercase() or mutateIntoLowercase() , but I tend toward lengthy names.就个人而言,我会调用第二个函数transformIntoLowercase()mutateIntoLowercase() ,但我倾向于使用冗长的名称。 While not specified by Taligent, I see the leading keywords transform and mutate as hints of transformation done in-place.虽然 Taligent 未指定,但我将主要关键字transformmutate视为就地完成转换的提示。

If strToLowerInPlace returned 'str' then you could simply write new_s = strToLowerInPlace(strdup(s)) .如果strToLowerInPlace返回 '​​str' 那么你可以简单地写new_s = strToLowerInPlace(strdup(s)) Thus I'd drop "InPlace" and assume everything was in-place and the caller could duplicate as needed.因此,我会放弃“InPlace”并假设一切都已就位,并且调用者可以根据需要进行复制。

(And if you are going to have two functions, at least make the copying one call the in-place one!) (如果你将两个功能,至少使复制一个呼叫就地一个!)

1st: char *copylo(char *dst, const char *src);第一个: char *copylo(char *dst, const char *src); (no allocations!) (没有分配!)
2nd: char *lowerize(char *data);第二个: char *lowerize(char *data);

  • there is a function called tolower() no need to do crazy testing and hard-coded transformation有一个叫做tolower()的函数不需要做疯狂的测试和硬编码转换
  • if you already have a function making in-place lowercasing, why are you reimplementing the code in the not-in-place version?如果您已经有一个实现就地小写的函数,为什么要在非就地版本中重新实现代码?
  • the naming is OK命名没问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM