HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Shortening boolean with if statement for document version comparison

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
booleanshorteningwithstatementversioncomparisonfordocument

Problem

Can this be shortened to one line?

bool areVersionsEqual = documentComparer.Equals(version, imported);

if (areVersionsEqual == false)
  {
    changedDocument.Add(imported); 
   }

Solution

Sure. Use the Not operator.

if (!documentComparer.Equals(version, imported))
{
    changedDocument.Add(imported); 
}


To explain a little, it's bad practice to do things like this.

bool boolean = true;
if (boolean == false)
{
//...


because it's completely equivalent to saying

if (true == false)


and can be simplified down to

if (false)

Code Snippets

if (!documentComparer.Equals(version, imported))
{
    changedDocument.Add(imported); 
}
bool boolean = true;
if (boolean == false)
{
//...
if (true == false)

Context

StackExchange Code Review Q#74064, answer score: 14

Revisions (0)

No revisions yet.