本文转自网上看到的一篇博客,非常赞同。 中文链接:如何编写更棒的代码:11个核心要点 英文原文:11 tips for better code
作为一个合格的程序员,有太多的理由促使你去编写干净利落且可读性强的代码。最重要的是因为你编写的代码,将来会有很多人一次次地阅读。当你有一天回过头来看自己的代码时,你就会明白编写优雅的代码是多么的重要。另外,如果别人来阅读你编写的代码,你是否想知道别人看到那些烂代码无比抓狂的感受。因此,花多一点的时间去编写优雅的代码,将来说不定会给你节省更多的时间。
Keep methods short
保持方法简短扼要
Never ever ever reuse a variable for different purpose
永远永远不要将同一个变量用于不同的目的
Use self-descriptive variable and method names
尽可能让变量和方法的名称能够描述要实现的功能
Define variables as close as possible to the place of their first usage*
尽可能将变量定义在最靠近它们的地方
No magic numbers不要出现让人费解的数字
Be friend with your language
要像对待朋友一样对待你擅长的语言
Don’t fight the convention
不要逆常规而行
Watch out for premature optimization
千万小心过早的优化代码
Always refactor the code after you test it
要常常重构经过测试的代码
Don’t get sucked into overengineering
不要沉溺于过度的设计技巧
Learn new things by prototyping
随时随地学习新的知识
2014-05-27