views:

122

answers:

4

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?

+2  A: 

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.

Roger Lipscombe
Thanks fot the hint on turning it off. I chose to keep it on for now, but at leat no I know I can turn it off if I want to :)
Sakkle
+2  A: 

Some people have an aversion to having multiple returns from functions.

All aversions in programming seem to wax and wane with the passing of time, and this particular one is not particularly fashionable at the moment.

Will Dean
+3  A: 

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.

Spencer Ruport
+2  A: 

In such cases, "readability" is much affected by the reader's personal style. Once you get used at writing stuff in a certain format, you also get used at reading it the same way.

For instance, I'd go for the ReSharper's suggestion. But it's just a matter of personal preference, combined with the fact that I don't find return statements at the end of the line very readable either. Scanning code is somewhat easier when all keywords are at the beginning of the line.

Again, there's no "true answer". You can just disable that suggestion and go with whatever you're used to.

Dan C.