Hacker News new | past | comments | ask | show | jobs | submit login
PHP to Objective C, where are the parameters? (kentnguyen.com)
21 points by kentnguyen on Jan 16, 2012 | hide | past | favorite | 25 comments



Not a bad intro to ObjC message syntax. Even though the article has few mistakes.

Most notably ObjC syntax does not have functions that are called, it has messages that are sent to objects and classes. In the end all messages are "translated" to C function calls to objc_msgSend().

Everything makes sense after you read the proper documentation. Which in this case is http://developer.apple.com/library/mac/documentation/Cocoa/R...


Thanks for pointing that out, nevertheless I guess using the similar concept to PHP/JS makes the user relate to the transition better.

I did read that Apple doc when I first starting out but it was overwhelming to me at that time and it backfired. I was more discourage to learn Objective C after seeing the doc.


I've always found it easier to just explain the messages by calling them functions, otherwise people will often get wrapped up in their previous conceptions of messages in programming.


I hope you understood the very fundamental reason why Objective-C syntax has to be the way it is.

It does not need to be like this in a statically typed language. Most statically typed languages don't require you to pass the parameter name. Some have this optionally so you can mix and match or have default values (C# for example http://msdn.microsoft.com/en-us/library/dd264739.aspx).

If you'd written other statically typed language the Obj-C syntax would probably still be surprising to you.

Brilliant design by Apple it is not, it's the nature of statically typed languages. For example your comment:

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

Shows a lack of experience with statically typed languages. IDEs will tell you if you hover over the method with your mouse as well as when you're creating the method (including all the variations).

On the other hand it's quite fun to see someone naturally find out some of the reasons why some people prefer statically typed languages.


I'm sorry, but all of your comment is nonsense.

1. Static typing does not help with writability when you're passing 4 strings to a method (now that can be made into a debate over good usage of types, language cultures and many other things, point is when your arguments are "untyped" having a stricter type checking of nothing will not help you)

2. Static typing does not help with readability when you end up with

    do_send_mail("some@email.com", "another@email.com", "Hello!", "hi!")
anyway, there are many cases where keyword arguments significantly enhance the reading experience

3. IDEs are perfectly able to "tell you if you hover over the method with your mouse as well as when you're creating the method" for dynamically languages.

The only thing a statically typed language will help you with TFA's example is if you have extraneous or missing arguments, and there even a good IDE will do it in a dynamically typed language (Jetbrains's IDEs do, for instance).


Did you read the article?

The author claims it has to be this way in Obj-C. Not that's it's a nice thing. It doesn't at all.

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.

The whole premise of the article is fundamentally flawed. I was just trying to tell him in a nice way.

All his 'reasons' are bunk when you look at what a static language provides you in terms of explicitness.

I'll leave with this:

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

That never happens in a static language. It does in dynamic.

Or perhaps he just doesn't use IDEs and that's the actual revelation?


> That never happens in a static language. It does in dynamic.

Er, says you?

> Or perhaps he just doesn't use IDEs and that's the actual revelation?

I'm guessing you're either relatively new to the industry, or have lived in the Microsoft and/or Java worlds during most of your career. Use of IDEs outside of those contexts is much rarer, and for much of history, even well into the 2000s, bordered on nonexistent. To this day, many of us do not use what you would consider an "IDE" at all for any language in our normal routine.


FFS, which way is it? Do dynamic languages have extensive intellisense support or not? Do you need to give it hints or not?

I'm not new to the industry, the day job's MS, but my personal stuff isn't.

If you're sitting there without an IDE in 2012 and looking up or memorising the syntax for every single method call you make it's time to grow up and realise there's a better world out there.

Even my bloody text editor has basic intellisense.


> looking up or memorising the syntax for every single method call you make

As programmers have done for generations? I've never seen an autocomplete functionality that was substantively faster than either actually knowing the platform I'm developing on (what a shocking concept), or having a browser window open alongside my editor.

> it's time to grow up

PKB. When you do, you'll realize not everyone who disagrees with you is an idiot.

> and realise there's a better world out there.

"Better" is highly subjective. IDEs inevitably lower my productivity dramatically with their bloated, buggy, slow, and brittle nature.

> Even my bloody text editor has basic intellisense.

No, it has an autocomplete function. IntelliSense is a Microsoft implementation of the general autocomplete concept. And if you're only worried about autocomplete, why are you so shocked people might not be using a full-blown IDE?


3. IDEs are perfectly able to "tell you if you hover over the method with your mouse as well as when you're creating the method" for dynamically languages.

Which IDEs and for which languages? The best I've seen so far was Emacs+Semantic for Python, and it's still totally uncomparable to what Eclipse, or MSVS, or Emacs+Semantic for C or Java offers.


> Which IDEs and for which languages?

Jetbrains's IDEs do it for most of the languages they support, I've had it for Python, Javascript and Ruby.

