From: "Guy N. Hurst" Date: 2002-03-20T00:53:39+09:00 Subject: Re: style choice Ron Jeffries wrote: > > A style question for the community ... which of the following do you prefer, and > why? Or, if you prefer some third version, what is it and why? > > def degree > return @degree if @degree > 0 > @scopes.each { | field | @degree += field.length } > @degree > end > > def degree > @scopes.each { | field | @degree += field.length } if @degree == 0 > @degree > end > For me, it depends. I would use the first form for things like handling error conditions: def mymethod(param=[]) return nil unless Array===param # rest of code expecting param to be an array end But in your case, it is an instance variable and possibly always known to be a number, so maybe I wouldn't need that form there. The second form uses an if-modifier after a block, which seems unusual to me, since I don't usually modify an iterator. Instead, I would typically loop over a conditional. In this case, I would tend to go for a third form, I think. def degree if @degree.zero? @scopes.each{|field| @degree += field.length } end @degree end This seems balanced to me. Guy N. Hurst