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

  Kent Nguyen        2012-01-16 09:46:09       4,067        0    

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("some@email.com", "another@email.com", "Hello!", "hi!");

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

[emailSender doSendEmailToRecipient:@"some@email.com"
                                 cc:"another@email.com"
                            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.

Source : http://kentnguyen.com/ios/ios-beginner-series-function-names/

OBJECTIVE-C  PHP  JAVASCRIPT  PARAMETER  FUNCTION NAME 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

When shifting from the tail of a queue