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)
If
strToLowerInPlace
returned 'str' then you could simply writenew_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!)
tolower()
no need to do crazy testing and hard-coded transformation1st:
char *copylo(char *dst, const char *src);
(no allocations!)2nd:
char *lowerize(char *data);
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
Following this, the first method could be called
createLowerCaseStr()
orcopyAsLowercaseStr()
. The leading keywordcreate
andcopy
indicate new memory that must be managed by caller.Personally, I would call the 2nd function
transformIntoLowercase()
ormutateIntoLowercase()
, but I tend toward lengthy names. While not specified by Taligent, I see the leading keywordstransform
andmutate
as hints of transformation done in-place.