From: Phillip Gawlowski Date: 2010-10-11T15:21:22+09:00 Subject: Re: Why does a lot of code not include parenthesis? On Mon, Oct 11, 2010 at 6:04 AM, egervari wrote: > In the case of the sqrt line, those parenthesis are not required, and > you're right - they are less readable. > > But take for example this line: > >    y = 10.div 10.div 5 > > This is valid code. It produces the result "5". To me, this looks > really bad though. Believe it or not, I can infer exactly what it's > doing much easier by writing it this way: > >    y = 10.div(10.div(5)) > > The order is very clear. And the Ruby parser should actually warn you about the ambiguity. The key to understanding when and why Rubyists (usually) use or not use parentheses is knowing that they are dropped when they are *optional*, and when it is, well, to taste. Same thing with block syntax. Whether to chose do...end or {...} is a matter of taste, and what feels "right" in the circumstances. > See, in Scala, you don't need the "." when you drop the parenthesis, > so dropping them both turns out to be much nicer and clearer: > >    val y = 10 div (10 div 5) This isn't clear to me. Is div called on the 10, or the 5 (or on the 10 and the result of 10 div 5)? The result would be different. But I'm pretty sure that you now consider me a particular brand of unenlightened. But that is because I'm not familiar with Scala, nor on what the Scala community settled on as best practices for writing code. > To me, that actually looks very nice indeed (let's assume div is also > a method on Int in Scala). Of course, dropping all the parenthesis > would change the result to .2 (or 0 I guess). > > I just think this mandatory "." without the parenthesis looks weird > and is unintuitive. I think it's better to drop both, or to have both. > The middle-of-the-road syntax that includes the "." just looks bad. That's more a matter of being used to it, than it being "bad" or "good". object.method makes it obvious that you are calling the #method on the Object, and anything that follows is a parameter you pass into #method. > Of course, once one gets used to it, I guess it'll become readable. Absolutely. A seasoned Perl vet will consider Perl code very readable, too, after all. ;) > Just not the most intuitive, that's all. This has more to do with where you came from (IIRC, the object.method notation is being used pretty much everywhere, from .NET, to Java, to Ruby), and what you are used to, than actual intuitiveness. Of course, nobody can tell you not to use parentheses when you want to, and to use {...} when you want to, or to create a DSL for yourself where you can use "10 div 5" as valid syntax in Ruby. :) -- Phillip Gawlowski Though the folk I have met, (Ah, how soon!) they forget When I've moved on to some other place, There may be one or two, When I've played and passed through, Who'll remember my song or my face.