日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

写出漂亮代码的七种方法

發(fā)布時(shí)間:2024/7/19 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 写出漂亮代码的七种方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

首先我想說(shuō)明我本文闡述的是純粹從美學(xué)的角度來(lái)寫(xiě)出代碼,而非技術(shù)、邏輯等。以下為寫(xiě)出漂亮代碼的七種方法:

????1.盡快結(jié)束 if 語(yǔ)句

????例如下面這個(gè)JavaScript語(yǔ)句,看起來(lái)就很恐怖:

?function findShape(flags, point, attribute, list) {
????if(!findShapePoints(flags, point, attribute)) {
????????if(!doFindShapePoints(flags, point, attribute)) {
????????????if(!findInShape(flags, point, attribute)) {
????????????????if(!findFromGuide(flags,point) {
????????????????????if(list.count() > 0 && flags == 1) {
??????????????????????????doSomething();
????????????????????}
????????????????}
????????????}
???????}
????}
??}

????但如果這么寫(xiě)就好看得多:

?function findShape(flags, point, attribute, list) {
????if(findShapePoints(flags, point, attribute)) {
????????return;
????}

????if(doFindShapePoints(flags, point, attribute)) {
????????return;
????}

????if(findInShape(flags, point, attribute)) {
????????return;
????}

????if(findFromGuide(flags,point) {
????????return;
????}

????if (!(list.count() > 0 && flags == 1)) {
????????return;
????}

????doSomething();

?}

????你可能會(huì)很不喜歡第二種的表述方式,但反映出了迅速返回if值的思想,也可以理解為:避免不必要的else陳述。

????2.如果只是簡(jiǎn)單的布爾運(yùn)算(邏輯運(yùn)算),不要使用if語(yǔ)句

????例如:

?function isStringEmpty(str){
????if(str === "") {
????????return true;
????}
????else {
????????return false;
????}
?}

????可以寫(xiě)為:

?function isStringEmpty(str){
????return (str === "");
?}

????3.使用空白,這是免費(fèi)的

????例如:

?function getSomeAngle() {
????// Some code here then
????radAngle1 = Math.atan(slope(center, point1));
????radAngle2 = Math.atan(slope(center, point2));
????firstAngle = getStartAngle(radAngle1, point1, center);
????secondAngle = getStartAngle(radAngle2, point2, center);
????radAngle1 = degreesToRadians(firstAngle);
????radAngle2 = degreesToRadians(secondAngle);
????baseRadius = distance(point, center);
????radius = baseRadius + (lines * y);
????p1["x"] = roundValue(radius * Math.cos(radAngle1) + center["x"]);
????p1["y"] = roundValue(radius * Math.sin(radAngle1) + center["y"]);
????pt2["x"] = roundValue(radius * Math.cos(radAngle2) + center["y"]);
????pt2["y"] = roundValue(radius * Math.sin(radAngle2) + center["y");
????// Now some more code
?}

????很多開(kāi)發(fā)者不愿意使用空白,就好像這要收費(fèi)一樣。我在此并非刻意地添加空白,粗魯?shù)卮驍啻a的連貫性。在實(shí)際編寫(xiě)代碼的過(guò)程中,會(huì)很容易地發(fā)現(xiàn)在什么地方加入空白,這不但美觀而且讓讀者易懂,如下:

?function getSomeAngle() {
????// Some code here then
????radAngle1 = Math.atan(slope(center, point1));
????radAngle2 = Math.atan(slope(center, point2));

????firstAngle = getStartAngle(radAngle1, point1, center);
????secondAngle = getStartAngle(radAngle2, point2, center);

????radAngle1 = degreesToRadians(firstAngle);
????radAngle2 = degreesToRadians(secondAngle);

????baseRadius = distance(point, center);
????radius = baseRadius + (lines * y);

????p1["x"] = roundValue(radius * Math.cos(radAngle1) + center["x"]);
????p1["y"] = roundValue(radius * Math.sin(radAngle1) + center["y"]);

????pt2["x"] = roundValue(radius * Math.cos(radAngle2) + center["y"]);
????pt2["y"] = roundValue(radius * Math.sin(radAngle2) + center["y");
????// Now some more code
?}

????4.不要使用無(wú)謂的注釋

????無(wú)謂的注釋讓人費(fèi)神,這實(shí)在很討厭。不要標(biāo)出很明顯的注釋。在以下的例子中,每個(gè)人都知道代碼表達(dá)的是“students id”,因而沒(méi)必要標(biāo)出。

?function existsStudent(id, list) {
????for(i = 0; i < list.length; i++) {
???????student = list[i];

???????// Get the student's id
???????thisId = student.getId();

???????if(thisId === id) {
???????????return true;
???????}
????}
????return false;
?}

5.不要在源文件中留下已經(jīng)刪除的代碼,哪怕你標(biāo)注了

????如果你使用了版本控制,那么你就可以輕松地找回前一個(gè)版本的代碼。如果別人大費(fèi)周折地讀了你的代碼,卻發(fā)現(xiàn)是要?jiǎng)h除的代碼,這實(shí)在太恨人了。

?//function thisReallyHandyFunction() {
//??????someMagic();
//??????someMoreMagic();
//??????magicNumber = evenMoreMagic();
//??????return magicNumber;
//}

????6.不要有太長(zhǎng)的代碼

????看太長(zhǎng)的代碼實(shí)在太費(fèi)勁,尤其是代碼本身的功能又很小。如下:

?public static EnumMap getGroupCategoryDistribution(EnumMap sizes, int groups) {
????????EnumMap categoryGroupCounts = new EnumMap(Category.class);

????????for(Category cat : Category.values()) {
????????????categoryGroupCounts.put(cat, getCategoryDistribution(sizes.get(cat), groups));
????????}

????我并不是說(shuō)非要堅(jiān)持70個(gè)字符以內(nèi),但是一個(gè)比較理想的長(zhǎng)度是控制在120個(gè)字符內(nèi)。如果你把代碼發(fā)布在互聯(lián)網(wǎng)上,用戶讀起來(lái)就很困難。

????7.不要在一個(gè)功能(或者函數(shù)內(nèi))有太多代碼行

????我的一個(gè)老同事曾經(jīng)說(shuō)Visual?C++很 臭,因?yàn)樗辉试S你在一個(gè)函數(shù)內(nèi)擁有超過(guò)10,000行代碼。我記不清代碼行數(shù)的上限,不知道他說(shuō)的是否正確,但我很不贊成他的觀點(diǎn)。如果一個(gè)函數(shù)超過(guò)了 50行,看起來(lái)有多費(fèi)勁你知道么,還有沒(méi)完沒(méi)了的if循環(huán),而且你還的滾動(dòng)鼠標(biāo)前后對(duì)照這段代碼。對(duì)我而言,超過(guò)35行的代碼理解起來(lái)就很困難了。我的建 議是超過(guò)這個(gè)數(shù)字就把一個(gè)函數(shù)代碼分割成兩個(gè)。

轉(zhuǎn)載于:https://www.cnblogs.com/flying-roc/archive/2012/03/21/2410398.html

總結(jié)

以上是生活随笔為你收集整理的写出漂亮代码的七种方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。