3

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)

theanine
  • 986
  • 3
  • 10
  • 21
  • 3
    Love how people comment about the fact that there already is a tolower() function. Yes, I know. The point of using this function as an example was to simplify the question. It's interesting that people on these boards like to answer the question that isn't asked. – theanine Nov 18 '10 at 23:48
  • @ComtriS It's also interesting that "people" on this board would rather wallow in their ignorance than to be corrected on something they didn't "ask." – San Jacinto Nov 19 '10 at 14:43
  • @San Jacinto It has nothing to do with ignorance. I needed to type up an example. The code I wrote can obviously be modified for improvement. The point of the example was so people could understand my question. – theanine Nov 20 '10 at 04:10
  • 2
    Makes me want to go on one of your questions and give you an answer you're not looking for. So irritating. – theanine Nov 20 '10 at 04:10
  • @ComtriS Do you think that none of the rest of us have experienced this? I personally assumed your examples were rather contrived than actual code. Do you really need to be so rude to others (I'm referring to your original comment, not to your response to me) who were trying to _help_ you, even thought their assumptions about your ability were wrong? You haven't been around these boards long, and your question was rather simplistic. Can you see _at all_ why people suggested this, much to your dismay? – San Jacinto Nov 23 '10 at 01:56
  • @ComtriS At any rate, I'll be sure not to "annoy" you anymore. Hopefully nobody else does either. – San Jacinto Nov 23 '10 at 02:00
  • @San Jacinto I can see how my original comment could be considered rude (though, I would say yours have been more rude). I can't see how people could misunderstand my original question. Honestly, if the people who made those bad suggestions actually read my question (a whole 2 sentences), they wouldn't have answered that way. Also, I have another username on these boards, but I hadn't used it for a while, so I'd forgotten about it (thus created this one). Yes, my question was simplistic AND it was clear. And I -can't- see at all why anybody would respond in any way other than Bert F did. – theanine Nov 25 '10 at 00:04
  • @ComtriS Fair enough. I agree. Obviously the majority saw it differently than Bert did, though. Not that it matters. It's a free board. Until you pay them, they're free to post what they see fit and you're free to ignore it. I'm just saying that you shouldn't fault people for trying to help you how they see best. For the record, I _agree_ with you. I'm just one of those people who doesn't see the necessity in barking at people who are trying to help you; thus my original comment. – San Jacinto Nov 25 '10 at 01:17

4 Answers4

4

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.

Community
  • 1
  • 1
Bert F
  • 85,407
  • 12
  • 106
  • 123
  • Glad to help. The book is available in print (or at least it used to be) if you want something to have on your bookshelf. I have a copy sitting on mine, though I don't do C/C++ anymore. Still, good ideas aren't often a good idea for just one language ... – Bert F Nov 20 '10 at 20:01
  • May be it is worth to mention that in some programming languages it is possible to use same method names with different signature (arguments). – bkausbk May 15 '12 at 13:10
1

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

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
1

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

pmg
  • 106,608
  • 13
  • 126
  • 198
0
  • 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
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151