PHP to Objective C, where the f**k are parameters?

Posted: January 16th, 2012 | Author: | Filed under: iOS | 22 Comments »
This entry is part 2 of 4 in the series iOS Beginner blog series

Javascript, PHP, Ruby functions

I assume you are very familiar with declaring functions in any of the languages above, if not, you should not be reading this. Let’s begin with a simple function to send email in these languages:

// PHP or Javascript
do_send_email (recipient, cc, subject, body);
// Ruby
do_send_email (recipient, cc, subject, body)

So it’s clear by looking at the function’s signature that it takes 4 parameters and they could be optional, depends on your implementation. Even if you missed one parameter, in most cases it is fine, no complain, no fatal error.

First encounter with Objective-C

Below is one example of Objective-C function calls

[[UILabel alloc] initWithFrame:CGRectMake(0,0,120,35)];

This is the kind of code I see the most when I first opening up a source file and try to read Objective-C syntax. And I’m sure this is the case for many of you right now.
Now, the biggest problem is that you cannot even tell what it is, left alone guessing what it does. Obviously, I couldn’t tell and that was why I had to ask myself “where the f**k are parameters?”. Because then I can somewhat guess what function it calls.

Unfortunately for me, no one told me that I’m way ahead of myself. So I’m tell you now: Stop guessing! Go back to the basics before continue reading this.

As mentioned in the beginning of the series, I’m not writing a tutorial series, I’m writing about the WHYs of the language, in a way more fundamental than even knowing the syntax itself.

The function’s full name

This is where it gets interesting. For an email sending function call similar to that of PHP:

[emailSender doSendEmailToRecipient:(NSString*)email cc:(NSString*)cced_email subject:(NSString*)subject_to_send body:(NSString*)body_to_send];

The function’s entire name is doSendEmailToRecipient:cc:subject:body: and *not* just doSendEmailToRecipient

Strict and brilliant by design

The Objective-C function’s name is designed to include the parameters’ names. This is not the case with most web programming languages.
Why does it have to be this way? The obvious reason is Objective-C is a compiled language, once you get it wrong, it cannot even be compiled.

However, the more important reason, the motivation of this post, and one of my most important ‘enlightment’ moments is that the function call tell you exactly what to expect without looking back at how it was declared. It is strict by design so that you cannot screw it up.

Imagine, you are writing a black-box email sending class and you need to tell other programmers how to use it by telling them the name of the functions, the parameters they accept. Ask yourself, how did you do it in PHP/Javascript/Ruby? For me, it would be via the source code or the documentation.

In Objective-C this mechanism is built right into the function name itself. Even if you don’t have the source code or the documentation, you still can pretty much understand how to use it correctly. The parameter names are always self-descriptive and the type is strict. Again, you cannot use the wrong syntax, the compiler will simply cough out tons of red lines and refuse to let you do any more stupid things.

So in my opinion, this is actually brilliant design. No doubt it requires more typing, but it potentially saves a lot of (human) communication errors when sharing codes and working in a team. When I was still coding in PHP, I found myself constantly having another file open just see the declaration of a function I wrote before that. (Extra typing time * 100000) < Total time save ==> happy coder.

If you are not convinced

do_send_email("[email protected]", "[email protected]", "Hello!", "hi!");

Now, which one is the recipient? What is the subject?

[emailSender doSendEmailToRecipient:@"[email protected]"
                                 cc:"[email protected]"
                            subject:@"Hello!"
                               body:@"hi!"];

Crystal clear!


I hope you understood the very fundamental reason why Objective-C syntax has to be the way it is. Accept the ‘weird’ syntax that you have never seen before, and appreciate the thoughts Apple has put into improving the language (Apple does not design it) and be at peace with yourself.

Updated: as pointed out by one commenter below. The language was designed by Brad Cox, not Apple. Apple inherited it with OpenStep/Rhapsody/MacOSX.

Let me know what you think about this post and give me some feedback on what should I write next for the series in the comment form below.


<< The iOS Beginner blog seriesPHP/Rails to iOS, what are .h and .m files? >>

  • Please attribute the design of the language Brad Cox, to its actual designer, not Apple. The choice to use Objective-C came from NeXT, not Apple. Apple inherited it with OpenStep/Rhapsody/MacOSX.

    • Kent Nguyen

      Thanks for pointing out the correct attribution. I made the changes.

  • First sentence should read: “Please attribute the design of the language to Brad Cox, its actual designer, not Apple.”

  • There are keyword arguments in many other languages as well.

    E.g. Python:
    send_email(
    recipient=”[email protected]”,
    cc=”[email protected]”,
    subject=”test”,
    body=”bla”
    )

  • named params works with Ruby 2.0
    Works with: MRI version 2.0.0dev (2012-01-04 trunk 34211)

    def example(foo: 0, bar: 1, grill: “pork chops”)
    puts “foo is #{foo}, bar is #{bar}, and grill is #{grill}”
    end

    # Note that -foo is omitted and -grill precedes -bar
    example(grill: “lamb kebab”, bar: 3.14)

    • Kent Nguyen

      Yes I do use some of these in my Rails 3 app. However, it’s optional and you still need to refer back to the declaration some times.

  • How much time would it take a moderately seasoned low level programmer to learn objective c and iOS programming?

    • Kent Nguyen

      i’d say a lot. Obj C is not a copy and paste language like PHP

  • In your example:
    [emailSender doSendEmailToRecipient:@”[email protected]….];

    Is “emailSender” the class that “doSendEmailTo…” function exists in?

    I’m sure there is a way, but is it possible to run one-off helper functions, or does everything have to be a method on a class object?

    • Kent Nguyen

      I’m just using this as an example, you got to be looking at it from a broader perspective.

  • Pingback: PHP to Objective C, where the f**k are parameters? | Kent Nguyen()

  • Pingback: abcphp.com()

  • Great start. Nice and clear. Really useful with regard to setting an expection of what learning Objective C will take. Looking forward to further installments.

  • URL

    I besides believe therefore , perfectly composed post! . 599417

  • Pingback: Homepage()

  • Dinah Carver

    Thx for your post, I really enjoy your blog.

  • Pingback: PHP/Rails to iOS, what's the difference between .h and .m files?()

  • Awesome read. I just passed this onto a friend who was doing some research on that. He just bought me lunch since I found it for him! So let me rephrase: Thanks for lunch!

  • chicken soup

    Very great post. I just stumbled upon your weblog and wanted to mention that I have really loved surfing around your blog posts. In any case I will be subscribing in your rss feed and I hope you write again very soon!

  • Hi, the real reason for this is that ObjC does not support method overloading like other languages do. For example in Java or C++ you can have methods like these in the same class:

    public void doSendEmail(String recipient, String subject, String body);
    public void doSendEmail(String recipient, String cc, String subject, String body);

    Now the compiler actualy sees the methods like:

    doSendEmail_String_String_String
    doSendEmail_String_String_String_String

    Now in Objective-C you don’t officialy have method overloading like this but the same
    thing is achieved by having the whole method name as a composite of all the parameter names!

  • Halley62373

    It’s very interesting, good work!

  • Pedro Luz

    Really enjoyed the post. All languages have diferences between them. It took me a while to get that parameter thing in objc working in my head.

    Languages should evolve to learn from other languages, to turn life easier to developers.