编程小贴士

给你的编程提供小点子


每个程序员都必须遵守的编程原则

我不要自我重复 ― 这也许是在编程开发这最最基本的一个信条,就是要告诉你不要出现重复的代码。我们很多的编程结构之所以存在,就是为了帮助我们消除重复(例如,循环语句, 函数,类,等等)。一旦程序里开始有重复现象的出现(例如很长的表达式、一大堆的语句,但都是为了表达相同的概念),你就需要对代码进行一次新的提炼,抽 象。

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

提炼原则 ― 跟“不要自我重复原则”相关,这一原则是说“程序中任何一段具有功能性的代码在源代码文件中应该唯一的存在。”

http://en.wikipedia.org/wiki/Abstraction_principle_(programming)

保持简单 ― 简单化(避免复杂)永远都应该是你的头等目标。简单的程序让你写起来容易,产生的bug更少,更容易维护修改。

http://en.wikipedia.org/wiki/KISS_principle

不要开发你目前用不到的功能 ― 除非你真正需要用到它,否则不要轻易加上那些乱七八糟用不到的功能。

http://en.wikipedia.org/wiki/YAGNI

用最简单的方法让程序跑起来 ― 在开发时有个非常好的问题你需要问问自己,“怎样才能最简单的让程序跑起来?”这能帮助我们在设计时让程序保持简单。

http://c2.com/xp/DoTheSimplestThingThatCouldPossiblyWork.html

不要让我动脑子 ― 这实际上是Steve Krug 关于web界面操作的一本书的书名,但也适用于编程。主旨是,程序代码应该让人们花最小的努力就能读懂和理解。如果一段程序对于阅读者来说需要花费太多的努力才能理解,那它很可能需要进一步简化。

http://www.sensible.com/dmmt.html

开放/封闭原则 ― 程序里的实体项(类,模块,函数等)应该对扩展行为开放,对修改行为关闭。换句话说,不要写允许别人修改的类,应该写能让人们扩展的类。

http://en.wikipedia.org/wiki/Open_Closed_Principle

为维护者写程序 ― 任何值得你编写的程序在将来都是值得你去维护的,也许由你维护,也许由他人。在将来,当你不得不维护这些程序时,你对这些代码的记忆会基本上跟一个陌生人 一样,所以,你最好还是当成一直在给别人写程序。一个有助于你记住这个原则的办法是“写程序时时刻记着,这个将来要维护你写的程序的人是一个有严重暴力倾 向,并且知道你住在哪里的精神变态者”。

http://c2.com/cgi/wiki?CodeForTheMaintainer

最少意外原则 ― 最少意外原则通常是使用在用户界面设计上,但这个原则同样适用于编写程序。程序代码应尽可能的不要让阅读者感到意外。也就是说应该遵循编码规范和常见习惯,按照公认的习惯方式进行组织和命名,不符常规的编程动作应该尽可能的避免。

http://en.wikipedia.org/wiki/Principle_of_least_astonishment

单一职责原则 ― 一个代码组件(例如类或函数)应该只执行单一的预设的任务。

http://en.wikipedia.org/wiki/Single_responsibility_principle

最小化耦合关系 ― 一个代码片段(代码块,函数,类等)应该最小化它对其它代码的依赖。这个目标通过尽可能少的使用共享变量来实现。“低耦合是一个计算机系统结构合理、设计优秀的标志,把它与高聚合特征联合起来,会对可读性和可维护性等重要目标的实现具有重要的意义。”

http://en.wikipedia.org/wiki/Coupling_(computer_programming)

最大化内聚性 ― 具有相似功能的代码应该放在同一个代码组件里。

http://en.wikipedia.org/wiki/Cohesion_(computer_science)

隐藏实现细节 ― 隐藏实现细节能最小化你在修改程序组件时产生的对那些使用这个组件的其它程序模块的影响。

http://en.wikipedia.org/wiki/Information_Hiding

笛米特法则(Law of Demeter) ― 程序组件应该只跟它的直系亲属有关系(例如继承类,内包含的对象,通过参数入口传入的对象等。)

http://en.wikipedia.org/wiki/Law_of_Demeter

避免过早优化 ― 只有当你的程序没有其它问题,只是比你预期的要慢时,你才能去考虑优化工作。只有当其它工作都做完后,你才能考虑优化问题,而且你只应该依据经验做法来优 化。“对于小幅度的性能改进都不该考虑,要优化就应该是97%的性能提升:过早优化是一切罪恶的根源”―Donald Knuth。

