By rubyminer
via rubyrailways.com
Published: Oct 20 2006 / 06:50
Interesting review of some Ruby vs Java idioms, with source code. More problems are solved with both languages for comparison. One of them is pretty funny: it requires 25+ lines of Java code (can you shrink it in a meaningful way?), and 1 line of Ruby code.
Comments
apolci replied ago:
The java version can be shrinked: you can delete the try catch and add a throws Exception without any difference for example (-5 lines). You can also remove one of the two regular expression (-2 lines). You can also inline some varibale used only once without getting a less clear code.
The real problem isn't the language in this example, but the standard library. By using some external library (like jakarta commons-io to read the file) you can do the whole thing with few lines of code.
It's true that Java is more verbose than Ruby, but the example is unfair.
rubyminer replied ago:
Yes, maybe the code was unfair (I guess the tree example is much more to the point).
However I guess it reflects the way a typical Java/Ruby programmer would try to solve such a problem on a typical workday (at least this is how I would do it).
If the task would be to 'golf' the code, I am sure it could be reduced, but normally people are not using third party libraries in Java to open a file... It's just not 'natural'.
However, the Ruby code is, at least to me.
apolci replied ago:
It's true that Java standard library lacks some usefult functions, what I just wanted to do is to point out the difference between the language and the libraries.
About the usage of external libraries, it's quite normal to me to use them, but it can depend on the kind of project you work on.
pascal replied ago:
I think some examples are unfair, for example when comparing with methods that does'nt exist in the standard library : then
you don't compare the language but the library.
By the same way, the gettser/setter example show the lack of knowledge of object programming : an object encapsulates the implementation. Ruby doesn't encapsulates with his accessor concept.
For example, quite simple but that shows the concept : look at a Point class.
You can use x and y attributes to store his properties. Thus the code will be :
Class Circle {
private float x,y;
public void setX(float x) { this.x = x; }
public void setY(float y) { this.y = y; }
public void getX () { return this.x; }
public void getY () { return this.y; }
}
But you can decide to change the way you store your properties, replacing by polar coordinates (radius and angle).
To keep your code unchanged, you want to keep your "accessors" setter and getter on x and y.
Then, your code migrates to :
Class Circle {
private float radius, angle;
public void setX(float x) { radius = some calculation; angle = some calculation; }
public void setY(float y) { ... }
public void getX () { return some calculation; }
public void getY () { ... }
}
I know for a point you'll rather use a setXY (,)
but it's the idea.
Voters For This Link (23)
Voters Against This Link (0)