News for hackers. Stuff that matters.
Re:How to reduce the cognitive load of your code by njharman on Tuesday March 29, 2016 @10:51AM (#11381873)
yeah, I'm not even sure what's suppose to be confusing? That the "constant" comes first, before the comparison operator?
Re:How to reduce the cognitive load of your code by jpfed on Tuesday March 29, 2016 @11:34AM (#11382199)
I am not sure, but here's my guess.

If I say (A != B), people may think of this as being equivalent to saying that there's a property we care about- being not equal to B- and we are checking whether A has that property.

If I say (Joe != null), then we are saying that there is a property "being not null" and we are checking whether Joe has that property. Feels pretty natural.

If I say (null != Joe), then we are saying that there is a property "being not Joe" and we are checking whether null has that property. This is pretty amusing way to think. In the lines after one says something like

    if (null == myArgument) throw new ArgumentNullException("myArgument")
one may proceed with assurance that some properties of null must hold :-)

The distinction between null and not-null is so generally useful that I could imagine making a partially-applied function like NotNull, and applying that to Joe (and many other objects). Would NotJoe get much use other than applying it to null? Probably not.

Re:How to reduce the cognitive load of your code by mc808 on Tuesday March 29, 2016 @11:08AM (#11381997)
Yoda speak, it is confusing like. Most people think "if the variable is not null" rather than "if null, the variable is not". It's easy enough to adapt to the style if it's used consistently throughout a code base, but that effort yields no benefits in most languages.
Re:How to reduce the cognitive load of your code by JustSomeNobody on Tuesday March 29, 2016 @12:11PM (#11382513)
Most people think "if the variable is not null"

Only because this is how people are traditionally taught to think. This can be untaught easily enough.

Re:How to reduce the cognitive load of your code by firethief on Tuesday March 29, 2016 @12:57PM (#11382864)
So you can unteach your developers to think... for what purpose?
Re:How to reduce the cognitive load of your code by mikekchar on Tuesday March 29, 2016 @11:06AM (#11381986)
I think he's complaining that it doesn't read like English. You aren't testing null, you are testing the variable. There is an implication that the subject of the "sentence" will come first. Of course English order is idiomatic in most programming languages, but I don't think that means you can't have other idioms if there is a reason. I find it amusing that he complains about abusing fluent interfaces (making it read like English for no real benefit), but doesn't realize that he's doing the same thing here.

That's why you have coding standards on your team. People agree on the idioms they use. Some people need to be told that it isn't an idiom if only one person likes it, though ;-)

Re:How to reduce the cognitive load of your code by kilink on Tuesday March 29, 2016 @02:16PM (#11383584)
For me it's not that it's confusing, it's annoying to read because it indicates that the programmer didn't actually take the time to learn the language, and is instead writing it as C/PHP something else where expressions in if statements don't need to be explicitly boolean (the examples in the article are Java). If they wrote that, what else do they not understand about the language?
Re:How to reduce the cognitive load of your code by rileymat2 on Tuesday March 29, 2016 @03:06PM (#11384006)
What if it is 0 but not null? I write in a few different languages throughout a typical week. It is nice to have some explicit statements when juggling between them. I know them very well, but it is still tricky when going back and forth.
Re:How to reduce the cognitive load of your code by kilink on Tuesday March 29, 2016 @03:52PM (#11384350)
That also wouldn't even compile. I also write in multiple languages, and for languages for which this is an issue I use a linter. This isn't even an issue in C if you compile everything with the appropriate flags (-Wall or -Wparentheses).