Talking to yourself is bad mmkay?
A lot of languages encourage talking to yourself. Lots of OO PHP code is sprinkled with code that looks like $this->foo_method();
. In some languages it's necessary. Ruby isn't one of them.
class Foo
def bar
# Why are you talking to yourself?!
@thingy = self.foo
end
def foo
"QUUX!"
end
end
The code above could be written without self
at all.
class Foo
def bar
@thingy = foo
end
def foo
"QUUX!"
end
end
While this is a (very) trivial example, it makes a huge difference on larger code-bases. Give it a try: if you don't talk to yourself your code will look less crazy.
Only one caveat: when you're doing assignment you'll need to talk to yourself unless you're doing a local assignment.
class Foo
attr_accessor :thingy
def bar
# This will assign to a local variable.
thingy = foo
end
def foo
"QUUX!"
end
end
class Foo
attr_accessor :thingy
def bar
# This will call Foo#thingy=
self.thingy = foo
end
def foo
"QUUX!"
end
end
You can verify that I've written this post by following the verification instructions:
curl -LO http://barkingiguana.com/2008/10/20/talking-to-yourself-is-bad-mmkay.html.orig
curl -LO http://barkingiguana.com/2008/10/20/talking-to-yourself-is-bad-mmkay.html.orig.asc
gpg --verify talking-to-yourself-is-bad-mmkay.html.orig{.asc,}
If you'd like to have a conversation about this post, email craig@barkingiguana.com. I don't bite.