Skip to main content

Dear Professor

Posted by daniel on December 1, 2004 at 5:47 AM PST

Avoid magic, hard-coded text in your programs.

It's another "I never thought of that" moment for me. In today's
Weblogs, John O'Conner
blogs about href="">
Applying the lesson of magic numbers. You are taught early to
eliminate magic numbers so that

 cost = time * rate + parts +
cost = time * rate + parts +
. Now it is more clear what the 25 represented and the
value can be changed in one location. That's not new.

What was new for me was O'Conner's point that you should do the same for text. In his example, don't write code like this:

if  (course.isEnrolled(aStudent)) {
  System.out.println("The student is enrolled.");

Write code like this:

static final String  ENROLLMENT_CONFIRMATION = "The student is enrolled.";
if  (course.isEnrolled(aStudent)) {

Note only does it convey meaning better, it again allows you to change the value of the confirmation to more easily internationalize your application.

Also in weblogs, Andreas Schaefer presents href="">Maven:Jump
Start. He gives an example of why he loves " Maven not only for
its scripting abilities but also for the fact that one could start a
simple project in a few minutes which is even faster than to build a
project with shell scripts."

In Also in
Java Today
, In the JDJ article href=""> Exploring
Enums, Ajith Kallambella writes "the main disadvantage of faking
enumerated types using primitives is the lack of strong typing and
hence the inability to catch errors at compile time. Other
shortcomings are less readable code, deviations from object-oriented
concepts such as encapsulation, the absence of a namespace requiring
an explicit prefix for all references, and the dangers of exposing the
internal implementation to client code."

Programmer Guillaume Desnoix said, "I wanted to run many Java apps at
the same time and to remove the artificial difference between applets,
beans and applications, and between the widget toolkit (AWT, Swing,
SWT, LCDui)," but running multiple applications under one JVM is
actually harder than it looks. To solve the problem, he and
Gérard Collin created JDistro, a desktop for hosting multiple
Java applications. In href="">Juggle
Your Java with JDistro, Howard Wen interviews the developers on
the goals, challenges, and possible uses of JDistro.

In Projects and
, the href="">JDDAC Community's href="">Java Measurement Calculus
Interface project provides a framework for performing operations
on measurements, in particular physical quantities.

The Rome "is a set of
Atom/RSS Java utilities that make it easy to work in Java with most
syndication formats. It includes a set of parsers and generators for
the various flavors of feeds, as well as converters to convert from
one format to another."

Peter Kessler gives some insight into the href="">issues
of implementing of switch in today's href=""> Forums. "I
think one of the key things about switch statements is that you know
(or hope) they are implemented efficiently. If I have a switch with
hundreds of cases I expect the compiler to sort the cases at
compile-time and to use a jump table if the cases are dense, or a
binary search if the cases aren't dense, or a tree of if-then-else's
if there aren't enough cases to worry about. If you require that
switch statements compile to chained if-then-else's, you have broken
the ability to implement switches efficiently."

Dondi_Imperial writes "Because Java is now part of the curriculum
(at least where I'm from) in most computer science courses an
unexpected side effect of adding this feature to Java would be that
teachers would be able to href="">teach
functional programming using Java."

In today's
News Headlines

Registered users can submit news items for the href=""> News Page
using our news
submission form
. All submissions go through an editorial
review before being posted to the site. You can also subscribe to
the href="">
News RSS feed.

Current and upcoming
Java Events

  • December 6-9, 2004 href="">
    TheServerSide Enterprise Java Architecture Workshop
  • December 7-8, 2004 href="">Eighth Jini Community
  • December 13-17, 2004 href="">JavaPolis, 2004

Registered users can submit event listings for the href=""> Events Page using our href=""> events submission
form. All submissions go through an editorial review before being
posted to the site.

Archives and Subscriptions: This blog is delivered
weekdays as the href="">Java
Today RSS feed. Also, once this page is no longer featured
as the front page of
it will be archived along with other past issues in the href="">

Avoid magic, hard-coded text in your programs