freelanceprogrammers.org Forum Index » Java

Naming conventions


View user's profile Post To page top
gblue42 Posted: Tue Feb 12, 2002 8:28 am


Joined: 10 Feb 2002

Posts: 15
Naming conventions
Does anyone have some links to thorough articles
about naming conventions? I`ve read the brief little
page on Sun`s site but it doesn`t really go into
naming member variables/objects, local
variables/objects, etc.
Reply with quote
Send private message
View user's profile Post To page top
mcconnell_stephen Posted: Tue Feb 12, 2002 8:34 pm


Joined: 01 Feb 2002

Posts: 73
Naming conventions
Although I did not write this book (I wish I
did), you might check out Steve McConnell`s Code
Complete.<br><br>While the coding conventions in that book are for C/C++
they are applicable to Java and the concepts are
applicable to all development.<br><br>Naming and Coding
Conventions are about as varried as there are programmers.
What I like is not what my bosses like, so I follow
their coding conventions. I like the Hungarian
nomenclature described in Code Complete, but not many people I
know follow it and it confuses people who
don`t.<br><br>A few guidelines, however:<br>1) Don`t abreviate...
Java will take names as long as you want, so make your
names as descriptive and uncryptic as possible.<br>2)
Class Names Always begin with a Capital letter and each
new word in the name begins with a capital. The name
should be descriptive of what the class is. If it is a
domain class (representing business objects) it should
be made of nouns.<br>ie.<br>PayrollAccount<br><br>If
it is a "helper" class, it should be
verbs.<br>ie<br>ReconcileCheckbookHelper<br><br>Utility classes should follow
the
same:<br><br>CollectionFactory<br><br>3) Your objects could actually be
descriptive...<br><br>PayrollAccount payrollAccount = new
PayrollAccount();<br><br>If
you are using a collection<br><br>Vector
payrollAccounts = new Vector();<br><br>Use plural....<br><br>4)
Different people use different techniques to differentiate
between local and instance varables.<br>First, your
methods should not be so large that you can`t see the
whole thing in one glance. When you define local
variables in your method, you can then see that you can
easally see that you have defined it within your method.
This will help if determining if you have a local or a
class (instance variable/object).<br><br>The
objects/variables always begin in a lower case.<br><br>You can go
on and on... but these are just a few conventions
for starters. Check out Code Complete; and that will
help you on your way further.<br><br>Good
Luck<br><br>Stephen McConnell<br><a href=http://www.crosslogic.com
target=new>http://www.crosslogic.com</a>
Reply with quote
Send private message
View user's profile Post To page top
epenak Posted: Tue Feb 12, 2002 9:00 pm


Joined: 31 Jan 2002

Posts: 39
Naming conventions
Stephen,<br>It is time to write a book! Just think of the confusion you could
cause. Just think of the money you could make! Fame, glory, sex, power. All of
this could be yours!<br><br>;-)
Reply with quote
Send private message
View user's profile Post To page top
gblue42 Posted: Tue Feb 12, 2002 9:51 pm


Joined: 10 Feb 2002

Posts: 15
Naming conventions
Thank you Stephen. I`ll check it out. The
guidelines you listed are ones I agree with and regularly
adhere to. The part I`m wrestling with has to do with a
tendency of some people to include something in the name
to indicate the type such as strFirstName, intAge,
blnActive, btnOK etc. I seems to me that this could work if
a program sticks to the basics but with object
oriented programming and the limitless supply of classes
that are out there, trying to come up with meaningful
abbreviations could prove frustrating. I suppose we could
include the complete name of the class in the name of the
object but sheesh!<br><br>What`s the advantages of
including the type in the name? So you don`t have to seek
out the declaration in the code to find out the type?
One convention that I have seen (and used) that seems
helpful is prefixes of "m_" or "l_" to indicate the scope
of member or local variables.<br><br>Thank you for
the book recommendation. I`ll check it
out.<br><br>Gerry
Reply with quote
Send private message
View user's profile Post To page top
mcconnell_stephen Posted: Tue Feb 12, 2002 9:55 pm


Joined: 01 Feb 2002

Posts: 73
Naming conventions
epenak<br><br>One out of four might be nice.... guess...<br><br>Steve
Reply with quote
Send private message
View user's profile Post To page top
mcconnell_stephen Posted: Tue Feb 12, 2002 10:04 pm


