* Mark Mielke <mark@mark.mielke.cc> [2008-06-23 15:55]: > Another common bit of code that I've written is: > > sub foo > { > my($this) = @_; > exists($this->{'foo'}) ? $this->{'foo'} : do { > ... expensive calculations ... > $this->{'foo'} = ...; > }; > } > > This introduces the other statement that is frequently used to > return the last value without a return - do{}. The above code > is not significantly improved by adding return anywhere. Yes it would. You could lose a level of indentation and it would be much easier to skim if you converted the truth case to a guard clause: return $this->{ foo } if exists $this->{ foo }; >> It is never ever a concern of mine to make code readable to >> people who are unfamiliar with the language. What concerns me is >> how to communicate intent to subseqent readers of the code. > > I agree - what we disagree with is whether an explicit 'return' > successfully communicates intent in any way more than an extra > pair of parens would. For example, I often see people doing: > > statement if (condition); > > Those last parens are not required. If I ask people why they > put them, they tell me that it is to communicate precedence to > the user. I strongly disagree - by dumbing down the language by > making every precedence explicit, the result is programmers who > do not understand the language, which might mean they > understand your code today, but not understand somebody else's > code tomorrow. "I thought you needed extra parens?" or "I > thought you needed a return?" - "what made you think that?" - > "Aristotle's code uses it all the time and he knows what he is > doing" - "yes, he does - he's dumbing it down for you." :-) Btw, why did you use quotes in `$this->{'foo'}`? They’re not required. > Perl is a dynamic scripting language. That it doesn't support > the 'void' return type is not a reason to explicitly return; on > every subroutine that does not have a value to return. This > would become almost ridiculous for those of us who write many > small subroutines instead of a few large subroutines. And those of us who prefer to use guard clauses and not contort subroutines into nested single expressions cannot avoid `return` at all. Regards, -- Aristotle Pagaltzis // <http://plasmasturm.org/>Thread Previous | Thread Next