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

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?
Pinocchio: Uh, I don't know where he's not.
Prince Charming: You're telling me you don't know where Shrek is?
Pinocchio: It wouldn't be inaccurate to assume that I couldn't exactly not say that it is or isn't almost partially incorrect.

Did you find this post useful? Show some love!
const foo = condition ? true : false;

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;

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.

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;

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.

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

Someone might read this and wonder why you split hairs over something seemingly trivial. But focus on these tiny decisions is critical. Even if they are subjective, they reflect someone thinking

  • This isn't good enough
  • This is good enough
  • That was good enough, but this is better.

and all for reasons that they've thought through. It costs seconds and pays minutes. It pays hours when it prevents introducing a bug or more complicated code or when someone else follows the good example. Code is made entirely of details. Code matters, therefore details matter.

To be a bit fair, the ternary operator can be a bit decisive and if it goes over several lines, you're usually better off with if-else.

The best part is your example doesn't even need a ternary operator lol

isValid = validate(objMyObj);

Yep, that was what made it even more hilarious. They applied the same logic in your ternary example in a normal if/else.

Would you believe me if I told you they said they did code reviews?

Good ol' javascript "scream boolean casting":

const foo = !!condition;

I like them nested as well:

const foo = 2 < 3 ? 2 > 3 ? false : true : false;

I'm screaming. One of my coworkers is refactoring some code with ternaries nested several levels deep. The logic is so bad that it temporarily broke my understanding of conditionals.

Still trying to figure this one out

var tt= false;

 //other stuff

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

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

Classic DEV Post from May 12

What Developers Should Know from Microsoft Build

We're clearly entering a new era for Microsoft and for developers that moves from tools and platforms to cloud and services.

Follow @remotesynth to see more of their posts in your feed.
Member since Apr 3, 2018
Trending on dev.to
What discontinued company/product do you wish was still around?
Ask me dumb questions about functional programming
#discuss #javascript #beginners
What are the main benefits of being a self-taught developer compared to being educated with a degree and mentoring?
Who's looking for open source contributors? (October 15th edition)
#discuss #opensource #hacktoberfest
dev.to Show us your octocat alter ego
#showdev #discuss
#goodbye #career
Choose Your Next Article Topic With This Exercise
#writing #productivity
I haven't been assigned work all week. This is my first job/internship. Should I quit or stay longer?
#anonymous #discuss #career