Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Naming convention: What about abbreviations in keys or IDs of type java.lang.String?  RSS feed

 
Avor Nadal
Ranch Hand
Posts: 156
Java Netbeans IDE Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

I've been programming in Java for several years. I started being very obsessed with compacting my code, over-using abbreviations and acronyms. But later I realized that many people was right and it was more useful writing well structured code and, specially, using full names that were descriptive. Code maintenance nowadays is not a pain. Indeed, I'm enjoying it.

However, from time to time, I face situations where some class or method names become too long. For example, this is very typical when I'm modelling the structure of a relational database, and map the tables and their columns to classes and properties in Java.

But it's even worse when I've to export this information as text, in a key-value manner, to an external application or media. Because each pair use a key or ID of type java.lang.String, which is built combining the object name, plus a dot, plus the property name. This is done this way to avoid ID clashes, because properties of different classes may be named the same way. The main consequence is that the IDs turn into really, really long ones:



In this case it's not complicated to read them, because they're listed one by line. But when the keys are the IDs of the fields of a form, for example, these long names complicate the reading of the code a little.

Taking into account that many tutorials recommend not to use abbreviations at all in class, method and variable names, Would you apply this "rule" to these keys or IDs too? Would you use abbreviations instead? Or would you simply ignore such a strict recommendation and use abbreviations wherever they're necessary (classes, methods or variables), setting a maximum length of X characters.

Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use descriptive and readable names everywhere. "Readable" can also mean "balanced"; names that are too long can hinder readability just as much as abbreviated names, so apply reason.
 
Avor Nadal
Ranch Hand
Posts: 156
Java Netbeans IDE Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault: Thank you for your answer. I'll continue using full names then ;) .

I agree with you about keeping the names "balanced". Until a few weeks ago, I used to add too many adjectives to the names of my classes in order to prevent future collisions. However, many of these collisions were very unprovable. For example, I used "TechnicalRegion" even though there wasn't any other type of region in my application. Now, I simply use the name "Region" instead. Much cleaner and more... Reasonable.

I've to admit that my worry about using "long" names in IDs also had to do with the performance of certain operations, like the Strings comparison and the hash calculation. I know, premature optimization is evil, he he he.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, in the example you gave, I would have left the adjective on. But it's a judgment call. And, assuming you are using a reasonable IDE, refactoring names should be easy.
 
Avor Nadal
Ranch Hand
Posts: 156
Java Netbeans IDE Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault: Sorry for the delay. Finally I decided to rename the class taking the adjective back, as you suggested ;) .

Some days ago I realized that it was more reasonable to put the "effort" in cutting down the variable names instead of the class names. Because the former are in a more specific context and it makes more sense. Thank you.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avor Nadal wrote:Some days ago I realized that it was more reasonable to put the "effort" in cutting down the variable names instead of the class names. Because the former are in a more specific context and it makes more sense. Thank you.


I believe that comes under the heading of "balanced" that Bear initially mentioned. And I agree--context is important. In addition to the class vs. variable judgment call that you made, I tend to be more long-winded with my member variable names than local variables, because they are used in more varied contexts, while local tend to be used in a narrow enough context that you can derive their meaning more readily from that context.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!