

PHP to Objective C, where are the parameters? - kentnguyen
http://kentnguyen.com/ios/ios-beginner-series-function-names/

======
mmariani
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...](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/ObjCRuntimeRef.pdf)

~~~
kentnguyen
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.

------
roadnottaken
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?

------
RandallBrown
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.

~~~
masklinn
> 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.

------
dqminh
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!")

~~~
masklinn
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.

~~~
nknight
> _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.

~~~
masklinn
> 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.

------
gerggerg
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.

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

~~~
gerggerg
Very minimal.

------
mattmanser
_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.

~~~
masklinn
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).

~~~
mattmanser
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?

~~~
nknight
> _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.

~~~
mattmanser
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.

~~~
nknight
> _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?

