views:

108

answers:

2

Hi

I was wondering if there are any memory/performance drawbacks, or just drawbacks in general, with using Class Methods like:

+ (void)myClassMethod:(NSString *)param {
// much to be done...
}

or

+ (NSArray*)myClassMethod:(NSString *)param {
// much to be done...
    return [NSArray autorelease];
}

It is convenient placing a lot of functionality in Class Methods, especially in an environment where I have to deal with memory management(iPhone), but there is usually a catch when something is convenient?

An example could be a thought up Web Service that consisted of a lot of classes with very simple functionality. i.e.

TomorrowsXMLResults;
TodaysXMLResults; 
YesterdaysXMLResults;
MondaysXMLResults;
TuesdaysXMLResults;
.
.
.
n

I collect a ton of these in my Web Service Class and just instantiate the web service class and let methods on this class call Class Methods on the 'Results' Classes. The classes are simple but they handle large amount of Xml, instantiate lots of objects etc.

I guess I am asking if Class Methods lives or are treated different on the stack and in memory than messages to instantiated objects?

Or are they just instantiated and pulled down again behind the scenes and thus, just a way of saving a few lines of code?

A: 

In my experience, class methods, or in my definition static functions, serve specific purposes. One of them CAN be performance, but only if they are small and not dealing with a lot of data. (i.e. NSString stringWithString). If you are dealing with a lot of data, your performance hit, as you are probably aware, is in dealing with the data, not the instantiation of an object. Stick with focusing on the dealing with the time consuming task as opposed to the overhead of creating objects to handle the task.

SPECIFIC ANSWER: Class Methods are loaded at load time, and are always available to your application, the code overhead for loading them via a class instantiation is minimal compared to the large amount of work you describe. (They will most likely be cached anyway)

Kenny
performance is EXACTLY the same with class methods vs instance methods. Class methods are treated at runtime like any other methods (eg. instance methods), and while the CLASS may be loaded at runtime, the methods themselves are C functions, same as instance methods, and the POINTERS to those functions are cached, just like instance methods. There is no 'class instantiation', although there is a small few methods sent on the first use of the class, but this happens only once in the lifetime of the app, is minimal, and happens regardless of whether it's an instance method or a class method.
Jared P
By loading THEM I was referring to having non class methods and having to instantiate an object to perform some work as opposed to statically declared class methods.
Kenny
Hi Kenny and thank you too:)I can see that some of my fears are premature, I guess as long as the different methods helps to make my code more-readable and there is no performance hit, I should just be happy. I am not doing things that are memory or CPU intensive.
RickiG
+3  A: 

Short answer: no downside - use as expected

Long answer: Classes in objective-c are actually objects, that you can use like anything else (check the return type of -[NSObject class] -- a pointer to an obj-c object). When you call [yourclass alloc], you're actually sending a message to yourclass, which is an object describing the class to the runtime. (the method itself is a bunch of wrappers around malloc(), so there's not exactly any magic involved.) As far as how these objects are handled, ALL objects in objc, including classes, are allocated in the heap, so the stack plays no part. EDIT: Just to be clear, there is no difference in using a class method as opposed to an instance method, except that with a class method you do not need to have an instance of the class.

for further reading on how these class objects are implemented, I recommend http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

Jared P
Hi Jared PThank you for both the explanation and the link(which I had to go over a few times..). I think the notion of Objective C being a subset of C helps me understand better, there is a clear common denominator that everything must boil down to.I will keep this thread in mind when I read other peoples code to see what the patterns of using Class Methods are.
RickiG