Do the following 2 code snippets achieve the same thing?
My original code:
if (safeFileNames != null)
{
this.SafeFileNames = Convert.ToBoolean(safeFileNames.Value);
}
else
{
this.SafeFileNames = false;
}
What ReSharper thought was a better idea:
this.SafeFileNames = safeFileNames != null &&
Convert.ToBoolean(safeFileNames.Value);
I think the above code is much easier to read, any compelling reason to change it?
Would it execute faster, and most importantly, will the code do the exact same thing?
Also if you look at the : Convert.ToBoolean(safeFileNames.Value);
section, then surely this could cause a null reference exception?
this.SafeFileNames = bool
Local safeFileNames is a strongly typed custom object, here is the class:
public class Configuration
{
public string Name
{
get;
set;
}
public string Value
{
get;
set;
}
}
Two statement forms are logically equivalent if, and only if, their resulting truth tables are identical for each variation of statement variables. p q and q p have the same truth values, so they are logically equivalent.
Two statements are equivalent iff they have identical truth columns. To test for equivalence, construct a joint truth table for the two statements and compare their truth columns. If the columns are identical, then the statements are equivalent. If they are not identical, then they are not equivalent.
EQUIVALENT STATEMENTS Any two statements p and q are logically equivalent if they have exactly the same meaning. This means that p and q will always have the same truth value, in any conceivable situation.
P → Q is logically equivalent to ¬ P ∨ Q . Example: “If a number is a multiple of 4, then it is even” is equivalent to, “a number is not a multiple of 4 or (else) it is even.”
The fact that you asked the question suggests to me that the former is preferred. That is, it seems to me that your question implies that you believe the first code to be easier to understand, and you aren't quite sure if the second is equivalent. A primary goal of software design is to manage complexity. If it's confusing to you now, it may also be to you later, or to whomever supports your code down the road.
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