if(!object)
[[Do something]about it]
vsif(object == null)
DoSomething.Aboutit
I know its a small thing, but my typenames are usually lengthy so in practice C# statements can get character heavy. public static bool operator true(Class c)
{
return c != null;
}
public static bool operator false(Class c)
{
return c == null;
}
To avoid redoing this for each class you can just derive from a base class that implements these.Part of me feels that Im not "really programming" when I write managed code though. I like writing C and ObjC because I feel like Im actually flipping switches on the CPU. It is silly I know(and all in my head), but being lower level seems to excite me more.
In reality I think its all about the right tool for the job, and choice is good.
The trick is to use null checks almost like you're writing C++, but drop them when they are unnecessary and inelegant. Convince yourself that a null check is unnecessary before removing it. This way you're reducing the chance of bugs and making them easier to pinpoint.
For example, you can do:
if ([someArray count])
rather than if (someArray && [someArray count])
On the other hand, you may be asking for trouble if you do [[someArray lastObject] dance];
without checking [someArray lastObject] for nil, since if someArray is empty, then nothing will dance and you won't know about it.tl;dr: Messaging nil is not idiot-proof but it makes code read better.
NSString *myString = nil;
if ([myString compare:@"something"] == NSOrderedSame) {
...
}