From: Robert Klemme Date: 2005-10-18T21:06:57+09:00 Subject: Re: A couple of questions/statements from a Ruby neohacker Christophe Grandsire wrote: > Selon Robert Klemme : > >> >> - boolean query method identifiers end with a question mark "?" >> ([].empty?) >> > > And? Are there some that aren't? > >> - destructive method identifiers end with an exclamation mark >> > > No! The exclamation mark indicates *dangerous* methods, i.e. methods > that have any kind of dangerous behaviour (modifying the object they > apply to is just one of those dangerous behaviours), *when it's not > clear from their name that they are dangerous.* So if the name of the > method already indicates a dangerous behaviour (a destructive > behaviour for instance), there is *no* need to add an exclamation > mark. > >>>> a=[1,2,3] >> => [1, 2, 3] >>>> b=a.reverse >> => [3, 2, 1] >>>> a >> => [1, 2, 3] >>>> a.reverse! >> => [3, 2, 1] >>>> a >> => [3, 2, 1] >>>> a.delete! 2 >> NoMethodError: undefined method `delete!' for [3, 2, 1]:Array >> from (irb):14 >> >> Ooops! >> >>>> a.delete 2 >> => 2 >>>> a >> => [3, 1] >> >> Ah! >> > > And that is *correct* use of the exclamation mark, because delete, by > definition, can *only* be destructive. Since it is clear from its > name that it is a dangerous method, the ! is unnecessary and unused. > It's not a case of a broken convention, but of you misunderstanding > the convention. > > Now you may disagree with that convention, and argue it should > change. But you can't call that a convention break when it is not. I'm not so sure: the history of this list / ng has shown that this issue has irritated people over and over again. So the convention that was followed in implementing this might be a different one from the publicly observed one. One reason for this might be that the term "dangerous" in this context is much broader and less precise than the term "destructive" (changing the receiver's state). Also "dangerous" is very context sensitive (even IO#puts might be regarded as dangerous in certain situations) which adds to the difficulty. It might be an interesting philosophical debate which of the two conventions is more adhered to, as a convention is a social thing... Regards robert