DEV Community

Whats the most irritating "if statement condition" that you've come across?

Kodengo on April 12, 2018

Inspired by the Pinocchio's discussion with prince charming in "Shrek": Prince Charming: You! You can't lie. So tell me, puppet, where is Shrek? P...
Collapse
 
jreina profile image
Johnny Reina
const foo = condition ? true : false;
Collapse
 
makiten profile image
Donald

When I was a junior, a senior dev on my team didn't know what a ternary operator was. When I explained it, she told me not to put any fancy programming stuff in the code.

So instead, I'd see something like this:

if (validate(objMyObj)) {
    isValid = true;
} else {
    isValid = false;
}
Collapse
 
ben profile image
Ben Halpern

Couldn't that be done without even a ternary statement? Not sure the language, but perhaps something like:

isValid = validate(objMyObj).toBoolean();

Or even just

isValid = validate(objMyObj)

if we're just looking for truthiness.

Thread Thread
 
makiten profile image
Donald

Yeah, I guess it was ambiguous, but since they didn't know (and didn't like) ternary operators, you'd find the same thing just as if/else.

Although, I'd say the first one would be better. Something like,

isValid = validate(objMyObj).Result;
Thread Thread
 
moopet profile image
Ben Sinclair

It might be just me, but once I've got a variable name that implies it's a boolean, I want it to be a boolean. If isValid was some kind of object, for example, then it's going to be really confusing to see it in a debugging message later on, and I'm going to wonder what I broke.

Thread Thread
 
makiten profile image
Donald

In C#, you'd probably have a property like .Result or .IsValid, and although Result is ambiguous, I see it often enough, especially with async methods. To make it less ambiguous, validate() would be CheckIfObjIsValid(myObj).Result, but that's a debate about naming conventions (and part of why I made this post).

Collapse
 
scrineym profile image
Michael Scriney • Edited

Still trying to figure this one out

var tt= false;

if(!tt){
 //stuff
}
if(tt){
 //other stuff
}
Collapse
 
moopet profile image
Ben Sinclair

Probably for quick debugging? Rename "tt" to "debuggingModeActivatedGoFaster" for verbosity.

Collapse
 
kodengo_com profile image
Kodengo

if (!invalid(param)) {
return;
} else {
throw new InvalidException();
}

Collapse
 
val_baca profile image
Valentin Baca

It's making me so mad! Love it