What about putting array.empty in object.d?
Daniel Murphy
yebblies at nospamgmail.com
Wed Mar 21 16:39:23 PDT 2012
"Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message
news:mailman.985.1332364578.4860.digitalmars-d at puremagic.com...
> I know that. Much point is that length == 0 is a bad thing to do in
> general,
> because it's ineffecient with some containers. The language itself is
> pretty
> much irrelevant as far as that goes. As such, I'd argue in pretty much
> _any_
> language that using length == 0 instead of empty is _not_ a good habit to
> be
> in. Doing it with arrays will make it much more likely that you'll end up
> doing it with containers without thinking about it. On the other hand, if
> you're in the habit of _always_ using empty rather than length == 0, then
> you
> don't have the problem.
Yes, .length is inefficient on some containers... but so is indexing. That
doesn't mean using indexing on arrays is a bad habit.
If you're writing general code for ranges, you are going to have to use the
range interface. But when you're writing code for arrays only, you can take
advantage of the fact that indexing is O(1), length is O(1), slicing is O(1)
etc.
I'm not even advocating using arr.length == 0 to check for empty. Just use
`if (!arr)`.
More information about the Digitalmars-d
mailing list