File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java naming conventions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java naming conventions" Watch "Java naming conventions" New topic
Author

Java naming conventions

Skanda Raman
Ranch Hand

Joined: Mar 21, 2008
Posts: 205

This would be more basic question for many experts here. Please advise.

However, please let me know what considerations to be taken while choosing name to class and variables.

In some situations selecting the names for the class and variables/method becomes very difficult for me.

For instance, recently i had a requirement to write java class which accepts a number and make sure that the entered number is always less the end limit number. If condition is succeeds display success message else failure

Here I selected class name as CheckNumberStatus, input variable name as inputVal, end limit value as endLimitVal and method as DisplayStatus()

Let me if i am right or is there any better practices in choosing name for classes, variables and methods
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
No.

I would be happy with the two variable names. The method name is appropriate, but ought to start with a small letter (d not D).

The name of the class should be a noun phrase; maybe NumberStatusChecker would be better. But it might be better to have a Utilities class with a private constructor only, and lots of static methods like that.

It would be more appropriate, I think, to throw an Exception than display an error message.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I'm not really happy with the two variable names. Why would you abbreviate value to val? It's only 2 more characters and it only complicates the name.
Like Campbell said make the class name a noun. Make methods names verb's but don't make then vague such as dealWithSomething(). A good method name describes that the method does.

The requirement describes a validator thus I would name it something like RangeValidator. Instead of throwing an Exception I would just return a boolean.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Avoid abbreviations. Vowels do not cost $250 each.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Bear Bibeault wrote:Avoid abbreviations. Vowels do not cost $250 each.

I've seen code in which, for a button handler, the coder shortened the first word by two letters (but left 'Handler' in full). Go figure!


luck, db
There are no new questions, but there may be new answers.
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Hi Eshwar,

There are certain things which you should keep in mind while choosing a class name, variable name or a method name.

Firstly, use the Hungarian notations, which helps in readability of the code.

Secondly, when choosing a name do not choose a name which you find suits best, always think what if someone else was reading your code, would he/she understand it?
Your objects have State,Behavior, Identity & Responsibility ; This should be seen in your names.

There is actually a very good article in "Effective Java" by Joshua Bloch (I guess in the first chapter). He gives some interesting tips for writing methods and their names, might want to look into it.


-Ninad


Don't walk as if you rule the world, walk as if you don't care who rules it...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Ninad Kuchekar wrote: . . . Firstly, use the Hungarian notations, which helps in readability of the code. . . .
Disagree.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

Ninad Kuchekar wrote:Firstly, use the Hungarian notations, which helps in readability of the code.

I disagree as well on using Hungarian notation. I do not think it helps the readability of the code; on the contrary. You're adding extra letters to variable names which just make the words harder to read. And modern IDEs help you so much with hints and auto-completion that it is really not necessary to use extra letters to indicate the types of variables. And furthermore, using Hungarian notation is not common practice in Java.

It's best to use a style and conventions that match the de-facto standard for Java, which you can find here: Code Conventions for the Java Programming Language (it's an old document, but it's still relevant).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
What that article calls Apps Hungarian is better than what it calls System Hungarian. For a different opinion, look here.
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Jesper Young wrote:
I disagree as well on using Hungarian notation. I do not think it helps the readability of the code; on the contrary. You're adding extra letters to variable names which just make the words harder to read. And modern IDEs help you so much with hints and auto-completion that it is really not necessary to use extra letters to indicate the types of variables. And furthermore, using Hungarian notation is not common practice in Java.

It's best to use a style and conventions that match the de-facto standard for Java, which you can find here: Code Conventions for the Java Programming Language (it's an old document, but it's still relevant).


Actually that makes sense too (after I read through the link). I must have found it easier, maybe because I am used to writing code that way. Though, no one has ever found it difficult to read since the amount of documentation in my code is triple my lines of code
But thanks for the article Jesper!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java naming conventions