I posted this long time ago in another incarnation of the blog, but I figured it is worth revisiting again. The original was found on [tag]Cay Horstmann[/tag]’s homepage.
1980: [tag]C[/tag]
printf("%10.2f", x);
1988: [tag]C++[/tag]
cout << setw(10) << setprecision(2) << showpoint << x;
1996: [tag]Java[/tag]
java.text.NumberFormat formatter
= java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++)
System.out.print(' ');
System.out.print(s);
2004: [tag]Java 5.0[/tag]
System.out.printf("%10.2f", x);
Funny but true. :mrgreen:
[tags]progress[/tags]
Gotta admit that’s pretty funny. I’m a C programmer mostly so can’t help but wonder why people try to reinvent the wheel. Of course I am also well aware of the faults of printf, and actually kinda like cout. However I have serious problems with the 1996 Java code example. I was actually trying to learn Java at about that time. Lots of fun, huh.
And thanks for the link to Cay Horstmann’s homepage. I love some of his quotes. haha.
“Our great democracies still tend to think that a stupid man is more likely to be honest than a clever man, and our politicians take advantage of this prejudice by pretending to be even more stupid than nature made them.” Bertrand Russell
I thin the Java example is a little over exaggerated to make the joke work. But yes, some simple things require many lines of Java to implement. But it does make sense in an enterprisey kind of way.
Once you set up that NumberFormat object you can just pass it around and reuse it. And if the format changes, you just go back and modify the 2 initialization lines instead of hunting down the 350 printf statements in your code.
Java 5.0 cuts down on the verbosity a little, and reintroduces introduces many old-new features into the language. It’s a step in a right direction.
Oh, and that is the best quote evar! lol
I like Java don’t get me wrong there. I actually like all the C derived languages. And yes I’m slowly but surely getting used to the OO paradigm. I certainly understand why Java and the other OO langs “make sense in an enterprisey kind of way.” The beauty and danger of C is that it allows ya to do whatever ya want to do, good or bad. “hunting down the 350 printf statements in your code” would be a great example of poor coding not a fault of C. There are ways of dealing with this situation. Both good and bad ways. Bad being macros, good being keeping the print statement in a function, or use a formatting string defined in a header file. I adore C because mostly I’m a glutton for punishment, what can I say. But I do try to keep my code readable and follow some kinda rational pseudo-object oriented coding paradigm. :)
Yeah, definitely – C can be very structured and reliable when it wants to. I actually have great respect for C programmers because of the fact that they will often need to deal with stuff like memory deallocation, array bounds and etc… Working in Java I blisfully ignore all these things, because the language does them for me.
So whenever I do need to write some C I susually forget to do all that stuff and get slammed for it when someone reviews my code. Force of habbit. :P
The 350 printf statements was a bad example, but you know what I mean. I totally agree this is not a feature of the language. Bad programmers will keep producing bad code regardless of the language or environment you put them in. :)
Somewhat related, as programming jokes go.
Hehe… Oldie but goodie. I love how the college senior code snippet is in lisp. :)