标识符用法,标识符命名约定

标识符用法,标识符命名约定(1)

标示符命名是作为一个developer最基本的技能,也是很简单的东西,却也是能体现专业素质的地方,很多时候,我都在纠结如何取一个合适的变量名。在网上搜命名规范会出现一大堆,这类文章大同小异,但是正确的姿势其实是看官方约定,粗暴的说,就是rtfm(不知道什么意思的自行google)。

下面内容引自官网。

9 - Naming Conventions

Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.

Identifier Type

Rules for Naming

Examples

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 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).

class Raster;

class ImageSprite;

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" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

int i; char c; float myWidth;

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;

官网根据标识符类型分别描述了各自命名的约定,其实大家都知道的,我针对每种类型结合自身说下要点。

1.包名.

首个前缀必须是

(1) 全小写ascii字母。

(2)顶级域名之一。

就是com,edu这些。

接着的名字可以根据分区,部门,项目而定。

比如com.apple.quicktime.v2我猜测就是apple公司下的quicktime项目的包命名(纯属猜测)。

2.类名

(1)名词属性。(还有多少人记得这点)

(2)每个单词首字母大写

(3)简单和可描述性

(4)使用完整单词而非缩写,特殊情况比如这个单词的缩写词很常见除外

3.接口名

同类的命名约定。(自己补充一点,有接口肯定就有对应实现类,习惯对接口对应的实现类的命名规则是i:接口名字加上Impl后缀)

4.方法名

(1)动词属性(有多少人留意到这点)

(2)驼峰法

5.变量名

(1)驼峰法

(2)短且有意义

(3)临时变量使用单个字符(比如遍历循环的时候常用的就是i,知道为什么很多循环的demo是使用的是i作为变量了吧,因为官网推荐就是这么写的)

6.常量

(1)全部字母大写

(2)单词间使用下划线分割

写这篇文章的原因一个是再次巩固下这个基础,另一个方面想说的是official manual才是最权威的,网上文章千千万,你从官网上得到的信息才是最具说服力的。

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页