Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

And most Objective-C (one of those languages) developers I've talked to about this would recall nightmares about that behaviour.


I would simply point to Apple's new language, Swift, dropping that feature as proof that they don't think it's the best way to design a language.

Our team is split (I don't know how evenly) on whether Obj-C or Swift's behavior is better. There are some staunch defenders of Obj-C's pattern being better.

I personally prefer requiring the programmer to explicitly handle nil/null. I think it improves maintainability, because future developers can see the code is correct, and it protects against changes (ex: you knew nil could never happen when your code was written, but then someone changed it so nil does occur and now your code is broken). It also saves future developers from spending time reasoning about the behavior of the code when nil occurs.


I prefer Optional to implicit nullability, but the debate isn't really between Optional and implicit nullability (is there any question there?).

The distinction is between segfault and returning 0 bits, and I'd say that returning 0 bits is generally better, because most of the time, in iOS apps, especially in absence of a "??" operator, it's what you want. For example, in a table view data source you don't have to check if your data array is non-null, just return its count, it'll be 0 if it is.

i.e. Optional monad > return all 0 bits > segfault


It's debatable whether this behavior was a good fit for Objective-C, since it's not actually a dynamically typed language. People reasonably expect their Obj-C compiler to do static checks and in this context dynamic behavior is an unwelcome surprise. It would be interesting if, for example, Smalltalk developers share the same frustration. My guess would be: probably not.

My go-to example in favor of the idea would be languages and libraries where 'null' and 'empty list' cause the same behavior in list-processing functions.


Objective-C is definitely dynamically typed, feel free to use "id" and go to town. To say nothing of swizzling.


The main problem I've seen with Objective-C is due to the fact that nil has truthiness, so this code is dangerous:

    if (input.isInvalid()) {
      reject()
    } else {
      accept()
    }
If input is accidentally nil, it's an invisible bug.

The other main complaint is when you have a long string of methods unexpectedly turn out nil at the end, it's a pain to figure out which one it was.


This is entirely incorrect.

nil is a pointer to the value 0, and in C there is only one value that evaluates to a logical false, which is 0. Invoking a method on nil returns 0. This itself alleviates the kind of nil-check-chains discussed in this thread:

    [[[user profile] name] isEqualToString:@"Paul"];

    [user profile] && [[user profile] name] && [[[user profile] name] isKindOf:NSString] && [[[user profile] name] isEqualToString:@"Paul"];
if user.profile.name is nil, then that expression evaluates to the value 0, which as we discovered, is the _only value that evaluates to false in C_


Huh?

    (int)nil == 0




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: