`

关于java命名的约定

 
阅读更多
先是匈牙利命名法,
http://www.land-of-kain.de/hnc4java/

Hungarian Naming Convention for Java

@for Java Developers
@author Kai Ruhl
@since 1997-07

Intro

A HNC, or hungarian naming convention (hungarian because reportedly the first inventor was hungarian programmer Charles Simonyie at Microsoft), is the methodology of prefixing variables with one or more letters to indicate their type. The Sun Coding Conventions do not use HNC.

The HNC variant presented in this article applies only to variables, not to methods; methods should make their type clear thru their names, e.g. isXXX for booleans, getXXXValue for numericals, etc. It is designed for ease of application and minimal visual clutter, which results in the usage of as few letters as possible.

The HNC for Java

LetterDescriptionExample
a<x> Array of <x> Object[] aoVals
b byte, short byte bVal
c char char cLetter
d double double dVal
e Enumeration, Iterator Enumeration eVals
f float float fVal
g GUI component JComboBox gChoice
h Hashtable, HashMap HashMap hData
i int int iVal
j - -
k - -
l long long lDate
m - -
n Numerical (Date, Dimension, BigInteger, ...) BigDecimal nVal
o Object (whenever nothing else applicates) MidiChannel oChannel
p - -
q - -
r - -
s String, StringBuffer String sLine
t boolean (truth value) boolean tRunning
u - -
v Vector, ArrayList ArrayList vData
w - -
x - -
y - -
z - -


然后是ibm上关于命名的一个文章:
http://www-128.ibm.com/developerworks/library/ws-tip-namingconv.html

Java naming conventions

Make your life easier

developerWorks
Document options
<noscript></noscript> <noscript>&lt;tr valign="top"&gt;&lt;td width="8"&gt;&lt;img alt="" height="1" width="8" src="//www.ibm.com/i/c.gif"/&gt;&lt;/td&gt;&lt;td width="16"&gt;&lt;img alt="" width="16" height="16" src="//www.ibm.com/i/c.gif"/&gt;&lt;/td&gt;&lt;td class="small" width="122"&gt;&lt;p&gt;&lt;span class="ast"&gt;Document options requiring JavaScript are not displayed&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;</noscript> <script type="text/javascript" language="JavaScript"> <!-- document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img alt="Set printer orientation to landscape mode" height="16" src="//www.ibm.com/i/v14/icons/printer.gif" width="16" vspace="3" /></td><td width="122"><p><b><a class="smallplainlink" href="javascript:print()">Print this page</a></b></p></td></tr>'); //--> </script> <script type="text/javascript" language="JavaScript"> <!-- document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="Email this page" /></td><td width="122"><p><a class="smallplainlink" href="javascript:void newWindow()"><b>E-mail this page</b></a></p></td></tr>'); //--> </script>
Set printer orientation to landscape mode

Print this page

Email this page

E-mail this page

<!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- 11/29/06 commented out by Jill--><!--<br /> <table border="0" cellpadding="0" cellspacing="0" width="150"> <tr> <td class="v14-header-2-small">Using XML, but need to do more?</td> </tr> </table> <table border="0" cellpadding="0" cellspacing="0" class="v14-gray-table-border"> <tr> <td width="150" class="no-padding"> <table border="0" cellpadding="0" cellspacing="0" width="143"> <tr valign="top"> <td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt="" /></td> <td><img src="//www.ibm.com/i/v14/icons/fw_bold.gif" height="16" width="16" border="0" vspace="3" alt="" /></td> <td width="125"> <p><a href="http://www.ibm.com/developerworks/kickstart/database.html?S_TACT=105AGX01&amp;S_CMP=SIMPLEART" class="smallplainlink">Download DB2 Express-C 9</a> </p> </td> </tr> </table> </td> </tr> </table> --><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
Rate this page

Help us improve this content


Level: Introductory

Scott Ambler (scott.ambler@ronin-intl.com), President, Ronin International

13 Jul 2000

In this first tip from Scott Ambler, he presents some guidelines for naming various Java elements, to make your job easier.
<!--START RESERVED FOR FUTURE USE INCLUDE FILES--> <script type="text/javascript" language="JavaScript"> <!-- if (document.referrer&&document.referrer!="") { // document.write(document.referrer); var q = document.referrer; var engine = q; var isG = engine.search(/google/.com/i); var searchTerms; //var searchTermsForDisplay; if (isG != -1) { var i = q.search(/q=/); var q2 = q.substring(i+2); var j = q2.search(/&/); j = (j == -1)?q2.length:j; searchTerms = q.substring(i+2,i+2+j); if (searchTerms.length != 0) { searchQuery(searchTerms); document.write("<div id=/"contents/"></div>"); } } } //--> </script> <!--END RESERVED FOR FUTURE USE INCLUDE FILES-->

Use full descriptors that accurately describe the variable, field, or class

For example, use names like firstName, grandTotal, or CorporateCustomer. Although names like x1, y1, or fn are easy to type because they're short, they do not provide any indication of what they represent and result in code that is difficult to understand, maintain, and enhance.



Back to top


Use terminology applicable to the domain

If your users refer to their clients as customers, then use the term Customer for the class, not Client. Many developers make the mistake of creating generic terms for concepts when perfectly good terms already exist in the industry or domain.



Back to top


Use mixed case to make names readable

You should use lowercase letters in general, but capitalize the first letter of class names and interface names, as well as the first letter of any non-initial word.



Back to top


Use abbreviations sparingly and intelligently

This means you should maintain a list of standard short forms (abbreviations), you should choose them wisely, and you should use them consistently. For example, if you want to use a short form for the word number, then choose one of nbr, no, or num, document the one you choose (it doesn't really matter which one), and use only that one.



Back to top


Avoid long names (15 characters max is a good idea)

Although the class name PhysicalOrVirtualProductOrService might seem to be a good class name at the time (OK, I'm stretching it on this example), this name is simply too long and you should consider renaming it to something shorter -- perhaps something like Offering.



Back to top


Avoid names that are too similar or that differ only in case

The variable names persistentObject and persistentObjects should not be used together, nor should anSqlDatabase and anSQLDatabase.



Back to top


Capitalize the first letter of standard acronyms

Names will often contain standard abbreviations, such as SQL for Standard Query Language. Names such as sqlDatabase for an attribute or SqlDatabase for a class are easier to read than sQLDatabase and SQLDatabase.



Back to top


Resources



Back to top


About the author

Scott W. Ambler is President of Ronin International, a consulting firm specializing in object-oriented software process mentoring, architectural modeling, and Enterprise JavaBeans (EJB) development. He has authored or co-authored several books about object-oriented development, including the recently released The Object Primer 2nd Edition, which covers, in detail, the subjects summarized in this article. He can be reached at scott.ambler@ronin-intl.com and at his Web site at www.ambysoft.com.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics