I am trying to simplify some of my code (right now I'm at the cyclomatic complexity and class coupling part) and I am having an issue with the class coupling metric...
This is the scenario:
public class firstTestClass
{
public string Name { get; set; }
}
public class secondTestClass
{
public List<firstTestClass> _list = new List<firstTestClass>();
public void testMethod()
{
string agg = string.Empty;
//code goes here
}
}
When I add this part, I have a class coupling of 4 on the testMethod()
foreach (firstTestClass cls in _list)
{
agg += cls.Name;
}
but when I switch to simple for, the class coupling is reduced to 2
for (int i = 0; i < _list.Count; i++)
{
firstTestClass cls = _list[i];
agg += cls.Name;
}
I am thinking that the foreach is behind this, but I can't reasonably explain why/how these extra two couplings happen...
PS. I used Visual Studio 2013 Pro, Update 4 for this example...