There is one feature of the Objective C language that I really love: the method naming. Let me explain with an example.

Here is a nasty call to a C function from the Win32 API that has 12 arguments:

hwnd = CreateWindowEx(WS_EX_LAYERED,
                      TEXT("Hello"),
                      TEXT("World"),
                      WS_OVERLAPPEDWINDOW,
                      10, 
                      10,
                      400, 
                      400,
                      NULL, 
                      NULL,
                      hInstance, 
                      NULL);

Pick an argument, any argument. What does it do? You can probably guess a couple of them, but basically you’re forced to look up the documentation. Sure, 12 arguments is a bit excessive, but even three or for argument functions can be ambiguous. What if you’re trying to understand a function call that is being passed three number literals as arguments? Even if you know the function, you’ll probably have to look up the documentation just to remember the order of the arguments.

Now for the equivalent in Objective C:

hwnd = [SomeClass createWindowExWithExtentedStyle:WS_EX_LAYERED
                                        className:TEXT("Hello")
                                       windowName:TEXT("World")
                                            style:WS_OVERLAPPEDWINDOW
                                                x:10
                                                y:10
                                            width:400
                                           height:400
                                           parent:NULL
                                             menu:NULL
                                         instance:hInstance
                                            param:NULL];

Aren’t the named arguments great?

At first it feels redundant when you’re writing method declarations because the method name segments are basically the same as the argument name. The method declaration for the above method would look like this:

+(HWND) createWindowExWithExtentedStyle:(DWORD)extendedStyle
                              className:(LPCTSTR)className
                             windowName:(LPCTSTR)windowName
                                  style:(DWORD)style
                                      x:(int)x
                                      y:(int)y
                                  width:(int)width
                                 height:(int)height
                                 parent:(HWND)parent
                                   menu:(HMENU)menu
                               instance:(HINSTANCE)instance
                                  param:(LPVOID)param;

Yes, it is a bit redundant in the declaration, but reading code that calls the method is so much easier. Whenever I write something in another language, I miss my named arguments.

  • Erik

    It’s a great improvement over C/C++, but Python goes even one better – you can supply the named parameters in any order when invoking the function/method, you don’t have to supply all of them (functions and methods can declare default values for any unsupplied parameter), and you can define functions and methods that take named parameters that don’t exist until they are called – the function body just gets a dictionary that contains whatever name/value pairs were passed to the function. It would be very cool to have this much power in Objective-C.

  • willc2

    Box a large number of parameters in an NSDictionary, wrap the method call in your own method that checks the dict and supplies reasonable defaults for missing keys. Easy peasy lemon squeezy.

  • Priyavarshini13

    its gud. well, is TOM similar to objective C? what is TOM exactly?????? pls reply.