What naming conventions do you follow with functio

2019-02-26 16:14发布

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)

4条回答
Ridiculous、
2楼-- · 2019-02-26 16:21

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

查看更多
地球回转人心会变
3楼-- · 2019-02-26 16:31
  • 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
查看更多
爷的心禁止访问
4楼-- · 2019-02-26 16:35

1st: char *copylo(char *dst, const char *src); (no allocations!)
2nd: char *lowerize(char *data);

查看更多
老娘就宠你
5楼-- · 2019-02-26 16:39

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

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

查看更多
登录 后发表回答