3 ms·
I would argue that by using CONSTANT == variable, you are reducing the cognitive load of the compiler, but increasing the cognitive load of the human reading th
by kennu 10y ago
I would argue that by using CONSTANT == variable, you are reducing the cognitive load of the compiler, but increasing the cognitive load of the human reading the code (who has to mentally flip the expression around). I think the original article intended to say the same thing.
- SomeStupidPoint 10y agoThats just what you're familiar with. I find it perfectly natural to read "if 5 == x:" without having to flip it around. (And it's not about "cognitive load" on the compiler, it's about fail to error not fail to silent success.)
- closed 10y agoAgreed. I think the key here is that consistent exposure over time to patterns reduces cognitive load through "chunking".
- nurettin 10y agoCompilers don't have cognitive loads.
- adrianN 10y agoAsk C++ compilers when they have to chew through Boost ;)
- thyrsus 10y agoI don't get it. A test for (in)equality is commutative unless you're inducing side effects (i++) - and then side effects are going to be the big "cognitive load", regardless of which side of the comparison they occur. Am I dyslexic because (null != foo) is exactly as easy for me to read as (foo != null)?
- douche 10y agoIt reads awkwardly for a lot of people, to the point that some call these constructs Yoda Conditionals. I'm quite happy that all the languages I use regularly disallow assignment in ifs, at least.
- irishsultan 10y agoAt least in C++ you shouldn't count on the order in which a and b are calculated in a == b, because comparisons are not sequence points. So no, even including side effects testing for (in)equality is commutative.