Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable Naming Conventions

 
Denis Zjukow
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I got a question to authors of "SCJD Exam with J2SE 5". There is a note at the bottom of page 21 which says: "The Sun Coding Conventions specify that you should apply the same naming convention to all instance, class, and local variables. Be aware that you may see code written by other coders where instance or class variables are signified by an underscore or some other special mark. Another way to achieve the same effect is by using the convention variable when referring to a local variable, this.variable when reffering to an instance variable, and Class.variable when referring to a class variable. Doing so makes it explicit which type of variable you are referring to."
Could you please provide a sample code clarifying the part in bold. And one other question about variable naming conventions: can I use some kind of prefixes for naming variables say 'a', 'an' for arguments 'the' for local variables and something like 'i' for fields? Thank you.
 
Jar Jaquiso
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you are:


[ May 12, 2006: Message edited by: Jar Jaquiso ]
[ May 12, 2006: Message edited by: Jar Jaquiso ]
 
Denis Zjukow
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jar Jaquiso:
There you are:


[ May 12, 2006: Message edited by: Jar Jaquiso ]

[ May 12, 2006: Message edited by: Jar Jaquiso ]


Ok, thanks. I was just confused by 'Class.variable'. It looks like 'variable' is a static field of Class class .
But what's about the second question?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using prefixes, postfixes, and such is extremely poor coding style.
Definitely NOT do that, if I were an assessor (I'm not) I'd detract serious points for just that.
I've written styleguides in the past, and they've always said to not do it.
So does the Sun styleguide which is what you have to follow.
So do most others, except the ones promoted by Microsoft for Visual Studio (which still use Hungarian notation which your idea is a weaker version of).

I now have to use it at work, but not for much longer (as I'm switching jobs) and it's a disaster. Completely unnecessary, complicates typing and makes changing the type of something next to impossible as it would mean massive code changes just to get the variable names to match their types again.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11907
207
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen T Wenting:
Using prefixes, postfixes, and such is extremely poor coding style.
I agree, and I am sorry that the note makes it appear that we are recommending this. We certainly do not recommend it, and you will see that we do not use prefixes in the book or in our sample code.

Regards, Andrew
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was referring to the last sentence by the OP, not the quote from your book (though maybe the OP misunderstood that note to mean using prefixes).
 
Denis Zjukow
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all. I actually didn't know that using prefixes/postfixes was such a bad thing. Saying the truth we used prefixes at work and I found it usefull . But now I know this is a bad practice and I will certainly NOT use it in my SCJD project!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic