One feature of Ruby that is often overlooked is the ability to use a Hash object as your method's parameter listing. This is a very useful feature that can make your code a lot easier to read, both for yourself and others. Consider the following initialize method on a fictional class:
class Item def initialize(viewport, x, y, width, height, text, alignment=0, font=nil) # Does things endendIf you were to make a new Item object somewhere in your code, it would look like this:
Item.new(@viewport, 30, 40, 150, 25, "Fancy thing", 2, @font)Now, with that many parameters, it's a bit hard to keep track of what is what, wouldn't you say? Let's take a look at how readability can be improved by using a hash as the method's parameter instead:
class Item def initialize(params={}) params = { :viewport => nil, :x => 0, :y => 0, :width => 50, :height => 25, :text => "", :align => 2, :font => Font.new }.merge(params) # Does things endend
What happens here is that we list the default values for the parameters we expect, then we use the merge method for hashes to overwrite these with the parameters we receive, if they exist. This way we can set default options more clearly for all the parameters (if we want to), and it looks cleaner. But what is even better is when we want to make a new item. We can write it like this:
Item.new(viewport: @viewport, x: 30, y: 40, width: 150, height: 25, text: "Fancy thing", align: 2, font: @font)Which means the code is suddenly a lot easier to read, as you know what each parameter actually is. You could work around this using the standard method by naming all the parameters as variables first, then passing them in, but that's a lot of unnecessary work. Also notice that when your parameters is a hash, you don't have to surround the key and value pairs with brackets when putting them in the method.
However, the above could be even more readable. Here's what I like to do: Group sets of parameters together by their logic:
Item.new(viewport: @viewport, x: 30, y: 40, width: 150, height: 25, text: "Fancy thing", align: 2, font: @font)But we have much more choice in how we want to present our parameters clearly this way. You could also do this:
Item.new( :viewport => @viewport, :x => 30, :y => 40, :width => 150, :height => 25, :text => "Fancy thing", :align => 2, :font => @font)And, order of parameters doesn't matter either, so this works fine too:
Item.new( :width => 150, :height => 25, :x => 30, :y => 40, :font => @font, :text => "Fancy thing", :align => 2, :viewport => @viewport)To access these parameters in your method, you'd have to do this as an example:
def initialize(params={}) params = { :viewport => nil, # -- Removed for brevity -- :font => Font.new }.merge(params) @viewport = params[:viewport] @font = params[:font]endThat's a bit cumbersome, to have to write params for each instance var you want to set. As a bonus, I'll show a more convenient way to do this if you find yourself doing this often:
params.each_pair{|key, value| instance_variable_set("@#{key.to_s}", value)}This would write all the parameter values received to their corresponding instance variables on the current class. Of course, if you only want to set some of them, you could modify it and do this:
%w(x y width height viewport).each{|key| instance_variable_set("@#{key}", params[key.to_sym]}What do you think? Got any tips to share about this topic, and code readability? Let's talk about it 🙂
class Item def initialize(viewport, x, y, width, height, text, alignment=0, font=nil) # Does things endendIf you were to make a new Item object somewhere in your code, it would look like this:
Item.new(@viewport, 30, 40, 150, 25, "Fancy thing", 2, @font)Now, with that many parameters, it's a bit hard to keep track of what is what, wouldn't you say? Let's take a look at how readability can be improved by using a hash as the method's parameter instead:
class Item def initialize(params={}) params = { :viewport => nil, :x => 0, :y => 0, :width => 50, :height => 25, :text => "", :align => 2, :font => Font.new }.merge(params) # Does things endend
What happens here is that we list the default values for the parameters we expect, then we use the merge method for hashes to overwrite these with the parameters we receive, if they exist. This way we can set default options more clearly for all the parameters (if we want to), and it looks cleaner. But what is even better is when we want to make a new item. We can write it like this:
Item.new(viewport: @viewport, x: 30, y: 40, width: 150, height: 25, text: "Fancy thing", align: 2, font: @font)Which means the code is suddenly a lot easier to read, as you know what each parameter actually is. You could work around this using the standard method by naming all the parameters as variables first, then passing them in, but that's a lot of unnecessary work. Also notice that when your parameters is a hash, you don't have to surround the key and value pairs with brackets when putting them in the method.
However, the above could be even more readable. Here's what I like to do: Group sets of parameters together by their logic:
Item.new(viewport: @viewport, x: 30, y: 40, width: 150, height: 25, text: "Fancy thing", align: 2, font: @font)But we have much more choice in how we want to present our parameters clearly this way. You could also do this:
Item.new( :viewport => @viewport, :x => 30, :y => 40, :width => 150, :height => 25, :text => "Fancy thing", :align => 2, :font => @font)And, order of parameters doesn't matter either, so this works fine too:
Item.new( :width => 150, :height => 25, :x => 30, :y => 40, :font => @font, :text => "Fancy thing", :align => 2, :viewport => @viewport)To access these parameters in your method, you'd have to do this as an example:
def initialize(params={}) params = { :viewport => nil, # -- Removed for brevity -- :font => Font.new }.merge(params) @viewport = params[:viewport] @font = params[:font]endThat's a bit cumbersome, to have to write params for each instance var you want to set. As a bonus, I'll show a more convenient way to do this if you find yourself doing this often:
params.each_pair{|key, value| instance_variable_set("@#{key.to_s}", value)}This would write all the parameter values received to their corresponding instance variables on the current class. Of course, if you only want to set some of them, you could modify it and do this:
%w(x y width height viewport).each{|key| instance_variable_set("@#{key}", params[key.to_sym]}What do you think? Got any tips to share about this topic, and code readability? Let's talk about it 🙂