1 / 9

Indentation & Comments

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.

mihaly
Download Presentation

Indentation & Comments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Indentation& Comments

  2. 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.

  3. 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???

  4. 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

  5. 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 (west) 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.

  6. Comments describe what is happening in the program method main(){Jeroo Sally = new Jeroo();// this is a loop     while(!Sally.isWater(AHEAD)) {        Sally.hop();    }    Sally.turn(RIGHT);// this is a loop    while(!Sally.isWater(AHEAD)) {        Sally.hop();    }    Sally.turn(RIGHT);// another loop    while(!Sally.isWater(AHEAD)) {        Sally.hop();    }    Sally.turn(RIGHT);  // still looping     while(!Sally.isWater(AHEAD)) {        Sally.hop();    }    // a turn     Sally.turn(RIGHT);} NOT stating the obvious These are lousy comments!

  7. 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?

  8. 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 (west) 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);} This could be a method Repeated code makes a good method Or would this be a better choice for a method?

  9. The End The important point is to make a reasonable effort to communicate to your human readers as well as to the computer. Comments and indentation are the 2 tools for making a program readable

More Related