From: David Masover Date: 2010-10-15T15:03:55+09:00 Subject: Re: Why does a lot of code not include parenthesis? On Wednesday, October 13, 2010 07:51:19 pm Eric Christopherson wrote: > On Tue, Oct 12, 2010 at 6:20 PM, David Masover wrote: > > On Monday, October 11, 2010 09:58:40 pm ara.t.howard wrote: > >> the only real argument for requiring parens is so that > >> > >> o.foo # returns the method foo > >> > >> as in javascript. other arguments are mostly religious. > > > > I actually really like this about JavaScript, but as it turns out, > > JavaScript has getters and setters. I'm guessing they've been added to > > the language itself because they pretty much always existed as part of > > the browser's API -- think "location.href = foo" -- so it makes sense to > > expose them to developers. > > > > So here, the only real question is whether it's more common to want to > > deal with methods directly this way, or to want to refactor instance > > variables into methods. I like that Ruby's approach has everything > > entirely encapsulated by default -- attr_accessor means I always have > > the option to replace an instance variable with a pair of methods. True, > > I have setters and getters in JavaScript, but they're the default in > > Ruby. > > I'm not sure I follow. It sounds like you're saying that the > expression "o.foo" can mean either "invoke method foo on object o" or > "instance variable foo of object o", depending on how o is defined. My point was that JavaScript is like this, while Ruby is not. In JavaScript, ordinarily, 'o.foo' can only ever mean an instance variable (it has to be o.foo() to be a method, and if foo is a method, o.foo just gives you the function object for that method). However, we can also specify a function which will be called whenever someone tries to access 'o.foo'. In other words, JavaScript makes it obnoxiously more tedious to either use normal setters and getters (for example, o.foo, o.setFoo(), and o.getFoo()), and even more tedious to define first-class setters and getters (so trying to set or get o.foo actually calls your custom code instead). However, it makes it very easy to remix objects, with far more flexibility and with a far more primitive concept than mixins. Ruby, by contrast, makes setters and getters easy (and required), which means encapsulation is much easier -- but it makes certain kinds of code re-use harder, and forces you to use classes and modules, whereas JavaScript starts out with something simpler (prototypal inheritance), and allows you to pretty much build any inheritance model you like on top of that. Anyway, this is all about syntax. JavaScript makes it easier to do things one way, Ruby makes it easier another way, but everything I mentioned should be possible in both. It's just a question of how ugly it will be.