2

I was going to through this post by Mattt Thompson in which he mentions that

Static functions are nicer than shoe-horned class methods

I just don't see any flaw with Class methods in Objective-C and I could not relate it to C based static functions. Is it something to do with the fact that Objective-C classes are itself objects of metaclasses?

closed as unclear what you're asking by user40980, Doval, gnat, BЈовић, Dan Pichelman Feb 13 '15 at 17:00

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • [A CLASS METHOD is one which is private to the class in which it is located. That is, it may only be called from a METHOD or PROPERTY in the same class. It is invisible elsewhere. The CLASS METHOD must be located within a CLASS block, but outside of any INTERFACE blocks. This shows it is a direct member of the class, rather than a member of an interface.][1] [1]: stackoverflow.com/questions/8089186/… – vvp Feb 13 '15 at 12:44
  • 3
    If you want to know what he meant, it's best to ask him. See Discuss this ${blog} and What is the problem with "Pros and Cons"?. There's no problem to solve here, no criteria to decide which one is "nicer" or when one has been "shoe-horned", and answers that attempt to list off pros and cons tend to overlap or contradict each other (or both). – Doval Feb 13 '15 at 12:47
6

Some languages such as Java have “static methods” that live in a class's namespace, but do not use dynamic dispatch. Instead, they are dispatched statically at compile time (hence their name). Dynamic dispatch is less efficient than static dispatch, since we have to search for the method in all classes of the object we're calling a method on. A central point of OOP is that we do not know the class of the object we'll be calling methods on a priori.

In Java, the properties of static methods make them a good candidate in two use cases:

  • “Class methods” (which Java doesn't have), e.g. some constructor with a better interface. These are problematic in Java since static methods can't be inherited, and classes can't be passed around as objects. The result is often an extra Factory object.

    Objective-C can use real class methods in these cases, which is welcome flexibility. After all, Objective-C has a metaobject protocol, and classes are objects.

  • Low-cost method calls. Since calling a statically dispatched method is much cheaper than invoking a dynamically dispatched method on an object, static methods can be used to optimize code.

    Since Objective-C builds upon the C language, we can always use plain old C functions. C does not have dynamic dispatch, and function calls are very efficient. However, C has no concepts of classes and namespaces, so great care has to be taken to avoid name clashes.

The object-oriented paradigm turns out to be very good at encapsulating complexity. While this makes it easier to design systems, it also makes it more difficult to reason about performance.

The point of the post you mentioned seems to be that using the C subset of Objective-C (i.e. the procedural features) can be a very good idea, since it contains a couple of sensible features the object-oriented parts of Objective-C won't give you. The sentence “Static functions are nicer than shoe-horned class methods” is likely to mean that if you have some function that does not have to be an instance method, using a C function is probably a good – and certainly more performant – solution than putting it into a class method (which would also miss the point of classes-as-objects).

Not the answer you're looking for? Browse other questions tagged or ask your own question.