2017年7月20日 星期四

CS106A - Programming Methodology - Lecture 3

Common errors

infinite loop
while (frontIsClear()) {
      turnLeft();
}

off-by-one bug (OBOB)
while (frontIsClear()) {
      putBeeper();
      move();
}
//this code would left one blank corner because "before" the beeper is dropped, Karel's front is no longer clear. To debug, you need to add another "putBeeper();" after the while loop.





Comments

Program is to be understandable by people, not just by machines.

/* blahblahblah
 * blahblahblah
 * blahblahblah
 */

OR

//blahblahblahblahblahblah 
(hit the enter and end)

you put in comments to clarify things in the program, which are not obvious

precondition and postcondition - a good habit to do Karel program so that others could easily understand and easier to debug for yourself.

high level description and break down into detail descriptions

(And I learnt it when I was working on the homework....)

primitives
"stepwise refinement"
"decomposition"
"top-down design"
"bottom-up design"

Do not write codes like "Do Your Thing" that no one understands.
not just "work"

"camel case" like the humps on camel's back

The order of the private voids are not important.

"decomposition"

1. every "one" of your methods should basically solve "one" problem

the problem could be high level but should better be for only one problem

2. methods should be 1-15 lines long

3. good names: in good English and explain what the methods do

4. "with comments" for people to read easily


clean up Karel

break down into several steps
and create the methods

沒有留言:

張貼留言