News for hackers. Stuff that matters.
Re:OS X Auditor by mdaniel on Sunday September 14, 2014 @02:47PM (#8315889)
And that, folks, is why we don't do chained expressions. Variables increase readability and also offer one the opportunity to check a condition that should be true.

This, KeyError, NoneType exceptions and its Java friend NullPointerException drive me batty. They are so simple to defend against, if the author just took the time.

Re:OS X Auditor by chc on Sunday September 14, 2014 @03:37PM (#8316019)
Chaining itself isn't really the problem here IMO. Creating a variable for each level of the nested data structure and manually checking for the validity of each subsequent one would really, really not produce more readable code. It would turn a "get this value out of a data structure" expression into about 15 lines of code that each need to be individually read and understood. The problem is that Cocoa doesn't provide a great way to say "Access this stuff in this data structure unless it isn't there."

An option type is really the best way to kill this breed of problem, and thankfully Apple is including one in Swift, though it's still somewhat green.

Re:OS X Auditor by photojosh on Sunday September 14, 2014 @08:13PM (#8316707)
That's a Obj-C problem, because it's being fixed with what Swift does with it's '?' on variables. :)
Re:OS X Auditor by chc on Monday September 15, 2014 @01:25PM (#8320082)
Not exactly. This program would have the same problem in Swift, because rather than returning optionals, Swift arrays have the same explode-on-invalid-accesses behavior as NSArray.
Re:OS X Auditor by arianvanp on Sunday September 14, 2014 @05:19PM (#8316268)
Maybe monads make this kind of chaining safe.
Re:OS X Auditor by yohanatan on Sunday September 14, 2014 @08:46PM (#8316777)
The accessor variant of the existential operator in CoffeeScript (which Swift apparently has taken influence from) handles this nicely.
Re:OS X Auditor by nsxwolf on Sunday September 14, 2014 @03:04PM (#8315935)
I love Optionals for this.