编程小贴士

给你的编程提供小点子


每个程序员都会犯的10个错误

1.面向编译器写代码,而不是面向用户

当人们使用编译器创建自己的app时,在把自己的想法诉诸于机器代码的过程中,常常会将那些可以使得编程更为简单却又冗长的语法遗忘于脑后。

无论你使用的是单字母的标识符还是更易于人脑理解的标识符,对于编译器而言,毫无区别。编译器不在乎你写的是否是优化表达式,也不在乎你是否用括号封装了子表达式。编译器要做的就是将这些人脑可读的代码,解析为抽象的语法树,并将这些树转换成机器代码,或某种中间语言。

那么,为什么不使用更可读或者语义更明显的标识符呢——而不要仅仅是I,J或x。老实说,现在我们用来等待编译器完成转换标识符的时间几乎是微不足道。但是,这么做却可以大大减少你和其他程序员用于阅读理解这些源代码所用的时间。

还有一个类似的观点是:或许你可能已经记住了相关的运算符优先级,于是省略了表达式中一些不必要的括号,但是却没有考虑到后面的程序员有可能会误读你的代码,并就它是如何工作的作出一些无效的假设。

我的想法是,假设大家都知道,乘法(或除法)优先于加法和减法。其他任何我放到表达式中的内容我都会用上括号,以确保能真正表达我的意思,其他人也能真正理解我的想法。

有研究表明,有的代码维护所需要的时间甚至超出其编写时间的五倍以上。所以将代码写得易于阅读和理解是非常有意义的。

2.函数方法过于庞大

有一个经验法则就是,我们写的程序不应该过于庞大。而且我们也可以发现,现在方法趋向于越来越小巧——有时候仅仅只是几行代码。

从本质上说,要想快速把握程序的目的和意义,只需要一定的代码就够了。长方法不但令人难以接受,而且往往最终趋向于支离破碎。

其原因也非常简单:长方法既难以理解,又难以维护,甚至还难以正常测试。

有一个相当不错的测量方法可以衡量你的代码的复杂程度,以及出现bug的概率—— 循环复杂度。

该方法由Thomas J. McCabe Sr于1976年开发。循环复杂度使用方便简单,能让你在匆忙之中尽可能地保证代码运行正常。只需要数一数代码中‘if’语句和循环的数量,再加1,就是该方法的CC值。

当然这只是对代码执行路径数量的粗略计数。不过,如果你的某个方法其循环复杂度值大于10,我建议你重写。

3.过早的优化

这一点非常简单。当我们在编写代码的时候,有时我们会自作聪明地对某些代码过于注重细节过于精益求精,虽然看上去这些“明智”的代码比原先写的那些 提高了速度,但是你忽略了一个事实,这些“明智”的代码往往是难以阅读难以理解的——而且真正节省的时间往往只有几毫秒。这就是所谓的过早的优化。

著名的计算机科学家Donald Knuth曾经说过,“过早的优化是一切罪恶的根源”。

换言之就是:我们的代码需要清晰、干净,然后再重点找出真正的瓶颈并对其进行优化。千万不要试图过早的优化。

4.使用全局变量

话说回来,有的编程语言是完全没有局部变量这个概念的,所以不得不使用全局变量。关于全局变量,虽然我们可以在子函数中使用它,但是却没办法声明这一变量只能在该函数中使用。尽管如此,全局变量依然非常受欢迎,因为我们只需声明一次,即可到处使用,太省时省力了有木有。

但是它的优点也是它的缺陷,这也是关于全局变量最糟糕的事情——我们没有办法控制它的改变,也没办法控制何时去访问变量。假设某个全局变量在调用到程序之前赋予了一个特定的值,但是很可能调用完了之后值就变了,而你却毫无察觉。

5.不进行评估

你的目标是写一个应用程序,你斗志昂扬,愈战愈勇。但是突然间,你发现了性能问题和内存不足的问题。

