[H-GEN] Which is better?

Russell Stuart russell at stuart.id.au
Wed Apr 28 18:56:10 EDT 2004


On Wed, 2004-04-28 at 23:02, Andrae Muys wrote:
> Fine.  Except of course that everytime someone tries to emphisise the 
> importance of readability, someone else seems to want to respond 
> "Readability is important, as long as you minimise heap allocation". 
> The sentiment is not only wrong, but seriously harmful.  IF you are 
> doing OOP then heap allocation is background noise, not worth thinking 
> about.  OTOH if you want to program Java, the whole language from design 
> to implementation will oppose any attempt to write use a different paradigm.

Somehow we drifted off the point here.  Harry, a new programmer, was
searching for the good way to write code.  Its like bowling a cricket
ball - everybody does it differently, and you have to choose a style
that both works and is comfortable.  So the beginner copies what other
have done, invents a few things for himself, and asks advice from his
coach.

This is what Harry was doing, and so he should be.  He asked about
optimising for loops, and was told it generally was not worth the
effort.  Interestingly, I was actually able to prove (at least to my
satisfaction) that all loop forms take amount of time to execute under
1.5.0 beta, something I am willing to bet no one here knew before.

So I gave Harry a tip that would be helpful - beware that memory
allocation in Java is not a cheap operation, like say i = i + 1.  the
"new" operator is not a VM op code - it is a function call, and a fairly
expensive one at that.  This statement is not contentious.  It is
precisely why Java has a StringBuffer.  StringBuffer's generally don't
enhance readibility, usually the reverse in fact.  But if you are going
to build up a large String in Java, then good coding practice says you
should use a StringBuffer.

Good coding  practice also says you should also write:

  static SimpleDateFormat f = new SimpleDateFormat("...");
  String formatDate(Date date) {
    return f.format(date);
  }

Instead of:

  String formatDate(Date date) {
    SimpleDateFormat f = new SimpleDateFormat("...");
    return f.format(date);
  }

If you wrote a date formatting class (an admittedly contrived example),
this:

  class DateFormatter {
    SimpleDateFormat myFormat;
    public DateFormatter(String format) {
      this.myFormat = new SimpleDateFormat(format);
    }
    public String format(Date date) {
      return this.myFormat.format(date);
    }
  }

Instead of:

  class DateFormatter {
    String myFormat;
    public DateFormatter(String format) {
      this.myFormat = format;
    }
    public String format(Date date) {
      return new SimpleDateFormat(this.myFormat).format(date);
    }
  }

This is not contentious.  Its also not about readability as both
versions are equally readable, to me at least.  And its not just about
optimising loops.  The advice "try to minimise memory allocation" does
seem to cover it, though.

It does seem from the reactions I get on this list that to suggest
someone should write reasonably optimal code is not politically correct
in programming circles.  God knows why.  It is an essential part of the
art of programming.  Your have to strive to write readable and tight
code.  It is often a trade off.  There is no right answer; it takes
experience just to get the balance right most of the time.  Telling a
new programmer to ignore optimisation entirely would make him get the
balance wrong all of the time, if he followed the advice.

Fortunately most don't.  Most over optimise.  This is not a bad thing. 
Just like bowling a cricket ball, your have to discover all the things
that don't work, and you can only do that by trying them.  So my advice
to Harry would be to optimise to his hearts content.  But do to try to
measure the effect of what you have done - most of it won't be worth it.





More information about the General mailing list