简体   繁体   中英

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)

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:

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...

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.)

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. Copyright©1995 Taligent,Inc. All rights reserved.

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

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.

If strToLowerInPlace returned 'str' then you could simply write new_s = strToLowerInPlace(strdup(s)) . Thus I'd drop "InPlace" and assume everything was in-place and the caller could duplicate as needed.

(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); (no allocations!)
2nd: char *lowerize(char *data);

  • there is a function called tolower() no need to do crazy testing and hard-coded transformation
  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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