Note of CLEAN CODE chapter 2 - Meaningful Name

Use Intention-Revealing Names

names should reveal intent

int d;// what is this variable used for?

d reveals nothing. If d is for elapsed time in days, you should name it.

int elapsedTimeInDays;
List<int[]> result = new ArrayList();
for(int[] cell : cells){
    if(cell[STATUS_VALUE] == FLAGGED){
        result.add(cell);
    }
}

as you can see, all variable names have their respectful meanings, But there are two points that can be modified: We can package int[] into a class Cell, and package cell[STATUS_VALUE] == FLAGGED as a function isFlagged()

List<Cell> result = new ArrayList();
for(Cell cell : cells){
    if(cell.isFlagged()){
        result.add(cell);
    }
}

Avoid Disinformation

Do not leave false clue.

  1. Do not name a variable as xxxList unless it is a List ( The List has a specific meaning to programmers). If not, it might be better to use xxxs or xxxGroup
  2. Be aware of the variable names which vary in small ways, like data and date; I, l and 1; 0, o and O; thisIsALongVariable and thisIsaLongVariable

Meaningful Distinctions

When it comes to the constant variable, you can name it, too! It is easier to recognize FRIDAY than 5 in a code

if(day == 5)

const int FRIDAY = 5;
if(day == FRIDAY)

disctions

xxxData and xxxInfo mean no distinction even you’ve made their names different. And some stop words (noise words) like a an the are distinct words, too.

More examples:

  • variable should never appeas in a variable name
  • nameString is not better than name. Would a name ever a floating number?
  • getAccount() getAccounts() getAccountInfo() are indistinguishable.

Pronounceable Names

Even if you can understand genymhms means generate year month day hour minute and second, you should make it pronouncable like generateTimestamp

Searchable Name

for(int i = 0; i < 34; ++i){
    s += t[i] *4 / 5;
}

There codes are harder to be found than the ones below

const int TASK_NUMBER = 34;
const int WORK_DAYS_OF_A_WEEK = 5; 
int sum = 0;
for(int i = 0; i < TASK_NUMBER; ++i){
    int realTaskDays = taskEstimate[i] * realDaysPerIdealDay;
    int realTaskWeeks = realdays / WORK_DAYS_OF_A_WEEK;
    sum += realTaskWeeks;
}

Avoid Encoding

Hungarian Notation

  • There is no need to add the type of the variable in the front of the name like strXXX intXXX

Memebr Prefix

you don’t have to prefix m_ with member variables, and lp_ with long pointers

Interface and Implement

It is suggested to use xxx as interface and xxxImpl as implement, instead of Ixxx as interface and xxx as implement.

Avoid Mental Mapping

avoid use single-letter variable to juggle

Class Name

  • class names should be a noun rather than a verb
  • avoid using manager, Processor, data, info

Method Name

methods should have verb or verb phrase names like save getName

If there is overloaded constructor, it is supposed to use static factory methods with name that describes arguments;

Complex.fromRealNumber(23.0);

new Complex(23.0);

Don’t be cute

avoid colloquialism, just mean what you say

Pick one work per Concept

get retrieve fetch are basically equivalent methods
controller maneger driver

Don’t pun

Using the same term for two different ideas is a pun.

For example, when you has a method that puts a variable to a collection, should you use add? Maybe it is better to use insert or append to avoid pun.

Use Solution/Problem Domain Name

(使用解决方案领域名称,使用源自所涉问题领域的名称)

Add Meaningful Context

public enum SellerApplyStatusEnum {
    APPLY_STATUS_DEFAULT(0, "待审核"),
    APPLY_STATUS_ENABLE(1, "通过审核"),
    APPLY_STATUS_IGNORE(2, "忽略的申请"),
    APPLY_STATUS_MODIFY(3, "退回修改"),
    APPLY_STATUS_BRAND(4, "等待商家添加品牌"),
    APPLY_STATUS_REJECT(5, "暂不合作");
}

The Class name has shown it is a status, so the prefixes are redundant.

Don’t add gratuitous context

  • Like unified prefix to make you hard for IDE to help you
  • or add redundant or irrevelant words in a variable name;
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值