熊掌号结合网站做seo,百度如何搜索网址,通过wordpress建站,新的网站的建设步骤首先我想说明我本文阐述的是纯粹从美学的角度来写出代码#xff0c;而非技术、逻辑等。以下为写出漂亮代码的七种方法#xff1a; 1.尽快结束 if 语句 例如下面这个JavaScript语句#xff0c;看起来就很恐怖#xff1a; function findShape(flags, point, attribute, list)…首先我想说明我本文阐述的是纯粹从美学的角度来写出代码而非技术、逻辑等。以下为写出漂亮代码的七种方法 1.尽快结束 if 语句 例如下面这个JavaScript语句看起来就很恐怖 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(); } } } } } } 但如果这么写就好看得多 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(); } 你可能会很不喜欢第二种的表述方式但反映出了迅速返回if值的思想也可以理解为避免不必要的else陈述。 2.如果只是简单的布尔运算逻辑运算不要使用if语句 例如 function isStringEmpty(str){ if(str ) { return true; } else { return false; } } 可以写为 function isStringEmpty(str){ return (str ); } 3.使用空白这是免费的 例如 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 } 很多开发者不愿意使用空白就好像这要收费一样。我在此并非刻意地添加空白粗鲁地打断代码的连贯性。在实际编写代码的过程中会很容易地发现在什么地方加入空白这不但美观而且让读者易懂如下 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.不要使用无谓的注释 无谓的注释让人费神这实在很讨厌。不要标出很明显的注释。在以下的例子中每个人都知道代码表达的是“students id”因而没必要标出。 function existsStudent(id, list) { for(i 0; i list.length; i) { student list[i]; // Get the students id thisId student.getId(); if(thisId id) { return true; } } return false; } 5.不要在源文件中留下已经删除的代码哪怕你标注了 如果你使用了版本控制那么你就可以轻松地找回前一个版本的代码。如果别人大费周折地读了你的代码却发现是要删除的代码这实在太恨人了。 //function thisReallyHandyFunction() {// someMagic();// someMoreMagic();// magicNumber evenMoreMagic();// return magicNumber;//} 6.不要有太长的代码 看太长的代码实在太费劲尤其是代码本身的功能又很小。如下 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)); } 我并不是说非要坚持70个字符以内但是一个比较理想的长度是控制在120个字符内。如果你把代码发布在互联网上用户读起来就很困难。 7.不要在一个功能或者函数内有太多代码行 我的一个老同事曾经说Visual C很 臭因为它不允许你在一个函数内拥有超过10000行代码。我记不清代码行数的上限不知道他说的是否正确但我很不赞成他的观点。如果一个函数超过了 50行看起来有多费劲你知道么还有没完没了的if循环而且你还的滚动鼠标前后对照这段代码。对我而言超过35行的代码理解起来就很困难了。我的建 议是超过这个数字就把一个函数代码分割成两个。转载于:https://www.cnblogs.com/flying-roc/archive/2012/03/21/2410398.html