官术网_书友最值得收藏!

Meaningful variable names

Type inference with var should be used responsibly. When you remove explicit data type from a variable declaration, the variable name takes the center stage. With inferred types, it is your responsibility to use descriptive and appropriate variable names, so that they make more sense in code. As you know, a piece of code is written once, but read many times.

For example, the following line of code won't make much sense to you or to your team members (especially with a big or distributed team) after a period of time:

var i = getData();             // what does getData() return? Is 'i' a 
// good name?

The key questions are—what is the variable i used for? What does the method getData() return? Imagine the plight of the other team members that will work with this code after you leave.

Also, it doesn't help to define variable names that are mismatched with their purposes. For example, it doesn't make much sense to create a connection object named database and assign a URL instance to it, or to define a variable with the name query and assign a Connection instance to it:

var database = new URL("http://www.eJavaGuru.com/malagupta.html"); 
var query = con.getConnection(); 
When variables are defined using var, variable names become more important. Without a type, it can become difficult to understand the purpose of a variable, especially if its name is not expressive enough. Choose variable names carefully and responsibly, making their purpose clear.
主站蜘蛛池模板: 任丘市| 镇远县| 鹤岗市| 晋江市| 长沙县| 阿鲁科尔沁旗| 平阴县| 大洼县| 安国市| 长海县| 巴林右旗| 云霄县| 西昌市| 北碚区| 江门市| 湖口县| 高邮市| 大余县| 双柏县| 阳高县| 富民县| 涿州市| 泌阳县| 卓资县| 鄂伦春自治旗| 利川市| 新乡县| 青川县| 高邑县| 淄博市| 东阳市| 南京市| 湟源县| 阿拉尔市| 道真| 永泰县| 石门县| 峨眉山市| 金沙县| 含山县| 格尔木市|