|
class Fib {
// It's "String[]" not "args[]"
public static void main (String[] args) {
// Variable declarations on a single line
int x1 = 0, x2 = 1;
// For-loop syntax is hard to grasp?
// I guess the hardest thing is to remember
// to count from zero.
for (int i=0; i<10; i++) {
// More on System.out later
System.out.println(x2);
// Temp variables should be local scope
int total = x1 + x2;
// Single line version
x1 = x2; x2 = total;
}
}
}
Now on to the critique of Ruby.
Ruby: x1, x2 = 0, 1
Java: int x1 = 0, x2 = 1;
Good for two variables. What happens when you have ten? How easy is it to keep straight? I think my Java example is easier to keep clear in this case.
puts x2
System.out.println(x2);
I've heard the argument that System.out.println is too long and verbose. I don't buy it. For simplicity, one could write:
PrintStream o = System out;
Once that is specified in your object, you can write:
o.println("Hello");
Of course, writing a line of text might not be your aim, so with Java there are other options:
o.print(...) // no end-of-line character
o.append(...) // appending characters to stream
o.format(...) // formatted output
o.write(...) // writing raw bytes
There's also "System.err" for standard err, the PrintStream that you specify as a redirection to a file, or (my favorite) the OutputStream that is specified dynamically and independent of your section of code.
Ruby: x1, x2 = x2, x1+x2
Yes, I acknowledge you need to specify a temp variable here for Java. Point taken.
On a side note, if the primary goal of Ruby is to reduce the amount of code while improving readability, why include the extraneous do-end? Why didn't Ruby use Python's ommision of do-end in favor of indentation? I think Python's the winner here.
Which brings me to an important point. Let's take Plone in Python as an example of rapid development. Plone -- and Zope by inference -- is an amazing piece of code. It is incredible to me that so little code relatively speaking could do so much. On the other hand, Plone introduced me to what I consider the fundamental flaw in so-called dynamic languages: lack of clear interfaces.
Some have called it a strength that you do not have to be restricted by an interface set in stone. It is however what I consider to be the single greatest reason why Plone is excellent while almost every public extension to Plone is a steaming pile of ad-hoc crap. Dynamic languages as a foundation is like building on sand or mud.
Don't get me wrong. I like scripting languages. I use them extensively; However, I use them for small projects or "the last mile." Sand or mud makes the last step very quick, agile, and complete. But it's a horrible foundation for a large building. Sometimes you need things to be set in stone.
It's all about the right tool for the right job.
|
min, max = find_supremes list
than to either return an array:
int []arr = find_supremes(list);
int min = arr[0];
int max = arr[1];
or pass the variables by reference?
Also suppose you need to switch the contents of two variables. Isn't
x, y = y, x
(or in the "oh-so-unreadable" Perl syntax
($x, $y) = ($y, $x);)easier to read than
{
guessWhatType tmp = x;
x = y;
y = tmp;
}
?