Class names repeating information stated in the package name

By Adrian Smith6 May 2007300 words2 mins to read

Classes in modern programming languages can be arranged in hierarchies, e.g. a perl class might be called Uboot::Message::Mail or a Java class com.uboot.message.Mail.

In some programming languages (e.g. Perl) one always refers to the class by its full name (such as Uboot::Message::Mail) and never by its leaf name (e.g. Mail). For example:

use Uboot::Message::Mail;
my $mail = Uboot::Message::Mail->new();
print "it's a mail" if ($mail->isa("Uboot::Message::Mail"));

In other langauges (e.g. Java) one almost always refers to classes via their leaf-name, such as:

import com.uboot.message.Mail;
class MyClass {
   public void static main(String[] args) {
      Mail mail = new Mail();
      if (mail instanceof Mail) System.out.println("it's a mail");
   }
}

For those languages such as Perl, which require using the class' full path at all times, it's not necessary to repeat information in the leaf name that has been specified already in the path. For example, a class to model an entry in a Uboot address book might be in a directory called Uboot/ABook in which case the entry class can be called Uboot::ABook::Entry.

But in Java, you don't want to have a class called Entry because, as soon as the "import" statement scrolls out of sight, you'll not know if your instance, helpfully statically typed to be an "Entry", is an address book entry, a guestbook entry, a blog entry, or any other conceivable type of entry. In that case the class needs to be called something like com.uboot.abook.ABookEntry.

Class names like Uboot::ABook::ABookEntry or Uboot::Monitoring::MonitoringResult are (only in langauges such as Perl) needlessly redundant and long.

This article was written by Adrian Smith on 6 May 2007

Follow me: Facebook | Twitter | Email

More on: Coding