Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are these 2 statements identical?

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;
        }
    }
like image 580
JL. Avatar asked Dec 11 '09 21:12

JL.


People also ask

How do you show that two statements are equal?

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.

Are the two statements 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.

What do you call the two statements that have exactly the same truth values?

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.

What is P -> Q equivalent to?

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.”


1 Answers

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.

like image 124
Dave Mateer Avatar answered Sep 29 '22 10:09

Dave Mateer