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

后端 未结 4 1329
夕颜
夕颜 2021-01-21 00:37

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 s         


        
相关标签:
4条回答
  • 2021-01-21 01:15

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

    0 讨论(0)
  • 2021-01-21 01:18
    • 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
    0 讨论(0)
  • 2021-01-21 01:25

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

    0 讨论(0)
  • 2021-01-21 01:30

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

    0 讨论(0)
提交回复
热议问题