As I always repeat, this good or bad practice depends on context. In this case without knowing all the requirements of what you are doing you cannot answer. Both are correct and acceptable.
If you make the second to be shorter, funnier, in theory save typing, then it shouldn’t, it doesn’t make sense. Especially in C# that privileges the compilation time.
The second has run-time processing to solve something that could have been solved at build time. Let the Contains()
for cases where you don’t know what the data list is, or it can be changed frequently, or if it’s too big, which doesn’t seem even close to being the case.
If C# had any optimization that would ensure that the Contains()
were linearized And then unrolled, which would probably generate roughly the same code as the first one, so I could do it. Has language that has an operator for this which generates optimized code.
But if you don’t have to worry about running time, you can do it, although I don’t recommend it.
But you have to look at the context, if it makes sense to produce a list, because this is what you’re manipulating, which doesn’t seem to be the case, make a list. If it’s not a list, the most readable is the first. If it’s a list, I don’t know if it should be created in if
, she probably should be somewhere else. Do what you want to do best, always.
I think this use of contains would be just by typing really, really good answer =]
– Rovann Linhalis
Late for the original question, but I would like to complement @Maniero’s reply. While creating an array at runtime and calling a method
Contains
potentially linear is not a good idea, there are structures likeHashSet
in time of lookup constant amortized. In particular, I had much success replacing a complicated chain of ifs with dozens of conditions for aImmutableHashSet
static and a call toContains
. Not only did the code get cleaner, the execution also got faster (in a trade-off for a little memory).– Anthony Accioly
It’s true, if you have too many items it starts to make up for it like this, especially if it won’t do
true
already in the first items in most uses.– Maniero
I once read something that changed my way of thinking: Your time as a programmer is worth much more than a few machine instructions. There’s nothing wrong with your code and it won’t fail (for strings).
– user178974
@Edney therefore must do the right thing because in general it is the one that will give the least work and will work best, including in most cases the right one is the fastest. There’s a difference between right, wrong and anyway, the most dangerous is either way because it feels right but it’s not.
– Maniero
There is a lot of discussion about "over Optimization", if this task will be used 1 time a month, whatever... it can spend 10 4 more instructions that either does (but it has to be perfectly functional). Now if the task will run 10 3 times per micro second for 10 years, then it’s worth optimizing as much as you can, even if you spend 10 8 more lines of code.
– user178974
Almost all discussions about optimization that is out there is talk cake recipe, the right thing is always to do what you have to do. When people start not worrying about what they’re doing, that’s when they start giving trouble. People think that producing fast is more important than thinking about what you’re doing, and then you shape everything they do and you get to a point where you lose the reference to what’s right to do, because of the cake recipes. They are adopted because they are more productive in the short term and worse in the long.
– Maniero