I have always looked at ruby code and thought the syntax seemed quite readable. Having explicit repeated sections like `self.` as instance variable in python seems just noisy and I prefer the more compact @ruby way.
Rails seems to be architected with a controller class `instance` per request, the view context sourced from the controllers instance variables.
def method
@a = 'hello'
@b = @a * 4
@c = @b * 3
end
Compare this with the typical python web paradigm. Instance variables (self.xxx) are unused as controller instances are shared between requests. Locals are built up first and then redeclared, returned as a context dict.
def method(self):
a = 'hello'
b = a * 4
c = b * 3
return dicta=b=c=
The example is very basic but note in python you couldn't even validly do the following
def method(self):
return dicta='hello'b=*4c=*3
`a` hasn't been declared until the dict structure has been parsed therefore you can't reference it to build `b`. At best you can do
def method(self):
a = 'hello'
return dicta=b=*4c=*3
I have always hated that pattern (as do some other pythonistas) and would generally return locals()
def method(self):
a = 'hello'
b = a * 4
c = b * 3
return locals
But what if you want to convert your context to JSON? At the very least you have the `self` controller instance in your context. This doesn't convert very well to JSON literals. You then have to filter keys from your context dict. It's supposed to be the Python Way to be explicit ( ie declaring your context vars rather than using locals() or horrible _getframe hacks to try and remove some repetition )
Beautiful is better than ugly.
Explicit is better than implicit.
To me, being explicit in Python (in this case) breaks the first commandment; it is fugly. Contrastingly, the compactness of the @ruby way, a trifling detail, allows a quite sweet pattern. Just prefix any variables you want in the context with a @. I appreciate it not just from a purely visual sense of aesthetics; practically it would be easier to work with.
How on earth is using @ here less readable or uglier on a whole? Could it be better? maybe .
Anyway, `It's all subjective`, `Beauty is in the eye of the beholder` and `Ugliness is to the bone` and other cliches.
No comments:
Post a Comment