Java编码规则

  • 格式:pdf
  • 大小:354.43 KB
  • 文档页数:76

www.AmbySoft.com/javaCodingStandards.pdfCopyright 1998-1999 AmbySoft Inc.Writing Robust Java CodeThe AmbySoft Inc. Coding Standards for Javav17.01dScott W. AmblerSoftware Process MentorThis Version: January 15, 2000www.AmbySoft.com/javaCodingStandards.pdfCopyright 1998-1999 AmbySoft Inc.This page left unintentionally blank.(yuk yuk yuk)www.AmbySoft.com/javaCodingStandards.pdfCopyright 1998-1999 AmbySoft Inc.Purpose of this White PaperThis white paper 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 increase 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 consistency,making teams of developers significantly more productive.Important Features of This White Paper• Existing standards from the industry are used wherever possible – You can reuse more than just code.• The 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 tradeoffs that have been made.• The standards presented in this white paper are based on real-world experience from numerous object-oriented development projects. This stuff works in practice, not just theory.• These standards are based on proven software-engineering principles that lead to improveddevelopment productivity, greater maintainability, and greater enhancability. Target Audience Professional software developers who are interested in:• Writing Java code that is easy to maintain and to enhance• Increasing their productivity• Working as productive members of a Java development teamHelp Me Improve These StandardsBecause I welcome your input and feedback, please feel free to email me at scott@ambysoft.com with yourcomments and suggestions. Let’s work together and learn from one another.AcknowledgmentsThe following people have provided valuable input into the development and improvement of thesestandards, and I would like to recognize them for it.Stephan MarceauLyle ThompsonDavid PinnLarry VirdenEva GreffWayne ConradMichael AppelmansWilliam GilbertGraham WrightAlex SantosKiran AddepalliBrian SmithLarry AllenDick SalisburyBruce ConradMichael FinneyJohn PintoVijay EluriCarl ZimmermanHakan SoderstromBill SiggelkowCamille BellFredrik NystromCory RadcliffKathy EckmanGuy SharfScott HarperKyle LarsonRobert MarshallPeter C.M. HaightMark BrouwerGerard BroeksteegHelen GilmoreScott W. AmblerJanuary 2000www.AmbySoft.com/javaCodingStandards.pdfCopyright 1998-1999 AmbySoft Inc.This page also left unintentionally blank.(although now it isn’t quite as funny)www.AmbySoft.com/javaCodingStandards.pdfCopyright 1998-2000 AmbySoft Inc.iTable of Contents1.GENERAL CONCEPTS.........................................................................................................................................11.1WHY CODING STANDARDS ARE IMPORTANT...........................................................................................11.2THE PRIME DIRECTIVE..................................................................................................................................11.3WHAT MAKES UP A GOOD NAME..............................................................................................................21.4GOOD DOCUMENTATION..............................................................................................................................31.4.1The Three Types of Java Comments.......................................................................................................41.4.2A Quick Overview of javadoc..................................................................................................................51.5AMBLER’S LAW OF STANDARDS.................................................................................................................62.STANDARDS FOR MEMBER FUNCTIONS....................................................................................................72.1NAMING MEMBER FUNCTIONS....................................................................................................................72.1.1Naming Accessor Member Functions....................................................................................................72.1.1.1Getters.................................................................................................................................................72.1.1.1.1Alternative Naming Convention for Getters – Has and Can..................................................82.1.1.2Setters.................................................................................................................................................82.1.1.3Constructors......................................................................................................................................82.2MEMBER FUNCTION VISIBILITY.................................................................................................................92.3DOCUMENTING MEMBER FUNCTIONS.......................................................................................................92.3.1The Member Function Header................................................................................................................92.3.2Internal Documentation.........................................................................................................................112.4TECHNIQUES FOR WRITING CLEAN CODE...............................................................................................122.4.1Document Your Code..............................................................................................................................122.4.2Paragraph/Indent Your Code...............................................................................................................132.4.3Paragraph and Punctuate Multi-Line Statements............................................................................132.4.4Use Whitespace in Your Code...............................................................................................................142.4.5Follow The Thirty-Second Rule............................................................................................................142.4.6Write Short, Single Command Lines....................................................................................................142.4.7Specify the Order of Operations...........................................................................................................142.5JAVA CODING TIPS.......................................................................................................................................152.5.1Organize Your Code Sensibly...............................................................................................................152.5.2Place Constants on the Left Side of Comparisons............................................................................153.STANDARDS FOR FIELDS (ATTRIBUTES/PROPERTIES)......................................................................163.1NAMING FIELDS............................................................................................................................................163.1.1Use a Full English Descriptor for Field Names.................................................................................163.1.1.1Alternative – Hungarian Notation................................................................................................163.1.1.2Alternative – Leading or Trailing Underscores..........................................................................173.1.2Naming Components (Widgets)............................................................................................................173.1.2.1Alternative for Naming Components – Hungarian Notation....................................................173.1.2.2Alternative for Naming Components – Postfix-Hungarian Notation.......................................173.1.3Naming Constants...................................................................................................................................183.1.4Naming Collections................................................................................................................................193.1.4.1Alternative for Naming Collections – The ‘Some’ Approach...................................................193.1.5Do Not “Hide” Names............................................................................................................................193.2FIELD VISIBILITY.........................................................................................................................................203.3DOCUMENTING A FIELD.............................................................................................................................213.4THE USE OF ACCESSOR MEMBER FUNCTIONS........................................................................................213.4.1Naming Accessors...................................................................................................................................223.4.2Advanced Techniques for Accessors....................................................................................................233.4.2.1Lazy Initialization.............................................................................................................................23