Esteban A. Maringolo On Sun, Apr 28, 2019 at 6:02 AM ducasse <stepharo@netcourrier.com> wrote:
On 28 Apr 2019, at 10:57, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 28 Apr 2019, at 01:05, Gabriel Cotelli <g.cotelli@gmail.com> wrote:
I tend to use the following idioms: - 'beSomething' idiom for cases modifying the receiver. - 'newWithSomething' or 'copyWithXx' for cases when I want to make it clear that you would get a new instance
I agree with the above
Me too.
- 'asSomething' can or cannot return a new object
Only if the receiver is an instance of Something, otherwise it should always return a new instance.
Yes but :) Take two minutes to look at the Color API. Because I use this when I design my class.
Now Color has also a nice API such Color red darker
Most of the time the methods are returning a new object. So using as would be strange
Color red asDarker. Color read asMuchLighter
(*) I wouldn't use #asOpaque or #asDarker unless there is an Opaque or Darker class in the system. To me "as" prefix implies "casting".
Why not I would like to address first all the methods that look like doing side effect but not doing them.
#darker and #muchLighter are good enough selectors, they don't imply returning a new object, but neither does Date>>#addDays: . The #new and #copy prefixes are too verbose in most cases, and I don't know a good "naming practice" related with this (selectors that return a new instance), so if there is consistency in the class that most selectors return a new instance even "offending" methods such as DateAndTime>>#offset: are not harmful if consistent. E.g. in collection I'd expect #sort to sort receiver, and #sorted to return a new instance. So the rule might be in the conjugation of the the selector. So using the Color selector I would do: Color>>#darken (verb) or Color>>#beDarker (verb) "Makes receiver darker." Color>>#darker (adjective) "Returns a darker copy of receiver." Color>>#beOpaque (verb) or Color>>opacity: (noun) "Makes receiver darker." Color>>#opaque (adjective) "Returns an opaque copy of receiver." Regards,