Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReSharper syntax suggestion

The if keyword in the following statement is underlined in green by ReSharper:

if (readOnlyFields.Contains(propertyName)) return false;

return base.CanWriteProperty(propertyName);

ReSharper suggests the following change:

return !readOnlyFields.Contains(propertyName) 
    && base.CanWriteProperty(propertyName);

Why is this "better"? I find the current code more readable and the result should be the same as far as I know. Any thoughts?

like image 521
Sakkle Avatar asked Jul 17 '26 23:07

Sakkle


2 Answers

Neither one is better in the sense that either one will perform better than the other. (Any difference is going to be entirely negligible.)

Certain coding conventions suggest that you use one return statement per function so that it's easy to understand it's flow. It's not a hard and fast rule though and in this case it's pretty obvious what's going on. If you don't like it's suggestion but you would like to ensure that your code is easily read by others I'd suggest the following:

if (readOnlyFields.Contains(propertyName)) return false;
else return base.CanWriteProperty(propertyName);

But your way is fine too.

like image 66
Spencer Ruport Avatar answered Jul 19 '26 14:07

Spencer Ruport


On the quick fix menu (Alt+Enter), there's a "Correction options" (or something like that). You can turn this specific suggestion into a hint, or turn it off entirely.

On a personal note, I prefer your original over ReSharper's suggestion.

like image 41
Roger Lipscombe Avatar answered Jul 19 '26 14:07

Roger Lipscombe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!