




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
文档编号 文档编号 sm cmmi 1104 03 1 0sm cmmi 1104 03 1 0 Java 编码规范编码规范 编编 写写 人 缪立群人 缪立群 编写日期 编写日期 2011 3 192011 3 19 部部 门 门 审审 核核 人 人 审核日期 审核日期 修订页 版本版本日期日期内容提要内容提要作者作者 1 02011 03 19完成首稿 缪立群 Java 编码规范 杭州信雅达数码科技有限公司 1 目录 1序言序言 1 2开发过程开发过程 1 3减少程序复杂性减少程序复杂性 1 4命名规则命名规则 2 5程序包程序包 类类 接口接口 4 6域域 6 7方法方法 6 8申明与语句申明与语句 6 9异常异常 6 10类型类型 7 11参考文献参考文献 7 Java 编码规范 杭州信雅达数码科技有限公司 1 1 序言序言 鉴于公司内部不同项目采用不同的 Java Web 框架及五花八门的工具 从公司管理层到 开发人员都意识到没有统一开发环境和规范 目前 Web 开发不利于公司生产率的提高 也 不利于公司的产品质量管理 2011 年初启动的统一 Java Web 开发项目 总体目标就是要 把我们公司内部 Java Web 开发统一到有秩序有规范的高效平台上 通过对现有开发人员的 集中短期培训 采用统一设计与编码规范以实现整体开发效率的提高 编写 Java 编码规范有两个目的 一是帮助 Java 开发人员减少运行错误 二是改善 Java 代码的可读性与可维护性 该规范主要依据于著名的美国加州理工大学 CalTech 一个研究 所最近的研究 希望更深入了解规范的人员可进一步阅读 CalTech 的资料 参考文献 1 2 开发过程开发过程 S01 每一个 Java 工程均须采用 Maven2 的项目结构 S02 每一个 Java 工程均必须包含单元测试 每个后台服务必须撰写对应的 UnitilsTestNG 单元测试用例 提交模块时必须保证所有 的单元测试用例均通过测试 PASS S03 每一个公开类或方法均须有 JavaDoc 说明 3 减少程序复杂性减少程序复杂性 S04 代码应减分成由简单的 API 组成的小块 一般推荐 一个类不含 10 个域以上 一个类不含 20 个方法以上 Java 编码规范 杭州信雅达数码科技有限公司 2 一个方法应不多于 75 行代码 一个方法应不多于 7 个参数 一个表达式应不多于 5 个运算符 4 命名规则命名规则 S05 应使用 Sun 推荐的标准命名规范 Identifier Type Rules for NamingExamples Packages The prefix of a unique package name is always written in all lowercase ASCII letters and should be one of the top level domain names currently com edu gov mil net org or one of the English two letter codes identifying countries as specified in ISO Standard 3166 1981 Subsequent components of the package name vary according to an organization s own internal naming conventions Such conventions might specify that certain directory name components be division department project machine or login names com sun eng com apple quicktime v2 edu cmu cs bovik cheese Classes Class names should be nouns in mixed case with the first letter of each class Raster class ImageSprite Java 编码规范 杭州信雅达数码科技有限公司 3 internal word capitalized Try to keep your class names simple and descriptive Use whole words avoid acronyms and abbreviations unless the abbreviation is much more widely used than the long form such as URL or HTML Interfaces Interface names should be capitalized like class names interface RasterDelegate interface Storing Methods Methods should be verbs in mixed case with the first letter lowercase with the first letter of each internal word capitalized run runFast getBackground Variables Except for variables all instance class and class constants are in mixed case with a lowercase first letter Internal words start with capital letters Variable names should not start with underscore or dollar sign characters even though both are allowed Variable names should be short yet meaningful The choice of a variable name should be mnemonic that is designed to indicate to the casual observer the intent of its use One character variable names should be avoided except for temporary throwaway int i char c float myWidth Java 编码规范 杭州信雅达数码科技有限公司 4 variables Common names for temporary variables are i j k m and n for integers c d and e for characters Constants The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ANSI constants should be avoided for ease of debugging static final int MIN WIDTH 4 static final int MAX WIDTH 999 static final int GET THE CPU 1 5 程序包程序包 类类 接口接口 S06 必须使用显式 import import java util this would not be ok S07 不可以有程序包或类的循环依赖关系 S08 必须遵守类的 equals 合同 reflexive x equals x for all non null objects x symmetric x equals y implies y equals x for all non null objects x and y transitive x equals y and y equals z implies x equals z for all non null objects x y and z null sensitive x equals null false for all non null objects x consistent x equals y should stay constant if no changes are made to x and y for all non null Java 编码规范 杭州信雅达数码科技有限公司 5 objects x and y S09 方法 equals 与 hashCode 应同时定义 S10 方法 equals 的参数类型应为 Object class Rover private String name public String getName return name public Rover String name this name name not ok XXX public boolean equals Rover r type should be Object instead return name equals r getName ok Override public boolean equals Object o if o instanceof Rover return false else Rover rover Rover o return name equals rover getName S11 不要调用 finalize 方法 垃圾回收执行有其不可预测性 Java 编码规范 杭州信雅达数码科技有限公司 6 S12 在继承不是最自然的场合优先使用合成 Composition 而不是继承 Inheritance 6 域域 S13 域应申明为 private S14 应申明不变的域为 final S15 域在使用前应初始化 7 方法方法 S16 使用注解 override 明确该方法重写父类中的方法 S17 不要返回无效的 NULL 数组或集合 S18 不要调用 System exit 8 申明与语句申明与语句 S19 一行只写一条语句或申明一个变量 S20 在控制结构 if else 里应用大括号标志执行块 表达式 S21 不要用 或 比较对象 包括 String Integer Long 等 应该用 equals 方法 S22 浮点类型 float double 表达式不可用 或 来测试条件 S23 在循环语句中 避免用 String 来链接 而应使用更高效的 StringBuffer Java 编码规范 杭州信雅达数码科技有限公司 7 9 异常异常 S24 异常 Exceptions 不可以随意丢掉或忽略 受阻的 checked 异常应当被处理 不受阻的 unchecked 异常通常可以扔到最外层 S25 一个 finally 代码块不可以包含 return 语句 10 类型类型 S26 当申明一个变量或参数时 应使用接口 interface 类 而不是实现类 HashSet elements1 new HashSet not ok Set elements2 new HashSet ok S27 能选择时应
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医院文艺汇演主持词(35篇)
- 2025年中海油县域加油站经理面试技巧与常见问题解析
- 2025-2030中国EAI市场需求态势及投资战略研究报告
- 2025年甘肃省天水市辅警人员招聘考试题库及答案
- 2025年甘肃省定西市辅警招聘考试题库及答案
- 江苏省扬州市梅岭中学教育集团2023-2024学年七年级上学期语文10月素养体验试卷(含答案)
- 员工培训计划与效果评估表人力资源开发工具
- 2025年度鹤岗市继续教育公需科目考试题(含答案)
- 国网安全管理平台培训课件
- 宝钢的设备管理体系
- 标志设计(全套课件88P)
- 数字货币投资入门指南
- 人教版(2024)七年级上册数学第一次月考测试卷(含答案)
- DL∕T 1804-2018 水轮发电机组振动摆度装置技术条件
- 新版学校班主任工作手册模板
- HG-T 5367.5-2022 轨道交通车辆用涂料 第5部分:防结冰涂料
- 国家公祭日成品课件
- 原油加工承揽合同
- QCT268-2023汽车冷冲压加工零件未注公差尺寸的极限偏差
- 八年级下册英语补全对话及答案
- 大便失禁课件
评论
0/150
提交评论