进一步的调查表明,尽管你的设计对于现在这样小型的用户数量、记录、条目运行良好,但是却不适合大规模的情况——Twitter就是例子。又或者它现在在你的8GB RAM和SSD的3GHz PC上运行顺畅,但一旦到普通的PC上,它会比乌龟爬还要慢吞吞。

所以,部分设计进程还是需要评估,需要一系列的封底计算。有多少用户需要同时处理多少个用户?需要处理多少记录?目标响应时间又是多少?等等。

尽量对这些类型的问题进行评估,这样就可以对应用程序中的一些技术问题做一些更进一步的决策,如不同的算法和缓存。不要什么乱七八糟的都纳入到开发中去——你还需要好好评估目标和目的。

6.大小差一错误(数组边界溢出)

这个错误基本上每一个程序员都犯过,通常在写循环的时候,由于循环变量的步长增加过多或过少,导致循环遍历元素的次数发生错误,产生数组溢出的异常。

这个错误会导致遍历数组元素时访问不存在的元素,或者遗漏应该遍历的元素。产生这个错误的原因就是你忘记了数组下标是从0开始还是从1开始了。

7.淹没异常

现在的编程语言大多使用异常系统作为错误报告技术,而不再是以往传统的传递和检查故障代码。现在的编程语言使用新的关键字来处理和捕获异常,其名称为throw、try、finally和catch等。

关于异常处理值得一提的是,它们的作用是展开堆栈,从嵌套程序自动返回,直到异常被捕获并处理。不再需要你检查错误条件,从而导致代码深陷错误测试的泥沼。

通过正确地运用异常处理,我们能够使得软件更为强大。比如说catch能让我们捕获异常,并根据异常类型执行某种行为。

关于异常处理,程序员犯的最大的错误有两种。第一种是程序员对于他们catch的异常了解得不够清楚具体。捕获过于笼统化的异常类型可能会导致你在不经意间处理掉一些最好能够保留的特定异常。而这样做,可能会导致这些异常被淹没,丢失。

第二个错误更为有害:程序员不想要任何异常离开自己的代码,因此捕获之后忽略了它们。这就是所谓的空catch块。他们可能是这样想的,只要throw某些类型的异常就可以了:于是名正言顺地忽略了这些异常。

而现实是,这可能会导致其他致命的运行时异常——如内存不足的异常,代码无效的异常等等,从而使得程序无法正常运行。因此,调整异常catch块时应尽可能的具体化。

8.纯文本格式存储密码

数据安全性是永远值得探讨的话题,其重要性是不言而喻的。在这里,我要郑重告诉你的是,千万不要将密码用纯文本格式保存。

密码的标准是,先存储经过加密后杂乱无章的原始密码,然后再输入通过相同加密方法后的杂乱的密码,看看它们是否匹配。

还不清楚这样做的害处,那么给你个提示:如果某个网站承诺,如果你忘记了原始密码,他们会给你发送电子邮件告诉你,那么远离这种网站。这可能会出现 巨大的安全问题。假设有一天,该网站会被黑的话,那么你所有的登录信息都会被泄漏出去,而你除了忍气吞声惶惶而不可终日却毫无办法。所以,千万不要接触这 类网站,同样的,也不要在你的app里用纯文本的格式存储密码或其他的“秘密”。

9.不验证用户输入

以前的程序是单用户的,于是我们对用户输入往往不以为然:毕竟,如果程序崩溃的话,只会影响到一个人的使用。我们的输入验证仅限于数值验证、日期检查,或其他类型的输入验证。

文本输入往往不会特别验证。不过后来出现了网页。于是,你的程序有了遍布世界的用户。而一些恶意用户则会通过输入数据到你的程序,以试图接管你的app和服务器。

新型的攻击大多是因为缺乏对用户输入的检查。其中最著名的是SQL注入,通过标记注入,不好的用户输入可能会引发XSS攻击(跨站脚本)。

这两种类型都依赖于用户提供包含了SQL或者HTML片段的文本,来作为正常表单输入的一部分。如果应用程序不验证用户输入,直接就拿来用,那么很可能就会执行篡改的SQL,或者产生一些被攻击的HTML/JavaScript。

