On Tuesday, 14 June 2016 09:36:54 UTC+3, Jorgen Grahn wrote:
> On Sun, 2016-06-12, Alf P. Steinbach wrote:
> ...
> > With all standard containers (unless I'm mistaken, in which case, with
> > most all standard containers) you can use the misleadingly named member
> > function `empty` to check whether it's empty. Contrary to what the name
> > indicates, it does not empty the collection. It just checks.
>
> It's a matter of point of view. I don't find it misleading but rather
> natural ... or at least in line with the standard library naming
> conventions in general.
Hmm. Are you sure about naming "conventions" of standard library?
What is correct by those "conventions"? Is it 'vector::get_allocator()' or
'vector::allocator()'? Is it 'vector::capacity()' or 'vector::get_capacity()'?
Feels there are no such "conventions".
We typically expect functions to be requests of activities in imperative
programming language therefore usage of word that is both verb and
adjective ('empty') for function name in meaning of that adjective is
misleading. Frank way of naming such function would be 'is_empty'.
>
> ...
> > To get more readable code you can define an `is_empty` function for
> > vectors, like this:
> >
> > template< class Item >
> > auto is_empty( std::vector<Item> const& v )
> > -> bool
> > { return v.empty(); }
>
> We've had empty() for around 25 years; I don't think it's helpful to
> introduce an alias. Not if more than one person sees the code, anyway.
That is entirely another question if it is worth to try to repair such
minor legacy cosmetic defects in standard library or not. I do not think
that it is.
>
> This on the other hand is one thing I sometimes wish for, since I
> tend to want to negate the empty() check:
>
> if (!foobar.empty()) ... // hard to spot the negation
That depends on how your code editor colors the operators.
>
> if (not foobar.empty()) ... // few use the 'not' alias
These aliases are made like inbuilt macros so one can declare rvalue
reference 'foo' like that:
auto and foo = get_foo();
>
> if (foobar.non_empty()) ... // better
But also likely not worth it. Lot of developers (possibly majority)
write one of those instead:
if (foobar.size()) ... // the reality
if (0 < foobar.size()) ... // the reality
if (foobar.size() != 0) ... // the reality
I suspect that it is so (at least partially) because 'empty' is such a
bad name. ;-)