Tag Archive for 'Programming'

The opterator hated by most, loved by me: ternary

So I’ve heard it or read it at least 5 times in the past few days: everyone hates the ternary operator. Why? What’d it ever do to you? Wait wait, what’s the ternary operator? Srsly? You’re on my blog and you don’t know what it is? Ok, so normally one writes a conditional like this:


if (someCondition equals some value) {
do stuff;
} else
{
do other stuff;
}

Lots of code for not so much of anything else (outside of readability). Ok, so for those cases where the stuff and other stuff are single lines or some other simple deal (e.g. returning a boolean or integer value), the ternary operator is great. Take a look:


(someCondition equals some value) ? do stuff : do other stuff;

Look at how nice and neat that is — how’s it difficult to read? What if you want to set a value?


x = (someCondition equals some value) ? true : false;

So if someCondition equals some value, x is set to true, otherwise it’s set to false. Nothing difficult or complicated about it. There’s nothing hard to read about it either, imo. Having said all of this, don’t go doing something stupid like this (because it IS hard to read):


(someCondition equals some value and another Condition equals some value or .............. and ............. yada yada more conditions and stuff that eventually evaluates to a boolean) ? do a ton of stuff : do some other ton other stuff;

Bad. Skip it, keep things simple, and go with the regular conditional syntax. Trust me, other people will look at your code and some might even want to slap you for it. Anyway, don’t hate on the ternary operator. If it’s not your style, that’s fine, but you should know that it’s very useful and saves some typing. If people didn’t hate it so much, it would have saved me this blog post..but then again that would have been sort of boring.

NSURLConnection + timeoutConnection = Amdev Woes

So for the past two days I’ve had my iPhone app crashing and vomiting “EXC_BAD_ACCESS” in the console. Generally speaking, this error means you’ve released a reference to some object you still need and some other object tried to send a message to said object afterward. It turns out that it was entirely my fault and (GASP) paying a little more attention to Apple’s reference would saved hours and hours of debugging for me.

NSURLConnection is actually pretty simple to use but here are some tips:

  1. Use the asynchronous mode for any reasonably large file (bigger than a few KB) as people might be on a slow Edge network connection and locking up the view is never a good thing.
  2. Do not send the message “start” to your connection if you use connectionWithRequest or initWithRequest to initialize your connection as you’ll get the nasty “EXC_BAD_ACCESS” error mentioned above. The reason for this is simple:  You’ve scheduled your connection to time out in n seconds, the method that initializes the connection starts fetching data immediately. You then send the message “start,” which spins off the connection trying to fetch data again. This works fine except that the original attempt eventually times out and tries to call back, hosing everything over and crashing your application hard core. Oops. You should only send the message “start” to your connection if you initialize it with initWithRequest:delegate:startImmediately.
  3. When setting up NSURLRequest to pass to NSURLConnection, I highly recommend using NSMutableURLRequest so you can set the User-Agent header.  Some http servers may not return data, even for a simple GET without the User-Agent.  It’s my understanding that User-Agents are optional, but I do not know if said server behavior is nonstandard.

    NSMutableURLRequest *request  = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: @"http://myurl.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];

    //Borrow FF2’s User-Agent

[request setValue: @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16" forHTTPHeaderField: @"User-Agent"];

I hope this saves someone some time.