RSS RSS feed | Atom Atom feed

StumbleUpon
Java's Printf vs OO

I still keep hearing that Java 5's printf mechanism is somehow "a throwback to non-OO techniques". I've heard this comment from several sources, that printf is anathema to an OO language. But I'm afraid I have never heard a rational defense of the claim, so I do not understand how
println("The value of " + f.get(g) + " is " +  
&nbspnew DecimalFormat("00.0").format(k) + ".")
can be defended as either "more object-oriented" or in any manner even "better coding" (certainly not on the grounds of readability) than
printf("The value of %s is %3.1f.", f.get(g), k)

To say that "constructing a new DecimalFormat specifically to represent "%3.1f" is good" strikes one as a claim against all use of convenience methods, from the "pain is good for you" school of coding style. :-)
Maybe some of the critics are confusing OO with "compile-time checking". A printf string is a specification and can be checked either dynamically or statically. At present they are checked dynamically (at run time) but there is a good likelihood that high-end tools such as Eclipse or Netbeans will gain printf checking support. Don't think it's really being typechecked already? Check this:
 Exception in thread "main" java.util.IllegalFormatConversionException: 
f != java.lang.Long
Of course the debate on dynamic type checking vs static type checking could go on forever...
Meanwhile, the only sensible objection to printf that I have heard from Java people is that it's harder to internationalize, and that may be true. And if you want to internationalize a bunch of things (not just the Date/Time/String things that Java supports), writing a Factory for them is a good OO approach. But you can still use this approach with printf. So having gone full circle, I have decided that I'll keep using it.
Tags :

StumbleUpon
Sensible by Default

Getting it right in OpenBSD

One of OpenBSD's mottos is "Secure by Default", but a more general form might be "Sensible by Default." I was astonished to find out the other day that some of the Linux distributions apparently produce CD-ROMs that will wipe out your hard disk if you just boot them and press ENTER enough times. With the OpenBSD installer, one of the first few questions is
Proceed with install? [no]
. So if you're not paying attention, you don't get to install, but you don't wipe out your existing partition either. That is OpenBSD: Sensible by Default.
Tags :