Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very long class names

Tags:

I try to name a class (also members, properties and so forth) as exact as I can. But sometimes I’m not sure if this is so clever if the class name becomes huge (50 chars and more). The handling is so what uncomfortable and the code becomes difficult to read.

The question occured for me seldom and therefore I don’t have much experience with working with such long names but from time to time (now) it occurs. How other people handle this? Have you an approximate upper limit and then make abbreviations or is it worth the pain to handle such long names?

Update

As requested here an example of such a long class name.

ProjectContractChargingPeriodProjectAccountReferenceVM 

The first Project represents the domain, it may be omitted because the namespace implies already that it handles projects. The problem with that is, that if I do this with this class name, then I must do it with all classes of this namespace and that I definitively don’t like because then many (short) class-names of this namespace will lose their expressiveness. [Project]ContractChargingPeriod describes the object, this class is used for and ProjectAccountReference means that the class is a reference to a ProjectAccount. With ProjectAccount is the same problem as with the ProjectContract. Only using Account is not meaningful because in the app exists also other Account-Classes. The Reference is a little bit weak because in reality it’s a little bit more than only a reference, but this is the general purpose. The VM is an abbreviation I always use and it stands for ViewModel. I think this is legal because everyone who works with WPF knows what VM means.

I have to say, that the class is used to wrap a class out of an ORM that is built with an elder tool I created a long time ago. The classes there represent quasi 1:1 the ERM and I am aware that this is not optimal, but changing it would be a major effort.

like image 750
HCL Avatar asked Jul 10 '10 07:07

HCL


People also ask

Can class name be all caps?

The first letter of class names are capitalized to distinguish class names from member names. The names of constant fields are written entirely capital letters.

How long should Java class names be?

In Windows, the limit for a single filename is 260 characters. The . java file extension takes 5 characters which leaves us with 255 character limit for class names. For UNIX based system most file systems also restrict the length of a file to 255 characters.

How do you properly name a class?

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).


1 Answers

(I use Java/C++, and have used other OO languages, but not C#, what I say here pretty much applies to all the languages I have used)

I use descriptive class names. I don't think I have made it to 50 characters however :-) Generally long class names are not public, and are usually hidden behind an interface and a factory. The interface has a much shorter name than the classes.

One thing you might do is, assuming you have a number of long-named classes that are closely related, put them into a package. One way to spot this is if the classes all have the same prefix word(s). If there are a number of classes that all start with the same name then, perhaps, the name should be a package instead.

like image 131
TofuBeer Avatar answered Sep 22 '22 09:09

TofuBeer