Maison  >  Article  >  php教程  >  在项目中寻找代码的坏味道(命名)

在项目中寻找代码的坏味道(命名)

WBOY
WBOYoriginal
2016-06-13 11:15:031042parcourir

介绍

这段时间一直做项目,所以相对忙碌些,今天终于有时间回过头来好好看一下自己写的代码,看哪里有问题,哪里有“坏味道”。

慢慢过,慢慢回忆代码。开始捕捉坏味道。

常做的和常想的事情

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095419483.png" />

晦涩的if条件

1)对于||的处理

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095420518.png" />

                                  498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095420978.png" />上面的代码要比下面的代码理解起来花更多的时间

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095420225.png" />

尽管减少代码行数是一个好目标,但把理解代码所需的时间最小化是一个更好的目标。

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095420376.png" />

返回值

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095421511.png" />

 498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095420978.png" />上面的"$ret"是“我想不出名字“的托词。与其使用这样空洞的名字,不如挑一个能描述这个实体的值或者目的的名字。

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095421331.png" />

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095420978.png" />$alias声明了这个变量是用来承载别名的——标明了这个变量的目的。并且可能帮我们找到缺陷

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095421813.png" />

好的名字应当描述变量的目的或者它所承载的值。

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095422796.png" />

临时变量

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095422838.png" />

 498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095420978.png" /> 这里的$i是“我的临时变量",专门用来让统计数据自动增加,避免统计点的重复。但是对这个$i来讲最重要的并不是临时变量。用charset_index表示“我的统计数据的下标”,更具“描述性”。

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095422626.png" />

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095423790.png" />

循环迭代器

我在我的js代码中也发现了坏味道。

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095423953.png" />

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095420978.png" />i变量名字很空泛,所以不要这么做。

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095423531.png" />

像i,j,iter和it等名字常用做索引和循环迭代器。尽管名字很空泛,但是大家都知道它们的意思是“我是一个迭代器“。——实际上你用这些名字来表示其他含义,那会很混乱。所以不要这么做。

如果你非要用i,j,it这样空泛的名字,那么你要有个好的理由说服自己。

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095424739.png" />

总结

我们在编码的过程中,多花几秒钟想出一个好名字,你会发现我们的“命名能力“很快提升上去。

我一般是先想中文名字,如果实在想不出对应的英文名字,我就会用翻译工具把想到的中文贴上去,然后进行裁剪命名变量或者函数名称。

赠送一张最近看到的不错的命名

498)this.width=498;' onmousewheel = 'javascript:return big(this)' alt="\" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095424909.png" />


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn