Note of CLEAN CODE chapter 2 - Meaningful Name

发布时间:2024年01月19日

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;
文章来源:https://blog.csdn.net/JamSlade/article/details/135707242
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。