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
href="http://weblogs.java.net/blog/joconner/archive/2004/11/applying_the_le.html">
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 thevalue 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
href="http://weblogs.java.net/blog/schaefa/archive/2004/11/maven_jump_star.html">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="http://www.sys-con.com/story/?storyid=46981&DE=1"> 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="http://www.onjava.com/pub/a/onjava/2004/11/24/jdistro.html">Juggle
Your Java with JDistro, Howard Wen interviews the developers on
the goals, challenges, and possible uses of JDistro.
In Projects and
Communities, the
href="http://community.java.net/jddac/">JDDAC Community's
href="https://jmci.dev.java.net/">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="http://forums.java.net/jive/thread.jspa?messageID=7115&tstart=0#7115">issues
of implementing of switch in today's
href="http://forums.java.net/jive/index.jspa"> 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="http://forums.java.net/jive/thread.jspa?messageID=7032&tstart=0#7032">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
href="http://today.java.net/today/news/">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
the
href="http://today.java.net/pub/q/news_rss?x-ver=1.0">java.net
News RSS feed.
Current and upcoming
Java Events
:
- December 6-9, 2004
href="http://www.theserverside.com/architecture_workshop/index.html">
TheServerSide Enterprise Java Architecture Workshop - December 7-8, 2004
href="http://jini.org/meetings/eighth">Eighth Jini Community
Meeting - December 13-17, 2004 href="http://wiki.javapolis.com/">JavaPolis, 2004
Registered users can submit event listings for the
href="http://www.java.net/events">java.net Events Page using our
href="http://today.java.net/cs/user/create/e"> 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="http://today.java.net/pub/q/java_today_rss?x-ver=1.0">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
href="http://today.java.net/today/archive/">java.net
Archive.
Avoid magic, hard-coded text in your programs
- Login or register to post comments
- Printer-friendly version
- daniel's blog
- 399 reads





