This loop was in the code for a few weeks now and was not being reported as any problem.
foreach (DataColumn column in rawSheetData.Columns)
{
var datum = Convert.ToString(row[column.ColumnName]);
cs.Data.Add(column.ColumnName, datum);
}
Today I pushed a PR to fix a legitimate critical issue elsewhere in the code, and while I fixed that, sonarcloud started complaining about rawSheetData.Columns
in the code above being a code smell and that it should be converted to a “clearer, more concise LINQ expression instead”.
Sonar should not have just started complaining like that about something. Was anything released in the last week that would have caused this to start getting flagged?
Either way this rule evaluation should be reverted, because a for loop does not get any more readable or easier to follow than this. Adding a LINQ expression to project… what exactly… is going to add to the cognitive burden of reading the rest of a loop and its not going to make the code any more concise or clearer.
The suggestion is to change the above code to something like this…
foreach (var columnName in rawSheetData.Columns.Select(cs => cs.ColumnName))
{
var datum = Convert.ToString(row[columnName]);
cs.Data.Add(columnName, datum);
}
what is going to happen is compiler errors because you cant LINQ a DataColumnCollection
(or a DataRowCollection
)
I would have to cast it to make this idea work
foreach (var columnName in rawSheetData.Columns.Cast<DataColumn>().Select(dc => dc.ColumnName))
{
var datum = Convert.ToString(row[columnName]);
cs.Data.Add(columnName, datum);
}
Which nobody should be doing when they have code like the original loop. All that was accomplished by following this sonar rule was a bit of code gymnastics in order to make column.ColumnName
into columnName
. And it made the intention of the statements unarguably less clear.
EDIT: Looks like it was just made available yesterday…
Its nice to know that not only will the sonar engine surprise me with a failed PR when I didnt even touch the code its complaining about, but also that sonar source takes the time to insult me and call me clumsy as well. After the sarcasm and irony has been properly digested, kindly burninate the ‘clumsy’ tag