• 技术文章 >php教程 >php手册

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

    2016-06-13 11:15:03原创494
    介绍

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

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

    常做的和常想的事情

    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" />

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:PHP实现人人OAuth登录和API调用 下一篇:PHP生成图片水印和文字水印
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• 一个odbc连mssql分页的类_php基础• php后台多用户权限组思路与实现程序代码分享• phpcms定时发布文章解决办法• php文件上传简单实现方法,• php5.4以下版本json不支持不转义内容中文的解决方法,
    1/1

    PHP中文网