Joined: 01 Feb 2002

Posts: 73
Naming conventions
Including "type" in the definition of an object
is what is described in the Hungarian notation in
Code Complete... he has a large discussion on it and a
couple of tables....<br><br>I`ve used the underscore
infront of a member variable to indicate an instance
variable and nothing to describe the local... Sometimes
this is an "overformalization" but if you can get
everyone on the team (and these conventions are mainly
used for code in a team environment or if you drop
dead tomorrow, someone ELSE might have to maintain) to
follow the convention, more power to you... Young kids
coming out of college have never worked in a team
environment and have never had to maintain someone else`s
cryptic spagetti code; and just don`t understand the need
for standardization. They also don`t understand that
if they change code to get their section to work;
and yet break everyone else`s functionality that it`s
THEIR problem not everyone else`s.<br><br>I guess it`s
just my prior military need for standardization that
drives me...<br><br>Stephen
McConnell<br><a href=http://www.crosslogic.com
target=new>http://www.crosslogic.com</a>
Reply with quote
Send private message
View user's profile Post To page top
mattc_26 Posted: Wed Feb 13, 2002 10:34 pm


Joined: 13 Feb 2002

Posts: 3
Naming conventions
The prefixing of the variable type is part of the
hungarian convention. Adding the scope to a variable name
was something put forward by microsoft. You`ll see
these conventions in java code , for the most part, by
those moving from VC++, C++ or VB to Java. <br><br>I
peronally don`t find it useful. If I`m casually looking
through code I`m not going to have any more of a clue as
to what is actually going on by having types
prefixed to a varaible names. And when you look closer
it`s pretty evident what datatype your working with
without any additional nomenclature. <br><br>As for
adding a scoping prefix I`ve personally hav not done
that but I could see some benefit. The only reason I
haven`t is because I guess my general programming
practice. That being that when I`m writing, for example a
class, I`m either dealing with a stateful or a statless
object. If it`s stateless everything is pretty much
method local while if it`s stateful I`m dealing
primarily with memeber variables using a bare minimum of
local variables.<br><br>Other than the coding
conventions provided by sun there is a group of experienced
developers at Rogue Wave who put out conventions which are
generally accepted by the java community with open
arms:<br><br><a href=http://www.ambysoft.com/javaCodingStandards.html
target=new>http://www.ambysoft.com/javaCodingStandards.html</a><br><a
href=http://www.amazon.com/exec/obidos/ASIN/0521777682/qid=1013617224/sr=8-1/ref
=sr_8_71_1/103-9033500-0132657
target=new>http://www.amazon.com/exec/obidos/ASIN/0521777682/qid=1013617224/sr=8
-1/ref=sr_8
_71_1/103-9033500-0132657</a><br><br>On a previous project a few years back the
architect
of the project purchased a copy of that book and
handed it to everyone on the team. I`ve been using it
ever since, and you`ll find a lot of people who have
been using the language for a while will be as well.
Reply with quote
Send private message
View user's profile Post To page top
mcconnell_stephen Posted: Wed Feb 13, 2002 11:18 pm


Joined: 01 Feb 2002

Posts: 73
Naming conventions
Cool. Downloaded the ambysoft pdf and it looks
great. I`ve seen the Java Style book, but didn`t
purchase it. Seems like a good investment.<br><br>You`r
right. If you are doing good general OOAD practices,
prefacing your variables with scope and type is not that
necessary. Your methods should be small enough to obviate
that. <br><br>If the code review practice is good
enough (used as an education process as well as QC),
good programming style should begin to permiate the
team.<br><br>However, most team environments view that practice as
overhead and not really important (which I think is a
mistake). Not all programmers on a team are at the same
level, or develop the same way (this is a good thing,
though). <br><br>Thanks for the links.<br><br><br>Stephen
McConnell<br><a href=http://www.crosslogic.com
target=new>http://www.crosslogic.com</a>
Reply with quote
Send private message
View user's profile Post To page top
gblue42 Posted: Thu Feb 14, 2002 12:03 am


Joined: 10 Feb 2002

Posts: 15
Naming conventions
Exactly the kind of white paper I was hoping for. Thank you!
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help