作者:empty 出版社:empty |
Purpose of this WhitePaperThis whitepaper describes a collection of standards, conventions, and guidelines for writing solid Javacode, They are based on sound.proven software engineering principles that lead to code that is easy tounderstand, to maintain, and to enhance.Furthermore, by following these coding standards yourproductivity as a Java developer should in er ease remarkably-Experience shows that by taking the time towrite high-quality code right from the start you will have a much easier time modifying it during thedevelopment process, Finally, following a common set of coding standards leads to greater consiste ney,making teams of developers sign it icantly more productive.Important Features of This WhitePaperExisting standards from the industry are used wherever possible-You can reuse more than just codeThe reasoning behind each standard is explained so that you understand why you should follow it.Viable alternatives, where available, are also presented along with their advantages and disadvantagesso that you understand the trade of Ts that have been made.The standards presented in this whitepaper are based on real-world experience from numerous object-oriented development projects, This stuff works in practice, not just theoryThese standards are based on proven software-engineering prine ip les that lead to improveddevelopment product i vily, greater maintainability, and greater enhanc ability
Target AudienceProfessional software developers who are interested in;Because I welcome your input and feedback,pleasefeelfreetoemailmeatscotaambyaofL.comwith yourcomments and suggestions.Let's work together and leam from one another.The following people have provided valuable input into the development and improvement of theseWr if ing Java code that is easy to maintain and to enhanceIn er easing their productivityWorking as productive members of a Java development teamHelp Me Improve These Standards
1.General ConceptsCopyright 1998-2000AmbySoftIne.Copyright 1998-2000AmbySoftIne.Pd like to start this whie paper wt had i seuss ion of some general concepts that l feel are important forcoding standards.I begin with the importance of coding standards, propose the“Prime Directive forstandards, and then follow with the factors that lead to good names and good documentation.This sectionwill set the stage for the rest of this whitepaper, which covers standards and guidelines for Java coding.1.1Why Coding Standards are ImportantCoding standards for Java are important because they lead to greater consiste ney within your code and thecode of your teammates.Greater consiste ney leads to code that is easier to understand, which in tummeans it is easier to develop and to maintain.This re due es the overall cost of the applications that youcreate.
You have to remember that your Java code will exist for along time, long after you have moved on to otherprojects.An important goal during development is to ensure that you can transition your work to anotherdeveloper, or to another team of developers, so that they can continue to maintain and enhance your workwithout having to invest an unreasonable effort to understand your code.Code that is difficult tounderstand runs the risk of being se rapped and rewritten-I wouldn't be proud of the fact that my e odeneeded to be rewritten, would you?If everyone is doing their own thing then it makes it very difficult toshare code between developers, raising the cost of development and maintenance.In experie need developers, and eow boys who do not know any better, will often fight having to followstandards.They claim they ean code faster if they do it their own way.Pure hogwash.They MIGHT beable to get e ode out the door faster, but I doubt it.Co who y programmers get hung up during testing whenseveral difficult-to-findbugs ero pup, and when their code needs to be enhanced it often leads to a majorrewrite by them because they're the only ones who understand their code.Is this the way that you want tooperate?I certainly do not.
The Prime DirectiveNo standard is perfect and no standard is applicable to all situations:sometimes you find yourself in asituation where one or more standards do not apply.This leads me to introduce what I consider to be theprime dire etive of standards:The bottomline is that you need to understand each standard, understand when to apply them, and just asimportantly when not to apply them,When you go against a standard, document il.All standards, except for this one, ean be broken.Ifyou do so, you must document why you broke the standard, the potential implications of breakingthe standard, and any conditions that may/must oc eur he for e the standard can be applied to thissituation,
MEMBER FUNCTIN VISIBILITY
DCUMENTING MEMBER FUNCTINS.
2.3.1
2.3.2 Internal Dcumentatin
3.11UseaFullEnglshDeseript r fr FieldNames
3.1.2 Naming Cmpnents(Widgets)
3.1.3 Naming Cnstants.
3.1.4 Naming Cllectins
4.1.3 NamingExceptin h jects.
4.1.4BadIdeus fr Naming Lcal Variables.
4.21 General Cmments Abut Declaratin.
5.1.1 Alternative-Prefix Pura meter Names with a'r an:
6.1.1ClassVisihility
6.1.2 Naming Classes
6.1.3 Dcumenting a Class.
6.1.4ClassDeclu ratins
6.1.5 Minimize the Public and Prtected Interface
6.2.2 Dcumenting Interfaces
6.3.2 Dcumenting a Package
6.4.1Naminga Cmpilatin Unit.
6.42 Dcumenting a Cmpilatin Unit
7.2.1 Alternative-Explicitly Specify Each Imprted Class
Altern tv e Naming Cnventin fr Geers-Has and.
Setters.
Cnstrue t rsa
The Member Fu n ctin Header
TECHNI U ES FR WRITING CLEAN CDE
2.4.1Dcument Yur Cdemn im in in
2.4.2Paragraph/indent Yur Cde
2.4.3Paragraph and Punctuate Mul n-Line State men.
2.4.4Use Whites puce in Yur Cde.
2.4.5Fllw The Thirty-Secnd Rule.
2.4.6Write Shrt, Single Cmmand Lines
247Specify the rder f peratins.
25JAVA CDING TIPS.
2.5.1r ganze Yur Cde Sensibly
2.5.2 Place Cnstants n the Left Side f Cmparisns
STANDARDS FR FIELDS(ATTRIBUTES/PRPERTIES.
3.1NAMING FIELDS.
Alternative-Leading r Trailing Underscres
Alternative fr Naming Cmpnents-Hungarian Ntatin.
Alternative fr Naming Cmpnents-Pstfix-Hungarian Ntatin.
Apply The“final*Keywrd Sensibly.
rdering Member Functins and Fields.
3.14.1 Alternative fr Naming Cllectins-The'Sme'Appr a.
3.1.5D Nt“Hide Names.
FIELD VISIBILITY.
DCUMENTING A FIELD
34THE USE F ACCESS R MEMBER FUNCTINS.
3.4.1Naming Accessrs
3.4.2Advanced Techniques fr Accer srs.
342.1Lazy Initializatin.
342.2Getters fr Cnstants.
342.3Accessrs fr Cllectins.
342.4Ae cessing Several Fic lds Simultaneusly.
3.4.3 Visibility f Accessrs
3.4.4WhyUse Accessrs?
3.4.5Why Shuld n 7 Yu Use Accessrs 7
3.5ALWAYS INITIALIZE STATIC FIELDS.
STANDARDS FR LCAL VARIABLES.
4.1NAMING LCAL VARIABLES.
4.1.1NamingSrreums.
4.1.2Naming Lp