Wednesday, May 18, 2005

Language Lessons

Java Tech columnist Jeff Friesen has been coding in Java for nearly ten years, and in that time, he's found some surprises in the language, like how += doesn't necessarily do what you expect, or the hazards of invoking a potentially overridden method in a constructor. In this article, he provides some important lessons based on this experience.
summary:
  1. Do not use the string concatenation operator in
    lengthy loops or other places where performance could suffer.
  2. Do not call overridable methods from superclass
    constructors.
  3. Do not use assertions to validate method
    arguments. Use if statements that explicitly throw
    exceptions if those arguments aren't valid.
  4. Use interfaces for flexibility. Use abstract
    classes for ease of evolution or to capture the essence of rigid
    class hierarchies while avoiding the creation of objects that mean
    nothing.
  5. Use covariant return types to minimize upcasting
    and downcasting.
  6. Don't forget the superclass while writing a
    subclass.
  7. Remember that compound assignment operators
    automatically include cast operations in their behaviors.
rating comment: Articles like this on are always useful, cause you can never know enough of these little mysteries!

No comments: