From: Josh Cheek Date: 2012-01-16T14:41:41+09:00 Subject: Re: The Better Code --f46d041826261d3c6e04b69eab37 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jan 15, 2012 at 6:30 PM, Intransition wrote: > Which would you judge to be the better code? > > The concise: > > def self.assert(*a, &b) > o = Hash === a.last ? a.pop : {} > > t = o[:backtrace] || caller > m = o[:message] > > e = new(m, *a, &b) > e.set_backtrace(t) > e.assert > end > > Or the more explicit: > > def self.assert(*arguments, &block) > options = (Hash === arguments.last ? arguments.pop : {}) > > backtrace = options[:backtrace] || caller > message = options[:message] > > assay = new(message, *arguments, &block) > assay.set_backtrace(backtrace) > assay.assert > end > > Which and why? > Explicit, because I was able to understand it. On the first one, when I got to the new, I was confused about the params being passed in. I also like how it uses parens on the ternary during assignment, because otherwise the syntax is confusing. I don't think it needs the parens after `new` and `set_backtrace`, my preference is the minimum amount of syntax such that the idea is clear. As an asside, it's unclear to me why it's set_backtrace rather than backtrace= --f46d041826261d3c6e04b69eab37--