On Thursday, January 20, 2011 05:48:12 so wrote: > > auto newStr = replace(str, "hello", "world"); > > replaceInPlace(newStr, "world", "hello"); > > > > it's quite clear that the first one returns a value and the the second > > one does > > it in place. Whereas if you have > > > > auto newStr = replaceCopy(str, "hello", "world"); > > replace(newStr, "world", "hello"); > > > > the first one is clear, but the second one is only clear because seeing > > the first > > one makes it obvious that the second one must be doing something > > different. > > I don't understand how the first two are clear and the last two are not so. > Where both have the name "replace" for different things, and replace to me > means "replace in place". > With this in hand, how is the first "replace" is quite clear? > > I am sure this is the case for many people. Problem is the naming here. > If you have named it something like "replaced" and return a copy, it would > be obvious and clear. > Here, aren't you just dictating functional language rules to a > multi-paradigm language, implicitly? > In a fully functional language "replace(something)" might mean "replace > and give me a copy", but it is not what we have.
replace is clearer in the first case, because you're getting the return value. If you don't get the return value, then it's not immediately clear whether it's replacing "world" with "hello" in the return value or whether the function is void and "world" is being replaced in the original string (though they fact that we're dealing with strings here means that it _can't_ alter the original string - it's more of a question when dealing with arrays with mutable elements). Also, replaced would just be downright confusing to me, since it's not a verb. I'd expect it to be some sort of boolean test for whether something had been replaced, though that doesn't make a whole lot of sense in the context. I expect functions to be verbs unless checking state. Now, as I understdand it, python uses past participles such as replaced and sorted, but having never programmed in python, I'm not particularly familiar with that naming scheme and it wouild really throw me off at first. Regardless, I don't see anything wrong with naming functions in a manner that implies that a functional style is the default - particularly when we're talking about arrays, and they pretty much _have_ to be used in a functional style, because their elements are immutable. Andrei is essentially asking us whether the default behavior of an array function should typically be to return the changed value or to change it in place, with the longer name going to the function which has the other behavior. And since strings _have_ to be copied/sliced, and strings are generally going to be the most common type of array used, then it would make sense to make the default behavior be copying/slicing, making the functions which alter arrays in place have InPlace in their name. - Jonathan M Davis