3 ms·
Yoda conditions
- wfn 13y agoNot sure of readability, but this surely is a neat trick to avoid null pointer exceptions, etc. (and it can be extended to cover other, related cases..): String myString = null; if ("foobar".equals(myString)) { /* ... */ }
- hibikir 13y agoI'd argue that the solution to the problem is to make sure null can't happen, ever. It's way too much boilerplate in Java, but par for the course in, say, Scala. If it can really be null, and it makes sense for it to happen, wrap it in an Option.
- bitmilitia 13y agoDo, or do not use Yoda conditions. There is no try.
- RKearney 13y agoI use Xcode for development and it already warns against assignment within conditions[0]. gcc also warns against it[1]. Python just errors out all together[2]. I don't have my Windows instance up with Visual Studio but I'm fairly certain that it too notifies you of assignments within conditions. So the question is, why would you sacrifice readability to help solve a non-issue? [0]https://www.dropbox.com/s/sjtefd55mwigq3y/Screenshot%202014-03-09%2020.51.54.png https://www.dropbox.com/s/sjtefd55mwigq3y/Screenshot%202014-... [1]https://www.dropbox.com/s/yx01tdeykpkespv/Screenshot%202014-03-09%2021.00.13.png https://www.dropbox.com/s/yx01tdeykpkespv/Screenshot%202014-... [2]https://www.dropbox.com/s/ex9dskab82n52qf/Screenshot%202014-03-09%2021.04.24.png https://www.dropbox.com/s/ex9dskab82n52qf/Screenshot%202014-...