This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Mehran Sahami Handout #17 CS 106A October 12, 2007 Coding Style Most of this handout was written by Nick Parlante and Eric Roberts, then edited for our own clandestine purposes. 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; its 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, its hard to clean it up. Its 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 inall the work and little of the benefit! Do it the right way from the start, and youll 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 that 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! 2...
View Full Document
This note was uploaded on 02/15/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.
- Fall '08