LTP:Crafting quality code

来源:互联网 发布:炉石淘宝买卡包安全吗 编辑:程序博客网 时间:2024/04/28 11:23

注:本文出自Coursera上的一门名叫“打造高质量代码”的网络课程的授课老师,是多伦多大学的CS教授,以下是他对构建代码的建议:


Paul Gries, the Senior Lecturer in the Department of Computer Science at the University of Toronto suggested some good habits in crafting quality code:

  • Plan before you type. One of my favourite quotes, from Greg Wilson (although I don't know if it's his), is "Sometimes and hour of thought can save you a week of hard work."

  • Make sure you understand the problem you're trying to solve before you try to solve it.

  • If you don't know how to write a function to do a task, do it by hand and think hard about what you're doing.

  • Test your code. You don't have to write all the tests first, but write at least a couple tests before you write a function. You do this in order to make sure you understand what the function is supposed to do.

  • If some code is broken, it's faster to figure out exactly what went wrong than it is to try things randomly until it works.

  • Top-down design has you write high-level sentences describing what you want to do. These usually make excellent comments.

  • Once you figure out something --- anything! --- make a note so that you don't have to figure it out again. Comments are usually the easiest way to do this.

  • The structure of your data is vital. Document it.

Bigger issues:

  • Learn about version control. git is the current crowd favourite:

http://git-scm.com/

Watch video tutorials, try out git, learn the ideas behind every version control system. Every company uses version control of some kind; options include git, perforce, subversion, and many more. (We teach version control at the beginning of second year here at the UofT, and it's beyong the scope of LTP2, but it's really, really important, especially when working with other programmers.)

  • If you know c-based programming languages (C, C++, Python, Ruby, Java, most others), take the time to learn a weird one. Haskell, Scheme, and Lisp are all excellent choices. Gregor Kiczales from UBC is running a Coursera course on Scheme, and I'm looking forward to following it myself:
原创粉丝点击