Which code is written better? [closed]
-
28-04-2021 - |
Question
I ran a static code analysis tool on our tool and looking at its results the code below was one of the things it was talking about:
SpreadSnapshot oSnap = new SpreadSnapshot();
using (oSnap.SetRowCol(fpSpread, row, col))
{
SpreadSetComboBox(fpSpread, list, displayProperty);
}
So I changed it to the code below and it fixed the error that the tool was talking about:
using (SpreadSnapshot oSnap = new SpreadSnapshot())
{
oSnap.SetRowCol(fpSpread, row, col);
SpreadSetComboBox(fpSpread, list, displayProperty);
}
So in your opinion Which style of coding do you think is more appropriate and less error-prone?
Thanks
Solution
The two mean completely different things, unless SetRowCol
returns this
at the end. In the first, you're disposing the results of SetRowCol
. In the second, you're disposing the SpreadSnapshot
.
If both are disposable, you should do a using for both:
using (SpreadSnapshot oSnap = new SpreadSnapshot())
using (oSnap.SetRowCol(fpSpread, row, col))
{
SpreadSetComboBox(fpSpread, list, displayProperty);
}
OTHER TIPS
The latter - it ensures that you don't end up using oSnap
after the using
statement.
Aside from anything else, it would be pretty odd for SetRowCol
to return something disposable... what would that even mean?