Timeline for Checking for emptiness of collections with (in)equalities - what's the best practice?
Current License: CC BY-SA 3.0
10 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 25, 2012 at 13:04 | comment | added | Vilx- |
Just to note - IsEmpty() is an elegant solution indeed, but it's not so elegant if you're using a standard data structure (like an array) which does not have this method. You can, of course, write one yourself, but the code becomes much longer than the simple .Length==0 . Arguably, that also hurts readability.
|
|
May 25, 2012 at 12:56 | comment | added | Vilx- |
@leftaroundabout - Obviously, I was talking about the typical everyday cases when obtaining .Length is a O(1) operation. If special conditions apply to getting it, then this is all irrelevant.
|
|
May 25, 2012 at 12:15 | comment | added | leftaroundabout |
For some containers an IsEmpty approach is not just faster than .Length == – the latter may actually be impossible! For instance in the case of lazy lists, which can be infinite.
|
|
May 25, 2012 at 11:28 | history | edited | Mert Akcakaya | CC BY-SA 3.0 |
deleted 52 characters in body
|
May 25, 2012 at 11:27 | comment | added | Mert Akcakaya | Actually you are right, sorry for calling that trivial. | |
May 25, 2012 at 11:26 | comment | added | Aidan Cully |
I'm not sure I agree that this is such a trivial thing... IsEmpty() is a generally preferred mechanism because it is simultaneously closer to how we think about the condition we're trying to describe, and easily executed by computer. We come to understand this sort of thing best by factoring out complicated domain logic, so that we're left with trivial problems like the OP described. Further, it can be important to have a consistent style, and it's best to have that style be based upon reason.
|
|
May 25, 2012 at 11:16 | vote | accept | Vilx- | ||
May 25, 2012 at 11:15 | comment | added | Benjamin Bannier |
+1 for IsEmpty() . This one does make it really clear. Plus it can be faster than getting the size for some containers.
|
|
May 25, 2012 at 11:09 | comment | added | Vilx- | Oh, I'm not thinking hard. It's just a thought that struck me as I was doing work today, and since I couldn't decide on my own in a few moments, I posted it here. :) | |
May 25, 2012 at 11:05 | history | answered | Mert Akcakaya | CC BY-SA 3.0 |