Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using true and false as the expressions in a conditional operation

I'm maintaining some code and have found the following pattern a lot:

var isMale = (row["Gender"].ToString() == "M") ? true : false;

instead of this:

var isMale = (row["Gender"].ToString() == "M");

Is there any reason why anyone would do this? Does anyone think the former is more readable or clearer? Is there some sort of old C "gotcha" that this is a hold-over from?

like image 473
Chris Avatar asked Jul 01 '10 19:07

Chris


People also ask

What is a conditional expression example?

if (y > z) x = y; else x = z; The following expression calls the function printf , which receives the value of the variable c , if c evaluates to a digit. Otherwise, printf receives the character constant 'x' .

What is conditional expression explain?

The purpose of the conditional expression is to select one of two expressions depending on a third, boolean, expression. The format for the conditional expression is. <boolean-expression> ? < expression-1> : <expression-2>


2 Answers

A valid reason? No.

It's usually produced by people who don't really understand that a condition is also in itself an expression, producing a boolean result. In particular, people schooled on a language where this isn't the case, such as many variants of BASIC.

like image 81
bobince Avatar answered Sep 20 '22 13:09

bobince


I guess if you get paid by the character that would be valid. Other than that I can't think of any reason.

like image 42
Shaun Bowe Avatar answered Sep 17 '22 13:09

Shaun Bowe