.

Solidarity From The ZenSpider |

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?

4 Responses to 'Solidarity From The ZenSpider'
  1. Nathan Eror:

    Funny you should post this. I just ran across some code the other day that used the “class unless is Engrish for “if not.” ;) It’s just not natural.

    Arguments of style can go on for ages without resolution, and a flexible language like Ruby make the arguments that much more contentious. I find that if I err on the side of clarity (even if it means a little more typing), I have a better chance of remembering what the hell I was thinking when I revisit the code later. It’s always good to see that I’m not the only one.

  2. Nathan Eror:

    Damn, I didn’t escape the << first. Here’s my second try:

    Funny you should post this. I just ran across some code the other day that used the “class << self” convention. I, actually, hadn’t seen it before (that I can remember), and it took me an extra few moments to process what its intent was. Once my initial confusion wore off, it was replaced with contempt for the code author. Maybe it’s my heritage as a Java developer, but I like to know if a method is a class method by looking at the method itself. If there are 10 class methods, and I’m only interested in number 8, I don’t want to have to scan all the way up to “class << self” to see that it’s a class method. “self.method_name” is more clear and concise.

    In regards to ternary operators, I love them. My Java and Ruby code is littered with them. Again, it comes down to being concise. If I can easily see, in one line, the intent of a piece of code, I’m a happy programmer. Ternary operators can get unwieldy, but they can be kept under control with good judgement.

    unless is Engrish for “if not.” ;) It’s just not natural.

    Arguments of style can go on for ages without resolution, and a flexible language like Ruby make the arguments that much more contentious. I find that if I err on the side of clarity (even if it means a little more typing), I have a better chance of remembering what the hell I was thinking when I revisit the code later. It’s always good to see that I’m not the only one.

  3. Cory:

    Wow Dirk, I never thought I would say it, but I have the same exact programming preferences as you, in Ruby that is. :)

    I am a big fan of the ternary operator and it was something I missed during my years of Python programming.

Leave a Reply

Moderation Active: Old stuff here... Therefore your comment on this post will be moderated (i.e. don't submit twice !)

    Categories
    Archives

    .