`
stingh711
  • 浏览: 122026 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

一份简单跟我风格比较一致的coding style

阅读更多

Introduction

The Java language gives you all the room you need to write code that would be very difficult for others to understand. Java also permits you to write code that is very easy to understand. Most development teams would prefer the latter.

A style guide provides provides a map so that the code generated by a group of programmers will be consistent and, therefore, easier to read and maintain. Many people do not care for the style guide offered by Sun. This document is one alternative.

This document covers most areas where there could be confusion or difference of opinion. Areas that have never been a problem in our experience are undocumented.

1 - Formatting

    1.1 - Indentation

    All indents are four spaces. All indenting is done with spaces, not tabs. (examples and reasoning)
    Matching braces always line up vertically in the same column as their construct. (examples)
    All if, while and for statements must use braces even if they control just one statement. (reasoning and examples)

    1.2 - Spacing

    All method names should be immediately followed by a left parenthesis.
    All array dereferences should be immediately followed by a left square bracket.
    Binary operators should have a space on either side.
    Unary operators should be immediately preceded or followed by their operand.
    Commas and semicolons are always followed by whitespace.
    All casts should be written with no spaces.
    The keywords if, while, for, switch, and catch must be followed by a space.
    (examples)

    1.3 - Class Member Ordering

    class Order
    {
    // fields

    // constructors

    // methods
    }

    1.4 - Maximum Line Length

    Avoid making lines longer than 120 characters. (reasoning)

    1.5 - Parentheses

    Parentheses should be used in expressions not only to specify order of precedence, but also to help simplify the expression. When in doubt, parenthesize.

2 - Identifiers

All identifiers use letters ('A' through 'Z' and 'a' through 'z') and numbers ('0' through '9') only. No underscores, dollar signs or non-ascii characters.

    2.1 - Classes and Interfaces

    All class and interface identifiers will use mixed case. The first letter of each word in the name will be uppercase, including the first letter of the name. All other letters will be in lowercase, except in the case of an acronym, which will be all upper case. (examples)

    2.2 - Packages

    Package names will use lower case characters only. Try to keep the length under eight (8) characters. Multi-word package names should be avoided. (examples)

    2.3 - All Other Identifiers

    All other identifiers, including (but not limited to) fields, local variables, methods and parameters, will use the following naming convention. This includes identifiers for constants.

    The first letter of each word in the name will be uppercase, except for the first letter of the name. All other letters will be in lowercase, except in the case of an embedded acronym, which will be all uppercase. Leading acronyms are all lower case. (examples and reasoning)

    Hungarian notation and scope identification are not allowed. (reasoning and examples)

    Test code is permitted to use underscores in identifiers for methods and fields. (reasoning and examples)

3 - Coding

    3.1 - Constructs to Avoid

    Never use do..while. (examples and reasoning)
    Never use return in the middle of a method. (reasoning)
    Never use continue. (reasoning)
    Never use break other than in a switch statement. (reasoning)

    3.2 - Do Not Compound Increment Or Decrement Operators

    Use a separate line for an increment or decrement. (examples and reasoning)

    Never use pre-increment or pre-decrement (examples and reasoning)

    3.3 - Initialization

    Declare variables as close as possible to where they are used. (examples)

    3.4 - Access

    All fields must be private, except for some constants.

4 - Self-Documenting Code

				"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand."
        --- Martin Fowler, Refactoring: Improving the Design of Existing Code

Rather than trying to document how you perform a complex algorithm, try to make the algorithm easier to read by introducing more identifiers. This helps in the future in case the algorithm changes but someone forgets to change the documentation. (examples and reasoning)


原文链接在 http://www.javaranch.com/style.jsp

分享到:
评论

相关推荐

    C# Coding Style Guide

    《C# Coding Style Guide》是一份详细的文档,旨在为 C# 开发者提供一套规范化的编码标准。这份文档由 Mike Krueger 在 2002 年 1 月发布,版本号为 0.2。它不仅适用于 SharpDevelop 这一集成开发环境下的项目,而且...

    Professonal Verilog Coding Style.pdf

    综上所述,Motorola发布的《Professional Verilog Coding Style》不仅是一份指导工程师如何编写高质量Verilog代码的手册,更是体现了半导体设计领域对标准化、规范化和法律合规性的重视。通过对这些关键知识点的深入...

    Verilog RTL Coding Style.pdf

    《Verilog RTL Coding Style.pdf》是一份由台湾清华大学提供的资料,该资料详细介绍了在编写Verilog RTL代码时应当遵循的最佳实践和编码规范。 #### 关键知识点 ##### 1. 基本设计概念 - **简单与规则**:采用简单...

    hdl_coding_style.rar_HDL coding style_coding style_hdl_hdl codin

    《HDL coding style》这份文档(参考文件hdl_coding.pdf)可能涵盖了以下几个关键知识点: 1. **命名规范**:良好的HDL代码应遵循一致且有意义的命名规则,如使用驼峰式或下划线分隔的命名方式,避免使用缩写和保留...

    xilinx coding style

    ### Xilinx Coding Style Xilinx官方发布的这份文档主要聚焦于如何优化FPGA设计,并通过合理的编程风格达到最佳的性能、资源利用以及功耗控制。以下将详细解析文档中的关键知识点。 #### 设计目标 设计FPGA的目标...

    Daniel's Objective-C Coding Style Guidelines

    Daniel’s Objective-C Coding Style Guidelines 是一套由杨德升根据 Apple 的 Cocoa 编码指南、Google 的 Objective-C 风格指南以及 Three20 的源代码风格指南总结而成的编码规范。这份文档主要关注于提高代码的...

    Google coding style

    ### Google C++ 编码风格指南 #### 概述 Google C++编码风格是一套详细的指导原则,旨在帮助开发者编写高质量、易于维护的C++代码。这套指南由Google内部的一组专家共同制定,并广泛应用于Google的软件开发实践中...

    Coding Style Guide

    该文档作为一份指导手册,旨在帮助开发者编写出健壮且可靠的程序。虽然重点在于C#语言的编程,但其中很多规则和原则同样适用于其他编程语言。这份指南涵盖了从文件组织到编程实践的多个方面。 #### 二、文件组织 #...

    google c++ coding style.png

    Google C++ Style Guide是一份不错的C++编码指南,我制作了一张比较全面的说明图,可以在短时间内快速掌握规范的重点内容。不过规范毕竟是人定的,记得活学活用。看图前别忘了阅读下面三条重要建议: 1 保持一致也...

    coding style

    这份文档不仅对初学者非常有用,对于任何希望遵循一致且高质量编码实践的开发者来说都是宝贵的资源。 #### 二、背景与意义 编程风格(或称为编码风格)是指编写源代码时所采用的约定和习惯。良好的编码风格有助于...

    Google C++ Style Guide.zip

    《Google C++ Style Guide》是Google公司制定的一套C++编程规范,旨在提供一套清晰、一致的代码风格,以提高代码可读性、可维护性和团队合作效率。这份指南覆盖了从变量命名到代码结构等多个方面的最佳实践,下面将...

    FreescaleVerilogCodingStyle-飞思卡尔Verilog代码风格

    ### Freescale Verilog Coding Style – 飞思卡尔Verilog代码风格 #### 概述 Freescale Verilog Coding Style文档提供了飞思卡尔半导体公司内部使用的Verilog HDL编程规范标准。这份文档遵循了Semiconductor Reuse ...

    Recommended C Style and Coding Standards中文翻译版第1/3页

    - 该标准虽非强制执行,但在某些机构中可能会被用来作为代码验收的一部分,因此保持风格一致是重要的。 - 遇到特殊情况,应咨询有经验的C程序员,或者参考他们的代码。 2. **文件组织** - 文件应以空行隔开各个...

    Megma综合工具代码风格的资料.rar

    《Megma综合工具代码风格指南》是一份针对Megma开发工具的编码规范和风格指导文档。这份资料集合了两个关键的资源:《coding_style_guidelines_for_nextreme-an01.pdf》和《coding style guidelines application ...

    谷歌开源项目的编程风格

    谷歌公司的开源项目以其高质量、可维护性和一致性而闻名,这些都离不开其严谨的编程风格指导。这份指南将深入探讨谷歌在C++、Objective-C、HTML和Java这四种语言中的编程规范,帮助开发者遵循最佳实践,提升代码质量...

    coding-style:安居客代码规范

    在安居客的`coding-style-master`压缩包中,很可能是包含了具体的代码风格指南、示例代码、检查工具配置等内容。开发者应当按照这些文档来调整自己的编码习惯,以便于团队的协作和项目的成功进行。通过遵循这套规范...

    linux源码编程代码风格推荐,帮忙自己养成记好的编码风格

    这份文档是基于 Linux 源码 `Documentation` 文件夹下的 `CodingStyle` 修改而来,主要修改了大括号的位置,其他内容沿用了原始的翻译。 首先,关于缩进,文档强调使用8个字符作为制表符,因为更宽的缩进有助于在长...

Global site tag (gtag.js) - Google Analytics