>It's not any easier if you understand what !! does
which only applies if you're writing code for yourself. you can't expect everyone to know that idiom. same with using the + operator to cast to number, or an extreme case, the tadpole operator https://blogs.msdn.microsoft.com/oldnewthing/20150525-00/?p=...
I'm curious, which languages? I've never seen any of my colleagues write !!, ever, in C/C++, Java, Python, or Swift. I've also not seen it C# or Go, but my exposure is limited.
I've been paid to write code in Perl, PHP, Ruby, JS, Go, ObjC, and Java, plus a little C and Python, for going on 15 years. I'm pretty sure I've never written "!!" and I think I could count the times I've seen it used on one hand (maybe never in any codebase I've worked on, but I'm not entirely sure)
I spend chunks of every day looking at a C/C++ codebase and have never seen this. Taught Java for a year in college, I have never seen this. I would leave a note to a student for using this to be honest - too unclear, and (as this thread denotes) not well known enough to put in production worthy code.
I'm pretty surprised to hear this from someone who taught a programming course. It actually makes a ton of sense when you evaluate what is happening: it's the negative unary value and then the negative unary result of that value - there's no possible way it could be anything other than true or false. It's more of a side affect of the unary operator ! than it is a language feature, which I believe is why it sees such ubiquitous implementation.
> I would leave a note to a student for using this to be honest - too unclear, and (as this thread denotes) not well known enough to put in production worthy code.
You've done a total disservice to your students that understood negative unary operations then. Given how many upvotes my initial post on this thread has (currently 38) I don't think the thread denotes what you think it does at all.
You're basically wrong on every single point. It's a syntactic hack in JS to get a guaranteed boolean coersion, but for anything else it's an absolutely pointless abomination. (Maybe PHP has a similar thing, but I haven't programmed in PHP, so I wouldn't know.)
What? I've been coding in multiple languages for multiple platforms for almost 30 years and have never come across it. It seems rather unlikely that it's 'extremely common'
It is very common in C code, where for most of its history you hadn't any explicit boolean data type but used int, following the rule that zero is false and anything else is true.
The '!!expr' is the idiom most people use whenever the need for a canonical 0/1 representation arises.