David Haley said:
Twisol, your cases are:
1. No modifications, or modifications uninteresting
2. Modifications are always interesting
You're leaving out the case I pointed out: modifications are only sometimes interesting, and you don't want to have to go through any hassle when you don't care about them.
Using your suggestions, if you force the function to take a char* on the assumption that the modifications are always interesting, then callers who don't care must do extra work. A simple overloaded wrapper fixes this completely.
You also incorrectly equate my suggestion and your case 2. My suggestion is that you overload the function so that it's easy to not care, but in those cases where you do care, and all you have is a const char*, only then must you do any extra work.
In other words, my suggestion minimizes work necessary in all cases.
There are four cases:
1. you care and you have a char*, and everything works
2. you don't care and you have a char*, and everything works
3. you care and you have a const char*, you need to do some work
4. you don't care and you have a const char*, and everything works
Your suggestion, however, needlessly forces extra work in case #4.
My definition of "interesting" in this case is more like "externally visible". If it modifies the source string at all, it should be "interesting". Correct me if I'm wrong, but your case 2 leaves this fact out, meaning the source string might be modified and you might not know about it.
Your cases are in regards to the user; mine are more in regards to the function itself.
1. If the function modifies the string and it should be publically visible, use char*.
2. If the function modifies the string and it shouldn't be publically visible, use const char*, and use strdup() internally.
3. If the function doesn't modify the string, use const char*.
Looking at how the user would use them:
1a. If you want the modifications, everything works.
1b. If you don't want the modifications, strdup.
2. Everything works.
3. Everything works.
The only work the user has to do is in the case of 1a, where you have to prevent the function from modifying a string that you want to preserve. This is logical. |