70 likes | 87 Views
Indentation & Comments. Overview. Indentation isn't important to the correctness of your programs (the computer totally ignores it), but it can make a big difference to the readability of your programs. Version without indentation.
E N D
Overview • Indentation isn't important to the correctness of your programs (the computer totally ignores it), but it can make a big difference to the readability of your programs.
Version without indentation method main(){Jeroo Sally = new Jeroo();while(!Sally.isWater(AHEAD)) {Sally.hop();}Sally.turn(RIGHT);while(!Sally.isWater(AHEAD)) {Sally.hop();}Sally.turn(RIGHT);while(!Sally.isWater(AHEAD)) {Sally.hop();}Sally.turn(RIGHT);while(!Sally.isWater(AHEAD)) {Sally.hop();}Sally.turn(RIGHT);} What does it do???
with indentation method main(){Jeroo Sally = new Jeroo(); while(!Sally.isWater(AHEAD)) { Sally.hop(); } Sally.turn(RIGHT); while(!Sally.isWater(AHEAD)) { Sally.hop(); } Sally.turn(RIGHT); while(!Sally.isWater(AHEAD)) { Sally.hop(); } Sally.turn(RIGHT); while(!Sally.isWater(AHEAD)) { Sally.hop(); } Sally.turn(RIGHT);} the meaning is easier to see
add in comments method main(){Jeroo Sally = new Jeroo(); // Go along the top (north) edge of the island while(!Sally.isWater(AHEAD)) { Sally.hop(); } Sally.turn(RIGHT); // Go along the right (east) edge of the island while(!Sally.isWater(AHEAD)) { Sally.hop(); } Sally.turn(RIGHT); // Go along the bottom (south) edge of the island while(!Sally.isWater(AHEAD)) { Sally.hop(); } Sally.turn(RIGHT); // Go along the left (east) edge of the island while(!Sally.isWater(AHEAD)) { Sally.hop(); } // Note that this last turn is needed to ensure that Sally is facing east // again, as required in the problem statement. Sally.turn(RIGHT);} now, anyone could look at the program and know exactly what it does.
refining further… • of course, there are areas of repeated code… • perfect candidates for creating methods • how would you rewrite it to be more efficient using methods? • can you still create a program as readable as the example given here?
The End The important point is to make some reasonable effort to communicate to your human readers as well as to the computer.