Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I Use self Keyword (Properties) In The Implementation?

I believe I understand properties for the most part. My question is, if I have a property for an instance variable, and I am setting or retrieving it from within a method in my implementation file, should I use self.myProperty or just myProperty? I know either one works, but I have seen mixed conventions, sometimes code accesses the variable directly and other times through the property.

Is there a technical reason for doing this? Is it just convention/personal preference? And I'm not referring to the instances where the parameter name of a method collides with the instance variable name, which might be one reason to use the property (At least, in other languages, I don't know about this one). I assume that when one uses the property within the implementation, they want to take advantage of the way in which they declared the property (i.e. nonatomic, retain), so for example in a method, one can do:

self.myProperty = [someObject someAutoReleasedObject];

instead of:

myProperty = [[someObject someAutoReleasedObject] retain];

Is this the reason? So are there only certain situations in which it would be good to use the property?

I'm new to Objective-C, and this is one of the few things that has me confused. So far I've just accessed the instance variable directly, under the most likely false assumption that going through the property actually calls/sends a method/message and adds unnecessary overhead. I'm pretty sure I'm wrong with this, but even if the difference in overhead is negligible (If there even is any), why would one choose to add it in when one can simply directly access the variable?

I'm pretty sure I'm wrong in my thinking, which is why I'm asking here.

like image 489
Jorge Israel Peña Avatar asked Jun 26 '09 22:06

Jorge Israel Peña


People also ask

When would you use the keyword self?

The self keyword is used to represent an instance (object) of the given class. In this case, the two Cat objects cat1 and cat2 have their own name and age attributes. If there was no self argument, the same class couldn't hold the information for both these objects.

Is self keyword necessary in Python?

Self is a convention and not a Python keyword . self is parameter in Instance Method and user can use another parameter name in place of it. But it is advisable to use self because it increases the readability of code, and it is also a good programming practice.

What happens if you don't use self in Python?

Don't use self when:you want to call an instance method normally; referencing a class attribute inside the class definition but outside an instance method; you are inside a static method.

Do you need self in __ init __?

__init__ does act like a constructor. You'll need to pass "self" to any class functions as the first argument if you want them to behave as non-static methods. "self" are instance variables for your class. 'self' defines if a method is static or not!


1 Answers

First, you should not use setters or getters in init or dealloc according to Apple documentation (and for good reasons).

Other than that, you should generally use the setter for setting the variable if there is one.

Usually I do not bother using the getter for accessing the ivar from within the implementation, but there are times when it is necessary. In particular, if you expect the getter might do some caclulation or checking, or if you want to allow for subclasses to override the behaviour.

Certainly, using the getter in the implementation is more general and safer, but it is also typically pointless and wasteful. Make your choice.

Using the setter is important as it gives an opertunity for other code to observe the changes (Key Value Observing), as well as subclasses a chance to override the setter and make any other adjustments required.

However one thing I highly recommend is to use a different name for your ivar and your property. The normal convention is an underscore prefix (_), although I personally use i_ as the prefix to avoid any confusion with Apple's private usage. That way you cannot accidently use the wrong one:

self.name // use property
i_name // use ivar
self.i_name // syntax error
name // syntax error
like image 57
Peter N Lewis Avatar answered Oct 15 '22 16:10

Peter N Lewis