Let’s say I have an Image class and I want to provide some operations on the image, like scaling, rotating etc.
I want to provide 2 types of functions for each operation. One that modifies the object and other that does not.
In Ruby, there are functions that end in ! and indicate that this one is going to modify the argument.
Since this is not allowed in C++/Java, what would be the best naming convention.
For e.g. how would you name the Mutating and non-mutating versions of img.scale()?
Thanks
One option would be to use
Scalefor the mutating version andScaleCopyfor the non-mutating version, since it returns a copy of the original with the operation performed on the copy.Another option would be to make the non-mutating version a non-member function. For example,
I’d lean towards the non-member approach since it reduces the number of member functions in the class. To quote Herb Sutter’s Monoliths Unstrung, “where possible, prefer writing functions as nonmember nonfriends.”