http://en.wikipedia.org/wiki/Program_optimization

代码复用 ― 这不是非常核心的原则,但它跟其它原则一样非常有价值。代码复用能提高程序的可靠性,节省你的开发时间。

http://en.wikipedia.org/wiki/Code_reuse

职责分离 ― 不同领域的功能应该由完全不同的代码模块来管理,尽量减少这样的模块之间的重叠。

http://en.wikipedia.org/wiki/Separation_of_concerns

拥抱变化 ― 这是Kent Beck的一本书的副标题,它也是极限编程和敏捷开发方法的基本信条之一。很多的其它原则都基于此观念:面对变化,欢迎变化。事实上,一些经典的软件工程 原则,例如最小化耦合,就是为了让程序更容易面对变化。不论你是否采用了极限编程方法,这个原则对你的程序开发都有重要意义。

http://www.amazon.com/gp/product/0321278658

124 Responses to “ 每个程序员都必须遵守的编程原则 ”

  1. suba suba说道:

    eiTCxN This is really interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post. Also, I ave shared your website in my social networks!

  2. Im having a little issue. I cant get my reader to pick up your feed, Im using yahoo reader by the way.

  3. tv show online说道:

    It as really a cool and helpful piece of info. I am glad that you shared this useful information with us. Please keep us up to date like this. Thanks for sharing.

  4. animal porn说道:

    Outstanding story there. What occurred after? Take care!

  5. coloring page说道:

    Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is excellent, let alone the content!. Thanks For Your article about sex.

  6. Smokers Lungs说道:

    pretty useful material, overall I feel this is worth a bookmark, thanks

  7. when we do our house renovation, we normally search for new home styles and designs on-line for some wonderful tips.

  8. that as what this web site is providing.

  9. Oluwadamilare说道:

    Your style is really unique compared to other folks I have read stuff from. Many thanks for posting when you ave got the opportunity, Guess I all just bookmark this page.

  10. website说道:

    Your style is unique in comparison to other folks I ave read stuff from. Thank you for posting when you ave got the opportunity, Guess I will just book mark this blog.

  11. pretty handy material, overall I feel this is worth a bookmark, thanks

  12. Your style is really unique in comparison to other people I ave read stuff from. Thanks for posting when you ave got the opportunity, Guess I all just bookmark this web site.

  13. Im grateful for the blog article.Much thanks again.

  14. PRADA OUTLET ONLINE ??????30????????????????5??????????????? | ????????

  15. woking cab说道:

    Your weblog is wonderful dude i love to visit it everyday. very good layout and content material ,

  16. Pretty nice post. I just stumbled upon your blog and wanted to say that I ave truly enjoyed browsing your blog posts. In any case I all be subscribing

  17. You can definitely see your enthusiasm in the paintings you write. The sector hopes for more passionate writers like you who aren at afraid to mention how they believe. At all times follow your heart.

  18. Maybe in the future it all do even better in those areas, but for now it as a fantastic way to organize and listen to your music and videos,

  19. microblog说道:

    This is one awesome article.Really thank you!

  20. more information说道:

    I truly appreciate this post. I?ve been looking all over for this! Thank goodness I found it on Bing. You ave made my day! Thx again

  21. I was recommended this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are amazing! Thanks!

  22. Citizenship说道:

    You definitely know how to bring an issue to light and make it important. I cant believe youre not more popular because you definitely have the gift.

  23. Nwokolo Collins说道:

    Really appreciate you sharing this blog post. Much obliged.

  24. Very neat blog article.Thanks Again. Awesome.

  25. her latest blog说道:

    You made some clear points there. I did a search on the issue and found most persons will agree with your blog.

  26. Nwokolo说道:

    Looking forward to reading more. Great article post.Really looking forward to read more. Cool.

  27. klm coupon code说道:

    I used to be able to find good info from your blog articles.

  28. It’а†s actually a nice and useful piece of info. I’а†m glad that you simply shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

  29. Such clever work and reporting! Keep up the superb works guys I ave incorporated you guys to my blogroll.

  30. It as hard to come by knowledgeable people for this subject, but you seem like you know what you are talking about! Thanks

  31. not positioning this submit higher! Come on over and talk over with my website.

  32. lien ke thanh ha说道:

    Some genuinely prize content on this web site , saved to my bookmarks.

  33. si quan ao说道:

    Really enjoyed this article post.Much thanks again. Really Great.

  34. I really liked your post.Really looking forward to read more. Much obliged.

  35. more info说道:

    Look advanced to far added agreeable from you! However,

  36. Top Sales Tools说道:

    I think this is a real great blog article.Thanks Again. Cool.

  37. Very good blog article.Much thanks again. Want more.

  38. information with us. Please stay us up to date like this.

  39. poker betting说道:

    Normally I do not read post on blogs, but I wish to say that this write-up very forced me to check out and do it! Your writing taste has been amazed me. Thanks, very great article.

  40. Kickboxing说道:

    Really appreciate you sharing this blog post.Really thank you! Great.

  41. You ought to be a part of a contest for one of the best websites on the net. I will recommend this web site!

  42. Really informative blog post.Really looking forward to read more. Awesome.

  43. IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžll create a hyperlink towards the internet page about my private weblog.

  44. Im thankful for the blog.Really thank you! Keep writing.

  45. play说道:

    I really liked your post.Really looking forward to read more. Keep writing.

  46. seo tips说道:

    I really liked your blog article.Thanks Again. Really Great.

  47. healthy说道:

    then again is just n?t yet very available,

  48. Thanks for the blog.Thanks Again. Really Great.

  49. Successful tips说道:

    Utterly pent written content, Really enjoyed looking at.

  50. Hosting说道:

    Im thankful for the post.Really thank you! Cool.

  51. baking equipment说道:

    Thank you ever so for you blog.Really thank you! Great.

  52. Dencover说道:

    There is noticeably a bundle to know about this. I feel you made some good points in features also.

  53. Normally I really do not study post on blogs, but I must say until this write-up really forced me to try and do thus! Your creating style continues to be amazed us. Thanks, very wonderful post.

  54. You made some good points there. I checked on the internet for more info about the issue and found most people will go along with your views on this web site.

  55. low fat diet说道:

    There is noticeably a bundle to know about this. I assume you made various nice points in features also.

  56. Tejido a crochet说道:

    wow, awesome blog article. Keep writing.

  57. You ave made some really good points there. I looked on the net for more information about the issue and found most individuals will go along with your views on this web site.

  58. wow, awesome post.Really looking forward to read more. Will read on

  59. try this说道:

    Thanks so much for the article post. Want more.

  60. value说道:

    tаАаб‚Т€Т“me now and finallаАааБТ“ got the braveаА аБТ–y

  61. Only a smiling visitor here to share the love (:, btw outstanding style and design.

  62. School admission说道:

    Nice Site , guys! Rewarding Articles aswell. Right into my social bookmarks

  63. School admission说道:

    Im obliged for the blog post.Much thanks again.

  64. ark ps4 server说道:

    Wow, wonderful blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is great, as well as the content!. Thanks For Your article about sex.

  65. You ave made some decent points there. I looked on the internet for additional information about the issue and found most people will go along with your views on this site.

  66. visit website说道:

    you have got an amazing blog here! would you wish to make some invite posts on my blog?

  67. My brother suggested I might like this blog. He was totally right. This post truly made my day. You can not imagine just how much time I had spent for this information! Thanks!

  68. I will bookmark your blog and check once more right here regularly.

  69. You are my inspiration, I own few blogs and rarely run out from brand . аАа’аАТ‚аЂТ˜Tis the most tender part of love, each other to forgive. by John Sheffield.

  70. tm objection说道:

    Some truly select blog posts on this internet site , bookmarked.

  71. Say, you got a nice blog article.Really looking forward to read more.

  72. Informative and precise Its difficult to find informative and precise information but here I noted

  73. Thanks again for the article.Thanks Again. Keep writing.

  74. Wow! This blog looks just like my old one! It as on a totally different subject but it has pretty much the same page layout and design. Superb choice of colors!

  75. I think you have mentioned some very interesting points, thank you for the post.

  76. share说道:

    yay google is my king assisted me to find this outstanding website !.

  77. This is one awesome article post.Really looking forward to read more. Cool.

  78. to read more说道:

    I went over this internet site and I believe you have a lot of fantastic information, saved to bookmarks (:.

  79. This is a topic that is near to my heart Thank you! Exactly where are your contact details though?

  80. Open Heaven说道:

    You made some really good points there. I checked on the net to learn more about the issue and found most people will go along with your views on this site.

  81. Well I truly liked reading it. This article provided by you is very effective for accurate planning.

  82. Looking forward to reading more. Great post.Really looking forward to read more. Fantastic.

  83. bed cover说道:

    Some genuinely prime articles on this website , saved to favorites.

  84. accident lawyer说道:

    There is obviously a lot to realize about this. I think you made some nice points in features also.

  85. Simply a smiling visitor here to share the love (:, btw great design and style. Treat the other man as faith gently it is all he has to believe with. by Athenus.

  86. Wonderful blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I ave been trying for a while but I never seem to get there! Cheers

  87. magnificent points altogether, you simply gained a brand new reader. What would you recommend in regards to your post that you made some days ago? Any positive?

  88. It is best to participate in a contest for among the finest blogs on the web. I all recommend this web site!

  89. Bac dan SKF说道:

    I really liked your blog article. Much obliged.

  90. Winners说道:

    Major thanks for the post.Really looking forward to read more. Keep writing.

  91. go to source说道:

    There is definately a great deal to know about this subject. I love all of the points you ave made.

  92. Wow, incredible blog format! How lengthy have you been blogging for? you make running a blog glance easy. The full glance of your site is fantastic, as smartly the content material!

  93. to learn more说道:

    Valuable information. Lucky me I found your website by accident, and I am shocked why this accident did not happened earlier! I bookmarked it.

  94. like this说道:

    Your style is so unique compared to other people I ave read stuff from. I appreciate you for posting when you have the opportunity, Guess I will just book mark this page.

  95. Regards for helping out, great information.

  96. you can check说道:

    This site really has all the information and facts I needed about this subject and didn at know who to ask.

  97. visit website说道:

    Wow! Thank you! I permanently wanted to write on my website something like that. Can I take a part of your post to my website?

  98. SANTOS JERSEY HOME ??????30????????????????5??????????????? | ????????

  99. Thanks-a-mundo for the blog article.Thanks Again. Much obliged.

  100. It as just permitting shoppers are aware that we are nonetheless open for company.

  101. This is one awesome article post.Really looking forward to read more. Really Cool.

  102. Major thanks for the article.Really thank you! Keep writing.

  103. Very good blog post.Really thank you! Want more.

  104. Louis Vuitton Handbags On Sale Louis Vuitton Handbags On Sale

  105. social说道:

    I think this is a real great blog article.Thanks Again. Really Cool.

  106. ndu说道:

    This very blog is without a doubt educating as well as informative. I have discovered helluva helpful stuff out of this amazing blog. I ad love to go back every once in a while. Cheers!

  107. click here说道:

    Very nice post. I just stumbled upon your blog and wanted to say that I ave really enjoyed browsing your blog posts. In any case I all be subscribing to your rss feed and I hope you write again soon!

  108. I truly appreciate this post. I ave been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thanks again!

  109. baked recipes说道:

    Usually I do not learn article on blogs, however I wish to say that this write-up very compelled me to take a look at and do so! Your writing style has been surprised me. Thanks, very great post.

  110. ads说道:

    I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You ave made my day! Thanks again!

  111. You produced some decent points there. I looked on the internet for just about any issue and discovered most of the people may perhaps go in conjunction with with your web page.

  112. Thank you ever so for you blog post.Thanks Again. Want more.

  113. read more说道:

    The information talked about within the report are a number of the very best offered

  114. Online Article Every so often in a while we choose blogs that we read. Listed above are the latest sites that we choose

  115. When someone writes an piece of writing he/she keeps the plan of a

  116. go说道:

    This is a topic that is near to my heart Best wishes!

  117. Really appreciate you sharing this article.Really looking forward to read more. Really Cool.

  118. tham trai san说道:

    I truly appreciate this article post.Much thanks again. Great.

  119. Its hard to find good help I am constantnly proclaiming that its hard to find quality help, but here is

  120. I needed to compose you one very small observation to be able to thank you once again for all the fantastic solutions you’ve discussed in this case. It was simply open-handed with people like you to provide extensively all most of us might have made available as an e-book to help with making some profit for themselves, and in particular since you could possibly have tried it in case you decided. These things likewise served to be the good way to be sure that someone else have similar eagerness just like my very own to figure out very much more in regard to this problem. I am certain there are numerous more fun sessions in the future for folks who discover your website.

  121. bon tam bang go说道:

    The hit musical Fela to be staged in Lagos

  122. official site说道:

    Really appreciate you sharing this blog post.Really looking forward to read more. Want more.

  123. more info说道:

    It as hard to come by educated people for this topic, but you sound like you know what you are talking about! Thanks

  124. It as difficult to find well-informed people for this topic, but you sound like you know what you are talking about! Thanks

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>