Which way is better practice: return a value from a method inside an using
statement or declare a variable before, set it inside and return it after?
public int Foo()
{
using(..)
{
return bar;
}
}
or
public int Foo()
{
var b = null;
using(..)
{
b = bar;
}
return b;
}
I prefer the first example. Fewer variables, fewer lines of code, easier to follow, easier to maintain...
public int Foo()
{
using(..)
{
return bar;
}
}
Following the "less is more" principle (really just a variant of KISS), the former. There are fewer lines of code to maintain, no change in semantics and no loss of readability (arguably this style is easier to read).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With