by kragen · 15y ago · 0 comments
In my case, I've been surprised at how much less syntactic overhead Ruby imposes than Python (despite the explicit "end"!), and how much it matters. Compare:
def mouse_clicked
x = mouse_x / @cellsize
y = mouse_y / @cellsize
if @cells[x][y] == :empty
set x, y, :wire
elsif mouse_button == LEFT
set x, y, :empty
else
set x, y, :electron_head
end
end
def mouse_clicked(self):
x = processing.mouse_x() / self.cellsize
y = processing.mouse_y() / self.cellsize
if self.cells[x][y] is Empty:
self.set(x, y, Wire)
elif processing.mouse_button() == processing.LEFT:
self.set(x, y, Empty)
else:
self.set(x, y, ElectronHead)
(Here ElectronHead and the like are assumed to be top-level empty classes in the module; the Ruby equivalent is a Lisp-style atom that doesn't need to be declared.)To me, the Ruby version has a much higher signal-to-noise ratio.
Another thing: JRuby seems to be a lot more mainstream among Rubyists than Jython is among Pythonistas, which I think means it's a bit more solid.
Explicit is better than implicit.
Errors should never pass silently.
Namespaces are one honking great idea -- let's do more of those!
Maybe after I spend more time with Ruby, I'll see the dark side of DSLs, but so far I'm loving it :)
from processing import ...Because they say the same thing, but the Python code is much longer, if you count tokens instead of lines. It has about 28-30 more tokens than the Ruby code does, about three per line. Those tokens don't convey any information. They're just redundancy, i.e. noise. And there are a lot of them; these redundant tokens are something like a third of the code.
Now, redundancy can be useful (see e.g. http://www.paulgraham.com/redund.html) but it is costly, so you should keep it to the minimum that achieves what you want the redundancy to achieve.
I wanted to translate the code to Haskell directly, but the idioms don't map that well (self.cellSize would becomes something else entirely, and the comparison would not be fair).
from processing import LEFT, mouse_x, mouse_y, mouse_button
Now you don't have to prefix things with `processing` just like you don't in your Ruby version.It does call fill from four call sites, but that was because there's no reasonable way to return a list of four arguments from a method in Ruby, as far as I can tell.
p, q, r, s = * method_that_returns_array_of_4_things
* aka "splat operator"