True, it is still in the language for a reason. But you should group your code if it belongs together. So if there is code which should be executed together, move it to a function. Not for the sake of having an early return, but because it should be executed together and as a bonus you can give it a fitting name. Or you could keep it if it is not to big.
The code still 'follows' an if statement if you use a guard statement; it's just at the top and you get to reduce indentation and brain fatigue. A lot of style guides suggest to do this by default where possible with rare exceptions rather than the other way around.
I speak Italian and I find if twice as funny. First, "ano" means anus. Secondly, if you remove the space between "var" and "ano" you get "varano", which means monitor lizard and it is glorious
Any is potentially going to iterate the enumerable in some cases in C# so it might iterate twice. This is not in all cases though, like if it's a list or arrays. But if this was a Queryable then it might even execute 2 SQL queries in the worst case.
Any will return true whenever condition is met on first object in iterable so no, this is not going to iterate whole collection. If this would check if collection is null then it might make sense as trying to iterate it might throw exception. I might even say that this Any method (if implemented correctly) should return false when iterable is in fact null
That's not Microsoft linq. That's .net API linq. But at this point, if you indeed rebuild your own linq in real life, what's the point to give your opinion here, you obviously are a lonly programmer that use it's own language 🙄. My point is that this community is low brain. "What if it's not standard linq", "what if there is a else", "what if first enumeration change the content of the collection", ... It's obviously standard c# / linq. It's obvious there is no else block. This is just bad/dumb code and that's why I put it here.
Any will return true whenever condition is met on first object in iterable so no, this is not going to iterate whole collection
Depends which "collection" you're talking about, it can end up iterating a base collection when used with other Linq methods:
var list = /* some huge list */;
var enumerable = list.Skip(list.Length - 1);
if(enumerable.Any()) { // iterates through all of list
foreach(var elem in enumerable) { //and again
// etc...
}
}
(Yes I'm aware this is a contrived example but hopefully it demonstrates the point)
Honestly not that bad if this is using lazy collections like entity framework, etc…
If you are using a lazy load enumeration on a db, you should almost always wrap w/ Any, because this will use lazy queries to figure out if it should start returning the entire set.
🤦♂️ If there's no data, ask for the entire set doesn't take that much resources : the set is empty! If there's data the Any call as a cost and is useless.
we can only see the code that's in front of us so that's just a possibility!
but yeah, obviously if there's some behaviour they want to implement based on the collection being empty, there's a use case. but right now, it's all imaginary.
This depends entirely on the situation. There are loads of situations where this would completely unnecessary or even bad. Especially if you have nullable references enabled.
Pabs or anomalies lists could be null if not initialized correctly. The if should be a null condition check
I like how you can tell the rest of the code not shown here, and can see they they don't have any of the constructs C# provides to avoid null references...
71
u/manafount Dec 14 '22
Pro tip: make traversing your collections O(1) by only ever storing 0 or 1 value in them.
150
u/dliwespf Dec 14 '22
The if is valid if there is code trailing the foreach loop.
17
u/GreatArtificeAion Dec 14 '22
I speak Italian and I find if twice as funny. First, "ano" means anus. Secondly, if you remove the space between "var" and "ano" you get "varano", which means monitor lizard and it is glorious
55
u/mprz Dec 14 '22
Can anyone explain what's the issue with this code?
2
u/[deleted] Dec 14 '22
There is no
elsenor code after the loop 🦧I was too lazy to do the photo montage 💤
Those are
Listbehind the scene (.ToList()everywhere too...)16
u/[deleted] Dec 14 '22
If there’s nothing in the collection, it doesn’t need to loop so the check is pointless.
7
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 14 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
5
u/Mindless_Work Dec 14 '22
Pabs or anomalies lists could be null if not initialized correctly. The if should be a null condition check on both so the foreach doesn’t break