• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java naming conventions

 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid abbreviations. Vowels do not cost $250 each.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ninad Kuchekar wrote: . . . Firstly, use the Hungarian notations, which helps in readability of the code. . . .
Disagree.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What that article calls Apps Hungarian is better than what it calls System Hungarian. For a different opinion, look here.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic