PHP to Objective C, where the f**k are parameters?
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:
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)];
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
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.
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@example.com", "firstname.lastname@example.org", "Hello!", "hi!");
Now, which one is the recipient? What is the subject?
[emailSender doSendEmailToRecipient:@"email@example.com" cc:"firstname.lastname@example.org" subject:@"Hello!" body:@"hi!"];
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.
When an IT support engineer goes to a new job interview, how does s/he describes her/his job experience? "Because there are no issues encountered after I deployed the application, so later I was fired and I am looking for a new job."