希望高手翻译啊
Steve Cooper
CS 106A Handout #17 January 24, 2011 Coding Style
When writing a paper, you can have well-crafted, correctly spelled sentences and create ―A‖ work. Or you can hack out the text in a hurry. It will not look as good, but it can convey your thoughts and get the job done; it’s worth maybe a ―B‖ or a ―C‖. Computer code is not like that. Code that is messy tends to have all sorts of bugs and other problems. Messy code attracts problems like a half-eaten lollipop attracts lint (and that's never pleasant). The problems and bugs in poorly written code tend to compound each other and pile up, so the code ends up being nearly worthless. It has bugs. Nobody knows how to fix them or add features without creating more bugs. Once code is in that state, it is hard to escape. In a sense, code tends to be more either ―A‖, or ―D‖ or ―F‖. Therefore, it is best to write code that is clean to start, and keep it clean as you add features. This is one of the lessons in CS for successfully building large projects. For that reason CS 106A emphasizes the habits of clean, well-engineered code right from the start, building the right habits for the future.
One reason to write clean, well-structured code is that it works better and takes less time in the end. The other reason to write clean code is that it is just more satisfying to do a good job on something. Clear, elegant code feels right, just like any other engineering or artistic creation.
The messy code trap
It is a basic fact of computer science that poorly designed, messy code is harder to build and debug than clean code. It is tempting to just type out a quick solution as it occurs to you to get started. It is better to take a little more time at the start to build the clean version, creating fewer headaches in the debugging phase. Once code gets messy, it’s hard to clean it up. It’s easier to start clean, and then keep it clean with each addition. The worst possible strategy is to build the messy version, do your debugging on that, and then clean it up before turning it in—all the work and little of the benefit! Do it the right way from the start, and you’ll be happier.
Decomposition
Decomposition does not mean taking a completed program and then breaking up large methods into smaller ones merely to appease your section leader. Decomposition is the most valuable tool you have for tackling complex problems. It is much easier to design, implement, and debug small functional units in isolation than to attempt to do so with a much larger chunk of code. Remember that writing a program first and decomposing after the fact is not only difficult, but prone to producing poor results. You should decompose the problem, and write the program from that already decomposed framework. In other words, you are aiming to decompose problems, not programs!
斯坦福大学开放课程讲义04-honor-code_理学_高等教育_教育专区。希望对大家有用...Discuss ideas together, but do the coding on your own. The prohibition ...
【YYeTs人人影视】《斯坦福大学开放课程:编程方法》讲义#17——编程风格-168655361 斯坦福大学开放课程:编程方法斯坦福大学开放课程:编程方法隐藏>> 分享到: X 分享...
《斯坦福大学开放课程:编程方法》讲义#1_工学_高等教育_教育专区。Mehran Sahami CS 106A CS 106A — General Information Handout #1 September 24, 2007 ...
《斯坦福大学开放课程:编程方法》讲义#29_工学_高等教育_教育专区 暂无评价0人阅读0次下载举报文档 《斯坦福大学开放课程:编程方法》讲义#29_工学_高等教育_...
《斯坦福大学开放课程:编程方法》讲义#26_电脑基础知识_IT/计算机_专业资料。...Such a coding scheme is called a letter-substitution cipher. The key in ...
斯坦福大学开放课程讲义09-karel-examples_工学_高等教育_教育专区。希望对大家有用 可惜没中文啊Steve Cooper CS 106A Handout #9 January 7, 2011 Example ...
《斯坦福大学开放课程:编程方法》讲义#38-2_理学_高等教育_教育专区 暂无评价0人阅读0次下载举报文档 《斯坦福大学开放课程:编程方法》讲义#38-2_理学_高等...
《斯坦福大学开放课程:... 8页 免费《斯坦福大学开...When we print out the results we’ll see: 8 17 18 Class Variables As ...
《斯坦福大学开放课程:编程方法》讲义#22_工学_高等教育_教育专区。Mehran Sahami CS 106A Graphics and Events Examples Handout #22 October 17, 2007 Based ...
【YYeTs人人影视】《斯坦福大学开放课程:编程方法》讲义#14——方法-128557637_IT/计算机_专业资料。斯坦福大学开放课程:编程方法今日推荐 ...
热门文档