5 ms·
!! is an extremely common idiom, across multiple different programming languages. Code should be written so that it is understandable by other professional pro
by srssays 9y ago
!! is an extremely common idiom, across multiple different programming languages.
Code should be written so that it is understandable by other professional programmers, not people who started programming two weeks ago.
- WalterSear 9y agoAs a professional programmer, I've come to prefer code written to be understood by someone who started two weeks ago.
- debaserab2 9y agoThen by that principle you'd want to use !! since casting via function or constructor differs more between languages.
- WalterSear 9y agoWhat it looks like in other languages is only minorly relevant to its obviousness and readability.
- debaserab2 9y agoI don't see the argument that Boolean(val) "reads easier" than !! as long as you know what !! does.
- joesb 9y agoIn that case, don't write any code. Don't use function. Don't use React. Don't use lodash. Don't use third party library. People who only start coding for two weeks can't understand any code. People who start has enough experience to work, but only start in this project for two weeks, will understand "!!" idiom.
- prewett 9y agoI'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.
- savanaly 9y agoIt's one of the earliest things I can remember learning in my intro to programming classes in college. Those were in Java.
- hirsin 9y agoI 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.
- debaserab2 9y agoI'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.
- empthought 9y agoYou're kidding, right? "!!foo" is a complete no-op in Java. It is never needed and never correct.
- GordonS 9y agoWhat? 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'
- slrz 9y agoIt 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.