It is said that programmers are most afraid of naming! This 6300 star manual can help

Time:2021-9-11

[introduction]: naming cheat sheet is a naming memo, which records some common norms and conventions of naming.

brief introduction

In programming, naming is a headache for many developers. There was a poll abroad about the most difficult tasks for programmers, and the results showed that naming accounted for 49 \%.

It is said that programmers are most afraid of naming! This 6300 star manual can help

A good variable or function name should play a role of self interpretation and even reduce the comments of our code.

Naming cheat sheet is a naming memo that records some common specification conventions and provides simple examples. If we can strictly abide by these specifications, we believe that our code readability will be greatly improved. Here are some suggestions provided by naming cheatsheet.

Project address:

https://github.com/kettanaito…

Use English

This is the most basic rule. English is the main language in programming. The grammar of all programming languages is written in English. Writing code in English can greatly improve its universality. For our domestic developers, we must avoid pinyin or even direct Chinese naming.

/* Bad */  
const primerNombre = 'Gustavo'  
const amigos = ['Kate', 'John']  
  
/* Good */  
const firstName = 'Gustavo'  
const friends = ['Kate', 'John']  

Naming style

Choose a naming style and strictly abide by it. It can be camelCase or snake\_ Case, or any other style, the most important thing is to keep consistent. Whether it is an individual developer or a team, it is important to keep a consistent naming style. Do not mix them.

/* Bad */  
const page_count = 5  
const shouldUpdate = true  
  
/* Good */  
const pageCount = 5  
const shouldUpdate = true  
  
/* Good as well */  
const page_count = 5  
const should_update = true  

Comply with Sid principles

Naming should be short, intuitive and descriptive, following Sid principles.

  • Short。 Be short and avoid entering too long, but be careful not to lose its original meaning.
  • Intuitive。 Intuitive and as close to natural language as possible.
  • Descriptive。 Reflect its role or purpose in the most effective way.
/* Bad */  
const a = 5 // "a" could mean anything  
const isPaginatable = a > 10 // "Paginatable" sounds extremely unnatural  
const shouldPaginatize = a > 10 // Made up verbs are so much fun!  
  
/* Good */  
const postCount = 5  
const hasPagination = postCount > 10  
const shouldPaginate = postCount > 10 // alternatively  

Avoid excessive shorthand

The naming should be short, but we should avoid getting into a tight corner. The most important thing of naming is to make people understand. If excessive abbreviations lose their original meaning and reduce the readability of the code, they should not do so. They prefer to write more letters.

/* Bad */  
const onItmClk = () => {}  
  
/* Good */  
const onItemClick = () => {}  

Avoid context duplication

Sometimes a variable definition with similar meaning may appear in a piece of code. At this time, avoid naming repetition.

class MenuItem {  
  /* Method name duplicates the context (which is "MenuItem") */  
  handleMenuItemClick = (event) => { ... }  
  
  /* Reads nicely as `MenuItem.handleClick()` */  
  handleClick = (event) => { ... }  
}  

Reflect expected results

The naming of variables or functions should reflect the expected results.

/* Bad */  
const isEnabled = itemCount > 3  
return <Button disabled={!isEnabled} />  
  
/* Good */  
const isDisabled = itemCount <= 3  
return <Button disabled={isDisabled} />  

Named schema

You can refer to the following similar patterns for naming.

A / HC / LC mode

A / HC / LC can be followed, i.e

prefix? + action (A) + high context (HC) + low context? (LC)  
name prefix A HC LC
getUser
get User
getUserMessages
get User Messages
handleClickOutside
handle Click Outside
shouldDisplayMessage should Display Message

The order of context may affect the meaning of variables. For example, shouldupdatecomponent means that a component will be updated. Changing the order to shouldcomponentupdate means that the component will update itself.

action

The verb part of the function name is the final part to describe the function, such as:

  • Getxxx means to obtain data
  • SetXXX indicates the set value
  • Resetxxx, reset data
  • Fetchxxx, request data
  • Removexxx, to remove data, means to delete something from somewhere
  • Deletexxx, deleting data, means that you know something completely
  • Composexxx to create new data from existing data
  • Handlexxx, handle an action

context

A function or method is usually an action of something. Combined with the context, it can specify the object of its operation, or it should reflect the expected data type of the function. In some specific cases, it is allowed to omit the context. For example, in JavaScript, it is common for filter to operate on array, so it is unnecessary to name it filterarray.

/* A pure function operating with primitives */  
function filter(predicate, list) {  
  return list.filter(predicate)  
}  
  
/* Function operating exactly on posts */  
function getRecentPosts(posts) {  
  return filter(posts, (post) => post.date === Date.now())  
}  

prefix

Prefixes are used to enhance the meaning of variables, such as:

  • Is, which describes a feature or state, usually a boolean type
  • Has, which describes whether it has a certain state or value, usually of boolean type
  • Should, reflecting positive conditions, plus specific execution actions
  • Min / max, used when describing boundary or limit
  • Prev / next, indicating the previous or next status

Singular and plural

Whether the variable name is singular or plural depends on whether the value is singular or plural.

/* Bad */  
const friends = 'Bob'  
const friend = ['Bob', 'Tony', 'Tanya']  
  
/* Good */  
const friend = 'Bob'  
const friends = ['Bob', 'Tony', 'Tanya']  

– EOF – 

Open source outpostShare popular, interesting and practical open source projects on a daily basis. Participate in maintaining the open source technology resource library of 100000 + star, including python, Java, C / C + +, go, JS, CSS, node.js, PHP,. Net, etc.