Should I care about moving invariants out of cycle scope if it worsens code readability?
Let's take a look at a simple example:
for (var i = 0; i < collection.Count; i++)
{
...
}
vs.
var collectionCount = collection.Count;
for (var i = 0; i < collectionCount; i++)
{
...
}
The performance of second piece of code is better or equal to first one. It will be equal only if collection is fixed-size and Count is not calculated every time. It will be much better if, for example, collection is Linked List which doesn't cache somewhere its Length.
I understand that second approach will unlikely kill my application performance (it is much more likely some inefficient SQL query will) but at the same time I don't feel comfortable when I write second piece of code as I miss (small) optimization. But at the same time from readability point of view I like the first piece of code more (less lines of code, less variables).
I guess it is minor thing and may be it doesn't worth discussing but I would like to hear your opinion.