Triaging items according to a time threshold I'm having this part of code which I want to make smaller, more readable and easy to manage. Any thoughts? I tried linq but got lost. 

foreach (var o in state.s)
        {
            TimeSpan timeDiff = (DateTime.Now - o.time);
            if (timeDiff.TotalSeconds &lt; 300)
            {
                richTextBox.AppendColoredText(o.name + " ", Color.Green);
            }
            else
            {
                richTextBox.AppendColoredText(o.name + " ", Color.Red);
                if (f == false)
                {
                    listA.Add(o.name);
                    listA_clone.Add(o.name);
                }
                else
                {
                    listA.Add(o._name);
                }
            }
        }
I tried linq but got lost.


LINQ stands for Language-INtegrated Query - and querying objects (or data) is not something that should have side-effects... and the very purpose of that loop is to have side-effects. In other words, using LINQ here would be abusing the language.



Your usage of var is inconsistent, and the naming is... dangerous - I like var, a lot. So I like that you're having it in your loop declaration. You see, I can guess that timeDiff is a TimeSpan just by looking at its name - and its assignment makes it pretty obvious what the type is as well. But I have no clue whatsoever what an o is, and all I know about s is that it's probably an IEnumerable&lt;whatever-the-type-of-o-is&gt;. Readable code is code that one can... read.

o.time and o.name are confusing, because they don't follow the typical PascalCase naming convention for public members. If they're public property getters, they should be o.Time and o.Name.

o._name is confusing at best, and a clear violation of encapsulation at worst. _name looks very much like a name for a private field - which shouldn't be accessible from this code. And if it's a public field, then you've broken encapsulation (because fields should never be public). And if _name isn't the backing field for the name property, then you have another problem: you clearly aren't writing code as if the next maintainer were a violent psychopath that knows where you live:

listA.Add(o.name);
...
listA.Add(o._name);


Another thing to note, is that the Boolean expression in an if condition doesn't need to involve a comparison against a Boolean value here:

if (f == false)


if f is a bool, then if (!f) is a Boolean expression... that would be perfectly clear if f had a meaningful name. By convention bool variable names start with is, or has:

if (!isHairyCode)


or

if (!hasBugs)