How to Naming in Java (翻译)

What Is a Naming Convention?
什么事命名约定?

A naming convention is a rule to follow as you decide what to name your identifiers (e.g. class, package, variable, method, etc..).

命名约定是你准备为你的标识符命名的约定准则(例如,类名,包名,变量名和方法名等)

Why Use Naming Conventions?

为什么要采用命名约定?

Different Java programmers can have different styles and approaches to the way they program. By using standard Java naming conventions they make their code easier to read for themselves and for other programmers. Readability of Java code is important because it means less time is spent trying to figure out what the code does, leaving more time to fix or modify it.

不同的Java程序员可能有不同的代码风格,但通过使用代码约定,可以是使Java代码更容易阅读,无论是对作者自己还是对其他程序员。可读性好的程序代码是十分重要的,这意味着我们可以花很少的时间去明白这些代码的作用,留下更多的时间来修改和完善这些代码。

To illustrate the point it's worth mentioning that most software companies will have a document that outlines the naming conventions they want their programmers to follow. A new programmer who becomes familiar with those rules will be able to understand code written by a programmer who might have left the company many years before hand.

大多数软件公司都有一个要求公司员工遵循的命名约定的文档,新来的程序员一旦熟悉了这些命名约定,就会比较容易的看懂前辈们写的代码,尽管这些代码的原创作者可能已经离开这家公司好多年了。

Picking a Name for Your Identifier

为你的标识符命名

When choosing a name for an identifier make sure it's meaningful. For instance, if your program deals with customer accounts then choose names that make sense to dealing with customers and their accounts (e.g., customerName, accountDetails). Don't worry about the length of the name. A longer name that sums up the identifier perfectly is preferable to a shorter name that might be quick to type but ambiguous.

为标识符命名的时候要确保名字是有意义的,例如,如果你的程序是和客户账户打交道的,我们就应该命名时体现出来,比如 customerName , accountDetails 这样的标识符。不要当心标识符的长度,长标识符能够虽然敲打慢,但是它们比那些敲打快但意义模糊的短标识符有更明确的意义。

A Few Words About Cases

关于字母大小写的约定

Using the right letter case is the key to following a naming convention:

正确的使用大小写是遵循命名约定的关键

  • Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage).

  • 小写字体是指标识符中没有任何大写字母(例如, while, if, mypackage).

  • Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS, FIRST_DAY_OF_WEEK).

  • 大写字体是指标识符中全是大写字母,当一个标识符中不止一个单词时候,我们应该用下划线分离这些单词(例如, MAX_HOURS, FIRST_DAY_OF_WEEK).

  • CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).

  • 驼峰式命名(也叫做大驼峰式)是指每个新的单词以大写字母开头,而其他地方小写(如:CamelCass, CustomAccount, PlayingCard).

  • Mixed case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).

  • 混合式命名(也叫做小驼峰式)和驼峰式一样,只是第一个字母得小写(如:hasChildren, customerFirstName, customerLastName).

Standard Java Naming Conventions

标准Java命名约定

The below list outlines the standard Java naming conventions for each identifier type:

下面罗列了不同标识符的标准Java命名约定

  • Packages: Names should be in lowercase. With small projects that only have a few packages it's okay to just give them simple (but meaningful!) names:
     package pokeranalyzer
     package mycalculator 
    In software companies and large projects where the packages might be imported into other classes, the names will normally be subdivided. Typically this will start with the company domain before being split into layers or features:
     package com.mycompany.utilities
     package org.bobscompany.application.userinterface 
  • 包名:包名应该是小写字体,一些只有少量包的小项目的包名可以是简单的(但是得有意义),例如:
     package pokeranalyzer
     package mycalculator 
  • 在一些软件公司的一些大项目里面,一个包通常要导入到其他类文件里面去,这些包就必须得细分了。典型的是以公司的域名为包名开头,然后再往下细分,例如:
     package com.mycompany.utilities
     package org.bobscompany.application.userinterface 
  • Classes: Names should be in CamelCase. Try to use nouns because a class is normally representing something in the real world:
     class Customer
     class Account 
  • 类名:类名应该使用驼峰式命名,尽量的用名词表示类名,因为一个类往往表示现时生活里面的一类事物:
     class Customer
     class Account 
  • Interfaces: Names should be in CamelCase. They tend to have a name that describes an operation that a class can do:
     interface Comparable
     interface Enumerable 
    Note that some programmers like to distinguish interfaces by beginning the name with an "I":
     interface IComparable
     interface IEnumerable 
  • 接口名: 接口名称也应该使用驼峰式命名,尽可能的描述出实现这个接口的类具有的功能:
     interface Comparable
     interface Enumerable 
    有些程序员喜欢在接口名称前面加上"I"以和类名区分:
     interface IComparable
     interface IEnumerable 
  • Methods: Names should be in mixed case. Use verbs to describe what the method does:
     void calculateTax()
     string getSurname() 
  • 方法名:方法名应该使用混合式命名,用动词来命名方法以描述出这个方法的作用:
     void calculateTax()
     string getSurname() 
  • Variables: Names should be in mixed case. The names should represent what the value of the variable represents:
     string firstName
     int orderNumber 
    Only use very short names when the variables are short lived, such as in for loops:
     for (int i=0; i<20;i++)
     {    //i only lives in here
     } 
  • 变量名: 变量名应该使用混合式命名,变量名应该能够反映出这个变量的意义:
     string firstName
     int orderNumber 
    只有当变量的作用域很小的时候才使用很简短的变量名,比如说在循环语句中:
     for (int i=0; i<20;i++)
     {    //i only lives in here
     } 
  • Constants: Names should be in uppercase.
     static final int DEFAULT_WIDTH
     static final int MAX_HEIGHT 
  • 常量名: 常量名应该使用大写字体:
     static final int DEFAULT_WIDTH
     static final int MAX_HEIGHT 

---EOF---


Visiting original tutorial website by click here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值