命名规范~

1. 命名原则

1.1 准确性 

可读性

"类"名应该是"是什么"。应该是一个名词,作为主语。

"方法"名应该是"干什么"。一个方法应该是动词,作为谓语。

避免不必要的缩写

把类/ 方法的名字写全。但是,首字母缩略词的术语是可行并且推荐的,如 Http , Id , Url 。

以下是可用的、得到普遍认可的缩写:
configuration -> config
identifier -> id
specification -> spec
statistics -> stats
database -> db (only common in Go)
regular expression -> re/regex/regexp

未得到普遍认可的缩写:
request -> req
response -> resp/rsp
service -> svr
object -> obj
metadata -> meta
business -> busi

避免双关

对类/方法的命名,不要使用 2 表示 To, 4 表示 For。

合乎语法

虽然不能完全符合语法(例如通常会省略冠词),但是,方法的命名应该尽量符合语法。例如:

class Car {
    void tireReplace(Tire tire); // BAD, reads like "Car's tire replaces"
    void replaceTire(Tire tire); // GOOD, reads like "replace car's tire"
}

使用单一的概念命名

选择一个单词,能够包含类的全部信息。 

class Fridge {
    public void openDoorAndMoveObjectIntoFridgeAndCloseDoor(Elephant elphant); // BAD
    public dealWith(Elephant elephant); // BAD: deal with? Anything can be dealt with. How?
    public void put(Elephant elphant); // GOOD
}

1.2 简洁 Simplicity

public,如 public 类的名字、public 方法的名字 - 应该详细、不使用缩写、减少依赖
上下文。通常是完整名词短语。

non-public,如类成员、私有方法 - 不使用缩写、可以省略上下文。下界是单词,不
应该使用单字符。

local,如函数的局部变量 - 基本上是风格是自由的。不影响可读性的前提下,例如函
数方法长度很短,可以使用单字符指代成员。

1.3 一致 Consistency

message Record {
    int32 start_time_millis = 1; // OK
    int32 commited_at = 2; // Wait. Why not commit_time? Anything special?
    int32 update_time = 3; // What unit? Also millis?
    google.types.Timestamp end_time = 4; // WTF? Why only end_time is typed?
}

2. 语法规则

 类

类应该是名词形式,通常由单个名词或名词短语组成。

名词短语通常不使用所有格。如,并非 ServiceOfBook ,也不是 BooksService
(省略 '),而是 BookService 。
 接口

接口的命名规则和类相同。除此之外,当接口表示可行动类型时,可使用另一个语法,即 Verb-able 。例如:

public interface Serializable {
    byte[] serialize();
}
public interface Copyable<T> {
    T copy();
}
public interface Closable {
    void close();
}
辅助类

一个类或概念所有的辅助方法应该聚合在同一个辅助类。这个类 应该以被辅助类的复数形式出现。不推荐使用 Helper/Utils 后缀表示辅助类。

class Collections {} // For Collection
class Strings {} // For String
class BaseRuleClasses {} // For BaseRuleClass
class StringUtils {} // WORSE!
class StringHelper {} // WORSE!

 方法

方法通常是谓语(动词),或是 谓宾(动词+名词) 结构。注意以上语法中,动词都在最 前端。例如:

class Expander {
    String expand(String attribute); // 主-谓
    String expandAndTokenizeList(String attribute, List<String> values); // 主-谓-宾
}
 访问器 Getter

直接使用所 Get 的对象的名词形式,即 Foo() 。不要使用 GetFoo() 。

func Counts() int; // GOOD
func GetCounts() int; // BAD: UNNECESSARY.
断言 Predicate

断言函数指返回结果是布尔型(即真伪值)的函数。它们通常有以下命名格式:

系动词: 主-系-表

即 isAdjective() 或 areAdjective() 格式,表示是否具有某个二元属性。类似于 Getter, 可以省略系语,只使用表语,即: adjective() 。

func IsDone() bool {} // OK-ish. But could be better

func Done() bool {} // GOOD. Why bother with is/are?

func CheckEnabled() bool {
     // BAD. Nobody cares if it is "checked". Just tell the user if it is return enabled;
}

func Enabled() bool {} // GOOD.
情态动词:

情态动词也是常见的断言形式。常见的是以下三个:
 

should: 查询当前是否应该执行给定的实义动词。

can: 查询当前类所在状态是否可以执行给定的实义动词。某些情况下,也可以使用 第三人称单数作为更简洁的代替。

must: 特殊形式。不同于前两者,会执行给定的实义动词。must 表示执行必须成功, 否则会抛出不可恢复错误 (throw/panic)。类似于 C++ 中常见的 OrDie 后缀。
func Compile(s string) Regexp, error // Returns error upon failure

func MustCompile(s string) Regexp // Panics upon failure

func (r Regexp) CanExpand(s string) bool // Whether s is legal and can be expanded

func (r Regexp) Expands(s string) bool // Whether r expands s, i.e. r can expand s.

func (r Regexp) ShouldReset() bool // Whether the state requires reset. Does not perform de-fac

func (r Regexp) Reset() // De-facto reset.
表尝试:maybe/try

上文 "must" 的反面,表示尝试性的执行,并且失败不会造成严重后果:
 

maybe 前缀用以表示指定的行为有前置条件,也在方法中执行。如果前置条件不满 足,不会执行指定行为。通常不会出现在公开 API。

try 通常用于 Try-Parse Pattern,用于避免抛出异常。
void maybeExecute() {
    if (!predicate()) {
        return;
    }
    // execute
}

std::unique_ptr<DateTime> ParseOrDie(std::string_view dateTime);
bool TryParse(string_view dateTime, DateTime* dateTime);
一阶逻辑 

一阶逻辑量词也是常见的前缀:

all 表示所有对象满足给定要求
any 表示任意对象满足给定要求
none 表示没有任何对象满足给定要求
class Stream {
    // Returns whether all elements of this stream match the provided predicate.
    boolean allMatch(Predicate<? super T> p);

    // Returns whether any elements of this stream match the provided predicate.
    boolean anyMatch(Predicate<? super T> p);

    // Returns whether no elements of this stream match the provided predicate.
    boolean noneMatch(Predicate<? super T> predicate)
}
介词
to: 转换至另一对象,等价于 convertTo。to 会产生一个全新的对象,通常不持有对原对象的引用。

as: 返回某个视图,等价于 returnViewAs。一个“视图(View)” 通常是对原有对象的另一角度的抽象,通常会持有对原有数据的引用,而不会产生新的数据。

of/from/with:构造新对象,等价于 createOutOf/createFrom/createWith。见下文“工厂模式”。

on: 监听事件,等价于 actUpon。见下文“事件”。
class Foo {
public List<T> toList(); // Convert to (Construct a new instance of) a new List. Creates a
public List<T> asList(); // Return a List as a different **view**. Holds reference of the o
static Foo of(); // Construct Foo as a factory method.
static Foo from(Bar); // Construct Foo from Bar.
Foo with(Bar); // Construct a new Foo by replacing Bar with new Bar.
void onClick(ClickEvent e); // Act upon click event.
}

3. 具体使用

接口:interface
实现:Impl
Abstract class:Base

异常

std::exception;
std::runtime_error

测试

Test后缀

模块/模组

Module或Component

服务

Service后缀

容器

Holder/Container/Wrapper

控制类

Manager/Controller

辅助类

{ClassName}s
避免使用 Util/Utility/Utils/Helper 。它们是无意义词汇。

函数式

 fun

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值