Method, know thy $self

jjohn on 2002-04-09T18:49:10

Programming pet peeve #24:

Classes whose methods use a different variable name than "$self" to refer to the obligatory object reference. Sure, you might be tempted to use $this or $obj, but don't. I, and the majority of module writers on CPAN it seems, expect $self.

Drive on through. Thanks for playing!


But why?

pdcawley on 2002-04-09T21:09:09

Frankly, I don't give a bugger what you call $_[0], so long as you're consistent and the name you choose tells me what it's for. Personally, I choose self, for fuzzy reasons that I really must get round to articulating (so I know what they are...), but I'm not dogmatic enough about it to say anyone else should do different.

Re:But why?

jjohn on 2002-04-09T21:28:52

Because surprises aren't amusing when you're maintaining other people's code. Following conventions helps avoid stupid mistakes. For a while, I switched to using '$this'. Bad, bad jjohn. The switch served no purpose.

Normally, I'm pretty laid back about style, but this bugs me.

Re:But why?

pudge on 2002-04-16T11:32:23

Bah! Foo! Just look at the first line of the function, and see all the wonders that might be some other name.