博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using Java Naming Conventions
阅读量:7056 次
发布时间:2019-06-28

本文共 4136 字,大约阅读时间需要 13 分钟。

hot3.png

http://java.about.com/od/javasyntax/a/nameconventions.htm

Using Java Naming Conventions

By , About.com Guide

Ads:
Ads

Windows 8 Drivers Latest Download. Microsoft Certified. (Recommended)

How do you build & grow independent advisor business? Free download.

cpt codes lookup Medical, Diagnostics, and More!

See More About
Ads

Programming Courses Info. Access 10 Search Engines At Once.

2 Year MSDN Premium $2190 For best prices call 800-900-1008

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.

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.

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

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

  • CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, 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).

Standard Java Naming Conventions

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

  • 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
  • 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
  • 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
  • Methods: Names should be in mixed case. Use verbs to describe what the method does:
    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 }
  • Constants: Names should be in uppercase.
    static final int DEFAULT_WIDTH static final int MAX_HEIGHT

转载于:https://my.oschina.net/simon203/blog/123006

你可能感兴趣的文章
诺基亚将在 MWC 上发布低成本 Android 手机
查看>>
《Outlook时间整理术》一不是电子邮件的问题,而是我们应如何处理它
查看>>
《Adobe Premiere Pro CS5经典教程》——第1课 Adobe Premiere Pro CS5概述 1.1 Adobe Premiere Pro CS5中的新功能...
查看>>
设计师是不是真正的用户
查看>>
《CCIE路由和交换认证考试指南(第5版) (第1卷)》——1.2节以太网第1层:线缆、速率和双工...
查看>>
补丁不起作用:Mac平台安全漏洞仍然存在
查看>>
《Spark核心技术与高级应用》——导读
查看>>
首席技术官 (CTO) 比普通程序员强在哪
查看>>
《交互式程序设计 第2版》一1.4 艺术与交互
查看>>
《脱颖而出——成功网店经营之道》一2.2 进货攻略
查看>>
X.Org 可能将失去它的域名 x.org
查看>>
《Adobe Photoshop CC经典教程(彩色版)》—第4课4.2节概述
查看>>
互联网企业安全高级指南3.1 从零开始
查看>>
后台权限管理的菜单设计
查看>>
linux搭建git服务器
查看>>
【原创】Percona 之 tcprstat 安装及使用
查看>>
oracle中drop后的表清楚表的含义
查看>>
js笔记——js数据类型转换
查看>>
Hadoop2.5.2集群部署(完全分布式)
查看>>
禁止sshd暴力尝试方案
查看>>