tags:

views:

29

answers:

1

Which is better practice; manipulating properties with accessors by @property or self.property?

+1  A: 

If you are just using straight accessors, then stick to @property (unless you come from Python and are turned off by the @ sigil hehe) otherwise:

It's entirely up to you. But self.property can be useful in some circumstances where you need to ensure the property is initially set up:

def property
    @property ||= []
end

# don't need to check if @property is `nil` first
self.property << "hello"

Also beware that there is a slight overhead to using self.property over @property as self.property is a method call.

NOTE: The reason i'm using self.property over just property is because the corresponding setter method property= requires an explicit receiver: self.property=, so I choose to use the explicit receiver with both.

banister