I'm not saying the static analysis is as good as a statically typed language, but basic features like listing arguments are not a problem.


Does it work for situations like:

    x = someObject.someMethod()
    # does IDE know here what the type of `x' is?
    x.otherMethod(...) # will IDE list arguments of otherMethod? 
If it does not, then it's hardly useful, and it's unfair to compare intellisense for Java/C#/C with this.


I was interested in this article when I saw that you

"... wanted to help out those who began to learn iOS evelopment, especially programmers who are coming from no programming background or web development background"

but then the first line of your next article is:

"I assume you are very familiar with declaring functions in [Javascript, PHP, or Ruby], if not, you should not be reading this"

So... which one is it? A tutorial for non-programmers or programmers familiar with other languages? or a tutorial specifically for people fluent in Javascript/PHP/Ruby?


I'm going to have to start referring to this post when I explain why I like objective c. Usually people just give me this crazy stare like I'm some kind of moron.

One thing though, if you spell a method name wrong, it will still compile. It's only a warning that "the object may not respond to that selector". Of course if you have warnings as errors turned on, then of course it will error.


> One thing though, if you spell a method name wrong, it will still compile. It's only a warning that "the object may not respond to that selector".

And it's important to understand that: while Objective-C's "C" part is statically typed, Objective-C's objective model comes from Smalltalk and remains essentially dynamically typed. That's in fact what `id` is: a purely dynamic type. This is quite unlike Java or OCaml.


i guess im abit overboard trying to make my point. but you are right, no error, just warnings.


python also has this right by introducing named parameters. The same function in python can be written as:

  do_send_mail(recipient="some@email.com", cc="another@email.com", subject="Hello!", body="hi!")


There are significant differences though:

1. It's not possible to mandate the usage of keyword attributes in Python 2, this function could be called with

    do_send_mail("some@email.com", "another@email.com", "Hello!", "hi!")
JS/Ruby's low-fi version of using hashes actually helps there as you have to provide hash arguments via the hash.

Python 3 lets you fix this issue (a great reason to switch, by the way) by using a ⭑[0] argument (not a ⭑-arg, which you can use in Python 2 but which will simply make your stuff fall into a black hole unless the creator of the function asserts no positional argument was passed via e.g. `assert not arg`; furthermore Python 2 would require that a default value — to check for — be given for each keyword argument):

    >>> def foo(*, a, b):
    ...     print(a, b)
    ... 
    >>> foo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() needs keyword-only argument a
    >>> foo(1, 2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() takes exactly 0 positional arguments     (2 given)
    >>> foo(a=1, b=2)
    1 2
2. the arguments are separate from the method name in Python, in your example the method is `do_send_mail`, in Smalltalk and Objective-C it's `doSendMailToRecipient:cc:subject:body:` which has advantages and inconvenients. Python is easier for default values (you need a new method in Smalltalk or Obj-C), but the Smalltalk/Obj-C method is simpler for behavioral differences (as the dispatch is done at callsite, Python will need internal soup)

3. There is no order in Python's argument (whereas changing the order in Smalltalk/Obj-C changes the method called), which means callers can lower the call's readability by swapping arguments around in... less than sensible ways.

(nb: I love Python, don't interpret my comment as a put down of it, just pointing out fundamental and important difference between Python's keyword arguments and Smalltalk's keyword messages)

[0] I hate HN's markup.


> It's not possible to mandate the usage of keyword attributes in Python 2

That's not completely true, but it's inconvenient. If you're prepared to go the kwargs route, calling with positional arguments raises a TypeError:

    >>> def foo(**kwargs):
    ...  print(kwargs['a'], kwargs['b'])
    ... 
    >>> foo(1, 2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() takes exactly 0 arguments (2 given)
    >>> foo(a=1, b=2)
    (1, 2)
You have to manually check for required parameters, though, and assign any necessary defaults.


> That's not completely true, but it's inconvenient. If you're prepared to go the kwargs route

Ah true, I'd not thought about using kwargs only.

> You have to manually check for required parameters, though, and assign any necessary defaults.

Yeah, and you likely lose parameters management in the IDE.


thanks for the point. I have no background in Python so I didn't take Python into consideration. In my opinion, there are more people like me: coming from pure HTML/CSS/PHP background straight to Objective C


Look into Smalltalk (which is the language Obj-C borrowed the message syntax from) too. Squeak by Example is an excellent intro.

http://squeakbyexample.org/


Totally personal preference. I would rather have a language be concise and the documentation verbose than the other way around or with both verbose.

Having it be verbose is great for learning, but after a short while syntax like:

  do_send_email(recipient, cc, subject, body)
is much better for quickly reading code. Plus, reading the docs is hugely important. Having to check every now and then is great for getting you back into them.


before I continue. Have you done any development in Objective C?


Very minimal.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: