Polishing Ruby: as seen on IRC
16:05 srbaker : using < < self to replace the "duplication" in "def self."
16:05 srbaker : is like using method_missing to remove the duplication of leading a in
16:05 srbaker : apple and aardvark
This is very timely since I was just having this discussion yesterday.
To an outsider (aka non-coder) it may be incredibly difficult to imagine that there is much style or choice in coding. Nothing could be farther from the truty.
You can think about it in terms of prose - describing someone getting home from work can take a number of different paths depending on the description, pacing, and mood set by that prose.
A man comes home. He puts his keys on a table. He gets a beer from the fridge. He drinks it.
vs
He stolled in and casually threw his keys onto the counter and headed directly for the fridge. He furiously scanned its contents looking for his prize. He removed the beer carefully, and in a single motion twisted off its cap and poured it down his throat.
Now the point that is being made above is a lot more specific.
You can write code this way in Ruby:
class << self def find_all code_goes_here end def destroy_all code_goes_here end end
or you can write
def self.find_all code_goes_here end def self.destroy_all code_goes_here end
Can you spot the difference?
Both pieces of code have the same behaviour. I’m pretty sure the first one is the more popular construct in the Ruby community. It turns out I prefer the second one.
Why?
Simple - I’m lazy. When I get to a method I prefer to know immediately if it is a class method (self.) or an instance method. By breaking that piece of info and putting it elsewhere in the file I have to go look for it to understand what is going on.
Is it a big deal? No. But a number of small style nits - I strongly perfer it one way over the other. Up until now I thought I was the only person who did (price of writing Ruby in an isolated community) - now I know I’m not alone.
For the curious - the other three that came up -
terinary operators - I love them - most people hate them - assuming they don’t get very long I prefer
def name self.obj ? self.obj.name : “” end or def name if self.obj return self.obj.name end “” end
unless - I can never seem to read an unless statement in the first attempt - I have to substitute the phase “if not” in order to read it - I blame preference for this on Ruby’s Perl philisophical heritage)
protected/private - that serve a small purpose in Ruby (mostly because you can always override them) But I personally don’t like it when people put the keyword in the file making all methods after that point private or protected. I prefer to use the alternate form where you put the keyword at the end of the class and list all methods that should be protected/private. It means you never accidentally make something private or protected and you don’t have to figure out if you are in a special part of the class to add a method. (This last one is probably silly)
Back in the days I wrote C,C++, PHP, and Perl - where you put the curly braces could be a serious argument.
Do you have a programming style preference? What style do you love? What drives you craze?