Dear Professor
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
Applying the lesson of magic numbers. You are taught early to
eliminate magic numbers so that cost = time * rate + parts +
25 becomes cost = time * rate + parts +
SHOP_FEE. 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)) {
System.out.println(ENROLLMENT_CONFIRMATION);
}
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 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 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 Juggle Your Java with JDistro, Howard Wen interviews the developers on the goals, challenges, and possible uses of JDistro.
In Projects and Communities, the JDDAC Community's 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 issues of implementing of switch in today's 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 teach functional programming using Java."
In today's java.net News Headlines :
- BerkleyDB Java Edition 1.7.0
- AWbench AOP Benchmarking Project Released
- JAdminUsers Initial Public Release
- New Java Tools for BlackBerry
- Ogg for Java 0.1.0
- SOFIA 2.3
Registered users can submit news items for the java.net 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 thejava.net News RSS feed.
Current and upcoming Java Events :
- December 6-9, 2004 TheServerSide Enterprise Java Architecture Workshop
- December 7-8, 2004 Eighth Jini Community Meeting
- December 13-17, 2004 JavaPolis, 2004
Registered users can submit event listings for the java.net Events Page using our 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 Java Today RSS feed. Also, once this page is no longer featured as the front page of java.net it will be archived along with other past issues in the java.net Archive.
Avoid magic, hard-coded text in your programs- Login or register to post comments
- Printer-friendly version
- daniel's blog
- 359 reads