这反过来可能会使得app崩溃,或被黑客接管。为了避免这些情况,所以我们应该时时验证或消除用户输入。

 

10.不与时俱进

上述这些我总结的内容或许并不新鲜——你可能已经在其他的书籍或网页上涉猎过。但是随着时代的发展,会有越来越多的新的设计和编程技术面世。

而你如果还抱着一些陈旧的逐渐在被淘汰的技术不放,不愿意学习和了解新的编程方法和技术——那么你终将会被拍死在沙滩上。对于程序员,学习是永恒的课题。例如TDD和BDD,SLAP和SOLID方法,以及各种敏捷技术,都是我们应该学习的技术。

我们应该时刻保持对最新的编程艺术和实践的同步。

112 Responses to “ 每个程序员都会犯的10个错误 ”

  1. i love pron说道:

    2s7pRt Im grateful for the blog.Really looking forward to read more. Awesome.

  2. suba me说道:

    fptvzz I reckon something really special in this internet site.

  3. This site was how do you say it? Relevant!! Finally I ave found something which helped me. Kudos!

  4. Tattoo Artists说道:

    Just what I was searching for, appreciate it for putting up.

  5. Mantra Yoga说道:

    Thank you for some other magnificent post. Where else may anybody get that type of info in such an ideal method of writing? I have a presentation subsequent week, and I am on the search for such info.

  6. Looking forward to reading more. Great post.Really looking forward to read more. Great.

  7. girl drawing说道:

    Rising prices will drive housing sales for years to come

  8. mca bbb说道:

    Simply a smiling visitant here to share the love (:, btw great design and style.

  9. Im grateful for the post.Really thank you!

  10. Lung Health说道:

    I went over this web site and I conceive you have a lot of great information, saved to bookmarks (:.

  11. We stumbled over right here by a unique web page and believed I might check issues out. I like what I see so now i am following you. Look forward to locating out about your web page for a second time.

  12. dau tram be tho说道:

    Wow, great article.Really looking forward to read more. Cool.

  13. Click to read说道:

    Im grateful for the blog.Thanks Again. Want more.

  14. visit website说道:

    Keep up the great work , I read few content on this site and I believe that your site is very interesting and contains sets of fantastic information.

  15. more info说道:

    Say, you got a nice blog.Really thank you!

  16. go说道:

    Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is magnificent, as well as the content!

  17. Precisely what I was looking for, thanks for posting.

  18. directory说道:

    lose weight diet just beneath, are quite a few completely not related websites to ours, nevertheless, they are certainly worth going over

  19. web page说道:

    Not clear on what you have in mind, Laila. Can you give us some more information?

  20. Penn Clothing说道:

    Lately, I did not give a great deal of consideration to leaving comments on blog web page posts and have positioned remarks even considerably much less.

  21. Vu que nous voulons que votre bien, nous offrons le premier

  22. pretty helpful stuff, overall I feel this is well worth a bookmark, thanks

  23. visit website说道:

    Keep up the excellent piece of work, I read few blog posts on this website and I think that your site is really interesting and has bands of good info.

  24. Woking taxi说道:

    iаЂа›?Produkttest Berichte in vielen Kategorien jetzt lesen.

  25. Wow! This could be one particular of the most helpful blogs We ave ever arrive across on this subject. Actually Wonderful. I am also a specialist in this topic so I can understand your hard work.

  26. Major thankies for the post.Much thanks again. Awesome.

  27. Terrific post but I was wanting to know if you could write a litte more on this subject? I ad be very thankful if you could elaborate a little bit further. Kudos!

  28. microblog说道:

    Wow! Thank you! I permanently needed to write on my site something like that. Can I implement a part of your post to my site?

  29. This website was how do you say it? Relevant!! Finally I have found something which helped me. Thanks!

  30. It as simple, yet effective. A lot of times it as

  31. you could have a fantastic weblog here! would you wish to make some invite posts on my weblog?

  32. Green Card说道:

    The web is really useful for me and all people studying English. The quality of podcast is good and I hope there will be more useful and interesting lessons. Thanks!

  33. lanyard supplier说道:

    It is not my first time to pay a visit this web site, i am visiting this website dailly and take nice facts from here everyday.

  34. Nwokolo Collins说道:

    it is something to do with Lady gaga! Your own stuffs excellent.

  35. Thank you for your article post.Really thank you! Great.

  36. It as difficult to find well-informed people on this subject, but you sound like you know what you are talking about! Thanks

  37. Nwokolo说道:

    Simply a smiling visitant here to share the love (:, btw great design. Make the most of your regrets. To regret deeply is to live afresh. by Henry David Thoreau.

  38. Very excellent information can be found on site.

  39. Saved as a favorite, I love your web site!

  40. I truly appreciate this post. I have been looking all over for this! Thank God I found it on Google. You have made my day! Thanks again..

  41. Your means of telling all in this article is really nice, all be capable of without difficulty know it, Thanks a lot.

  42. MP3 Downloads说道:

    Thanks a lot for the blog.Really thank you! Great.

  43. Very good blog article.Really thank you! Keep writing.

  44. There is perceptibly a lot to know about this. I suppose you made certain good points in features also.

  45. this website说道:

    Nice blog here! Also your web site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol

  46. keyword planner说道:

    Well I found this on Digg, and I like it so I dugg it!

  47. Read this Article Good info and right to the point. I am not sure if this is really the best place to ask but do you people have any thoughts on where to employ some professional writers? Thank you :)

  48. You should not clone the girl as start looking specifically. You should contain the girl as design, yet with your own individual distinct distort.

  49. Tattoo Artists说道:

    Very nice write-up. I definitely appreciate this site. Thanks!

  50. sidekick说道:

    This is a excellent blog, would you be interested in doing an interview about just how you designed it? If so e-mail me!

  51. Vs model说道:

    Wow! This can be one particular of the most useful blogs We ave ever arrive across on this subject. Basically Fantastic. I am also a specialist in this topic therefore I can understand your effort.

  52. money说道:

    Very nice article and straight to the point. I am not sure if this is in fact the best place to ask but do you folks have any ideea where to get some professional writers? Thx

  53. You ave made some really good points there. I checked on the web to find out more about the issue and found most individuals will go along with your views on this web site.

  54. Hosting说道:

    Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Basically Wonderful. I am also a specialist in this topic so I can understand your hard work.

  55. Some really prime blog posts on this internet site , saved to favorites.

  56. Really appreciate you sharing this article.Really thank you! Much obliged.

  57. Crochet bolero说道:

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

  58. Crochet basico说道:

    Wow, great blog.Thanks Again. Will read on

  59. try this说道:

    Some truly good content about this web website, appreciate it for info. A conservative can be a man which sits and also thinks, mostly sits. by Woodrow Wilson.

  60. white shoes说道:

    I really loved what you had to say, and more than that,

  61. value说道:

    Precisely what I was looking for, appreciate it for posting.

  62. I was suggested this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are amazing! Thanks!

  63. I value the blog.Really looking forward to read more. Cool.

  64. hampton bay说道:

    I’а†ve recently started a blog, the information you offer on this site has helped me tremendously. Thank you for all of your time & work.

  65. Waec说道:

    You are my inspiration, I have few blogs and rarely run out from post . Analyzing humor is like dissecting a frog. Few people are interested and the frog dies of it. by E. B. White.

  66. Waec expo说道:

    Wow, superb blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is wonderful, as well as the content!

  67. Thank you for the auspicious writeup. It if truth be told

  68. It as hard to find experienced people for this topic, however, you seem like you know what you are talking about! Thanks

  69. Brandon Moss说道:

    Say, you got a nice article post.Much thanks again. Fantastic.

  70. This blog is no doubt educating as well as informative. I have picked helluva helpful things out of this source. I ad love to return again and again. Thanks a bunch!

  71. Thank you for your article. Will read on

  72. I was able to find good advice from your blog articles.

  73. This website was how do I say it? Relevant!! Finally I ave found something which helped me. Thanks!

  74. Many thanks for sharing this good article. Very interesting ideas! (as always, btw)

  75. Thanks-a-mundo for the article. Want more.

  76. Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is excellent, as well as the content!

  77. It as laborious to search out knowledgeable folks on this matter, but you sound like you understand what you are speaking about! Thanks

  78. info for a very lengthy time. Thank you and good luck.

  79. You can certainly see your skills in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always follow your heart.

  80. tututorials说道:

    Right now it sounds like Expression Engine is the top blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

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

  82. sleekness as well as classiness. An elegant ladies watch that

  83. We all talk a little about what you should talk about when is shows correspondence to because Maybe this has much more than one meaning.

  84. Spot on with this write-up, I truly feel this web site needs a lot more attention. I all probably be back again to read more, thanks for the info!

  85. lawyer说道:

    Really informative blog.Thanks Again. Really Great.

  86. into his role as head coach of the Pittsburgh click here to find out more did.

  87. This very blog is without a doubt awesome and also amusing. I have discovered a bunch of interesting things out of it. I ad love to return every once in a while. Cheers!

  88. Very informative blog post.Really looking forward to read more. Keep writing.

  89. Very nice post. I just stumbled upon your blog and wanted to say that I ave truly enjoyed browsing your blog posts. After all I all be subscribing to your feed and I hope you write again soon!

  90. know more说道:

    Is not it amazing whenever you discover a fantastic article? My personal web browsings seem full.. thanks. Respect the admission you furnished.. Extremely valuable perception, thanks for blogging..

  91. click for more说道:

    Very good article! We are linking to this particularly great article on our site. Keep up the good writing.

  92. whatsapp说道:

    This unique blog is obviously interesting as well as informative. I have chosen a lot of helpful things out of this source. I ad love to go back every once in a while. Thanks a bunch!

  93. Usually I don at learn article on blogs, but I would like to say that this write-up very forced me to check out and do so! Your writing taste has been amazed me. Thank you, quite nice post.

  94. Some really excellent posts on this site, regards for contribution.

  95. There is certainly a lot to know about this issue. I like all of the points you have made.

  96. Wow! I cant believe I have found your weblog. Very helpful information.

  97. Wow! Thank you! I permanently wanted to write on my site something like that. Can I include a part of your post to my blog?

  98. particularly wonderful read!! I definitely appreciated every little

  99. social说道:

    What as up to every one, the contents existing at this web site are in fact awesome for people experience, well, keep up the good work fellows.

  100. you can check说道:

    It as hard to find well-informed people on this subject, however, you sound like you know what you are talking about! Thanks

  101. It as genuinely very complicated in this active life to listen news on TV, so I simply use world wide web for that purpose, and obtain the latest news.

  102. app说道:

    I recommend to you to visit a site on which there are many articles on this question.

  103. Thanks for sharing, this is a fantastic article.Really thank you! Want more.

  104. for details说道:

    Way cool! Some very valid points! I appreciate you penning this post plus the rest of the site is also really good.

  105. caratulas CD说道:

    Wow, fantastic blog layout! How long have you been blogging for?

  106. Your style is unique in comparison to other people I have read stuff from. Many thanks for posting when you have the opportunity, Guess I all just book mark this blog.

  107. buat pb说道:

    There is perceptibly a lot to identify about this. I feel you made certain nice points in features also.

  108. This is one awesome post.Thanks Again. Keep writing.

  109. Just Browsing While I was surfing yesterday I saw a great article concerning

  110. thanh duoc说道:

    You have mentioned very interesting points ! ps nice site. I just wish we knew a little less about his urethra and a little more about his arms sales to Iran. by Andrew A. Rooney.

  111. go there说道:

    You ave done a formidable task and our whole group shall be grateful to you.

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>