• Post Reply Bookmark Topic Watch Topic
  • New Topic

The Dangers of using Horrible and Evil Names  RSS feed

 
Tom Piersa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As promised,

We’ll, I recently started a thread on a problem I was having getting a tag library to compile. I received some good pointers here and for that I say thank you.

A few people hinted that my choice of naming things had a little bit to be desired and recommended this article: http://www.javaranch.com/style.jsp Good article. Now that the issue is resolved, I have the time to clean up the code.

I think I understand 2.1

nypldap.java and nypldap.class should be NYPLDAP.java and NYPLDAP.class. I took care of that. Class and file name has been changed. But when I declare a local instance of NYPLDAP what should I call it?

NYPLDAP onypldap= new NYPLDAP();

Or

NYPLDAP oNYPLDAP= new NYPLDAP();

Thanks,

Tom

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the "o" for? To indicate it's an object? That'll get old pretty fast, since everything but primitives are objects. Name it the closest to what it represents. If the best that can be come up with is "nypldap", then just name it that.

Capitalized names are generally reserved for static constants. Personally I wouldn't name the class NYPLDAP, either, but there's some divergence in the Java community when dealing with acronyms: some people will write "FooHTML", others "FooHtml", and so on. I'm in the latter camp, because it makes reading longer names easier. HTMLPDFGenerator bad, HtmlPdfGenerator good, IMO.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is that . Class should have name as NypLdap, if NYP and LDAP are two thing else it could have been Nypldap. and ref variable should have been all small.
 
Tom Piersa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, thanks for the quick reply. NYP and LDAP are acronyms. As such, in all of the examples, they are shown in caps. So, I think the best answers to this is:

NYPLDAP nypldap= new NYPLDAP();

Or

NYPLDAP NypLdap= new NYPLDAP();

Thanks again,

Tom

 
M K Rayapudi
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Piersa wrote:NYPLDAP nypldap= new NYPLDAP();
Or
NYPLDAP NypLdap= new NYPLDAP();

As Rahul said, is more readable.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variable names shouldn't begin with capital letters--classes start with capital letters.

See http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html

Even in the Java API there is disagreement (HttpRetryException, for example, as compared to URLXxx, the XmlReader interface as opposed to the XMLReader class, etc).
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Tom, It is not that what API said what? They are guidelines based on vast experiences. What is more readable, you can decide on your own and choose your style with one major consideration that others should not get confused by those names.
 
Tom Piersa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the things I saw in the guide was that Hungarian Notation should not be used. In other languages, I accept all user input as string. Then, after it passes validation, I convert it to the desired data type. So what starts out as strMyNumber gets converted to lngMyNumber. I can then use lngMyNumber for arithmetic and strMyNumber to concatenate with other strings to build an SQL statement. I don’t normally do anything in the name that references the scope of the variable. But I always specify the data type in the name. Is this something that is normally considered Horrible and Evil in java?

Tom
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Type-based Hungarian Notation is relatively unusual (but that's not the only kind of Hungarian, nor really what Simonyi had in mind). I've been known to prepend an "s" in front of pre-converted strings, but only when working with systems that don't do the conversion behind-the-scenes, or when it's not appropriate for the manual conversion to throw an exception.

Some folks also use Hungarian to identify "safe" strings (i.e., HTML- or SQL-escaped, etc.) and develop a convention for that.

Other than those two situations it's rare I've needed to identify the type of a variable in the variable name itself, but YMMV.
 
Tom Piersa
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for you response. Ya know is seems like java programmers uses reasonable conventions like programmers from other languages. I wasn’t sure after my last thread.

Get a little smarter today than I was yesterday
Make it as readable as possible.
Don’t loose sleep, it’s a job not a way of life.

Thanks all again,

Tom
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!