From: Kent Dahl Date: 2002-03-19T22:59:47+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 In this case: def degree if @degree == 0 @scopes.each { | field | @degree += field.length } end @degree end Why? Because it appears that the main function is to return the value, and calculating the value is something that happens more seldom. Both your examples have the computing in the first scope, which I read as what "mainly" or "usually" happens. The first version also conflicts with the doctrine of "single-point-of-exit". My take is basically your second version, but with more emphasis on the condition. I like using unless after statements, if I'm checking for some seldom state. Inversely, an if after statements would suit me if I'm checking for a common state. Does that make sense? -- <[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]> )__(stud.techn.; ind. econ & management: computer technology)__( /"Opinions expressed are mine and not those of my Employer, "\ ( "the University, my girlfriend, stray cats, banana fruitflies, " ) \"nor the frontal lobe of my left cerebral hemisphere. "/