Getting Objective, part 2: [Square Brackets] and other animals


18 December 2011, by

previous article in series

Recap

In part 1 I gave a little introduction to Objective C – why people would want to develop for it, and a quick hello world example. This post will go into more detail about the syntax, with the help of a few household pets.

Making Small Talk

Objective C is based on Smalltalk, which was first released into the wild in 1980 – though it first came into existence in the early seventies off the back of a bet. Smalltalk, and by extension Objective C, is based on message-passing – your code sends a message to an object and the object either deals with that message in the appropriate fashion, or says that it doesn’t understand. To illustrate:

Message passing example illustration

You send a message to objects (or in this case, people) and they take an appropriate action, provided they understand the message.

Caring for your menagerie

Let’s take our cat message and convert it into something more like Objective C. First up, let’s assume that we have an object called Jim and an object called cat. Asking our instance of Jim to look after that cat might look something like:

// Jim loves furry animals, he's sure to say yes
[Jim canYouLookAfterMy:cat]

Jim receives (and understands) the message “can you look after my” with the parameter cat. His internal processing would, presumably, check for the furriness of cat and then return an appropriate response: “Sure!”

If, on the other hand, we tried:

[Jim canYouLookAfterMy:turtle]

Jim would probably notice that turtles are not furry, and return “Sorry!”, while Bob might identify that turtles are not cats and return “Sure!” instead.

The square brackets indicate that we are sending a message, while everything inside defines the receiver and the details of message. We can add extra parts inside the brackets to our heart’s content:

[Bob canYouLookAfterMy:turtle until:thursday andFeedIt:haribo]

Bob receives the message and knows how to deal with the parameters turtle, thursday and haribo.

Because everything in Objective C is, itself, an object, we can also put square brackets inside square brackets inside square brackets inside…:

// Let's stitch Bob up!
[Bob canYouLookAfterMy:[petShop buyRandom:[Marsupial class]]
                 until:thursday
             andFeedIt:[harrods buyFrom:foodHall aisle:4 shelf:2 price:200]]

So what about Chen1? Chen doesn’t understand the “can you look after my” message, and trying to send it to him would cause an exception. Fortunately, we can check first:

if ([Chen respondsToSelector:@selector(canYouLookAfterMy:)])
{
	[Chen canYouLookAfterMy:walrus];
}

Because Chen doesn’t understand our message, the if statement at the start will return false, and so we won’t ever try to ask him to look after the walrus.

Keeping things readable

You’ve probably guessed by now that a lot of the syntax in our pet care example is somewhat expendable: “canYouLookAfterMy”, “until” and “andFeedIt” could equally well be “a”, “b” and “c”. Because Objective C allows us to describe all our parameters, though, it seems churlish not to exploit that functionality.

Having a comprehensible explanation of what our parameters are for means that when you come across a piece of code calling a function, you can get a decent understanding of what it’s likely to do without having to look at the actual definition. If we were to try to convey the same amount of information in Java, we’d need something like:

/**
 * @param animal - the animal to be cared for
 * @param until  - the date up to which care is required
 * @param food   - what the animal needs to be fed
 *
 * @return - returns a boolean indicating whether or not
 *           the person is able to care for your pet
 */
public bool canLookAfterAnimalUntilDateWithFood(Animal animal, Date until, Food food)

where in Objective C our equivalent function definition is:

-(BOOL) canYouLookAfterMy:(Animal *)animal until:(NSDate *) date andFeedIt(Food *)food

Essentially, we have negated the need for Javadocs.

Next time

Next time, we’ll get back to our Hello World example and, now that we’re up to speed with the syntax, make it a little more full-featured.

Footnote

1Chen is a character in Micro Machines V3 – a game which occupies a special place in the hearts of many Softwire employees.

next article in series

Tags: , , , , , , ,

Categories: Mobile, Technical

«
»

Leave a Reply

* Mandatory fields


2 × seven =

Submit Comment