SO rotate question
Jonathan M Davis
jmdavisprog at gmail.com
Fri Sep 3 05:44:39 PDT 2010
On Friday 03 September 2010 04:35:30 bearophile wrote:
> > canFind isn't really the best possible name, is it?
>
> Some name/APIs of Phobos are not the best possible, they were often
> invented by a single person. I don't know if contains() or isIn() are
> better.
It used to be that all you had was find(), and you had to check whether the
result was empty if you were looking to see whether what you were trying to find
was in the range or not. I pointed out that that was overly verbose and less
efficient in cases where all you cared about was whether the item or items was in
the range rather than getting the range starting at the point where the item was
found. I probably suggested contains() for the new function name, but I'd have
go look at the bug report. In either case, Andrei agreed that it was a good idea
and added the function. However, he named it canFind() - presumably because
that's what you're trying to do. You're seeing whether you could find it with
find(). So, from that perspective, it's a perfect name. However, I don't think
that's really how people think about it. They're looking for whether an element
is in a range, not whether they can find it there. So, from that perspective,
it's a poor name. Personally, I think that contains() would be better, but
canFind() does make sense depending on how you look at it.
Now, canFind() might be going away. I opened a bug report asking for all() and
any() (all() returning whether all of the elements of a range satisfied a
predicate, and and any() returning whether any of them did) and pointed out that
while there is no function in std.algorithm which does all(), the version of
canFind() which takes a predicate and a haystack but no needle is effectively
any(). Andrei thought that any() was better and decided that canFind() should be
replaced with any(). After that, I pointed out that while any() is definitely
better for the case where it's a just a predicate and a haystack, the other
cases would still make more sense as canFind(), and he didn't say anything
further on that bug report, so I don't know what he intends to do, and canFind()
may or may not be going away.
Regardless, contains() is definitely a better name for the other versions of
canFind(), so perhaps a bug report should be opened on the matter. Still, naming
functions is a bit of an art and most function names are debatable as to how
good they are, so you're bound to have functions in any API that aren't named
the way that you think they should be.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list