1 / 11

Indentation & Comments VB

Indentation & Comments VB. 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. sub main() Dim Sally as Jeroo = new Jeroo()

till
Download Presentation

Indentation & Comments VB

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& CommentsVB

  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 sub main() Dim Sally as Jeroo = new Jeroo() While NOT Sally.isWater(AHEAD) Sally.hop() End While Sally.turn(RIGHT) While NOT Sally.isWater(AHEAD) Sally.hop() End While Sally.turn(RIGHT) While NOT Sally.isWater(AHEAD) Sally.hop() End While Sally.turn(RIGHT) While NOT Sally.isWater(AHEAD) Sally.hop() End While Sally.turn(RIGHT) end sub What does it do??? It is very hard to read!

  4. using indentation and separate lines sub main()    Dim Sally as Jeroo = new Jeroo()     While(NOT Sally.isWater(AHEAD))         Sally.hop() End While     Sally.turn(RIGHT)    While(NOT Sally.isWater(AHEAD))         Sally.hop() End While     Sally.turn(RIGHT)    While(NOT Sally.isWater(AHEAD))         Sally.hop() End While     Sally.turn(RIGHT)    While(NOT Sally.isWater(AHEAD))         Sally.hop() End While     Sally.turn(RIGHT) end sub the structure is easier to see

  5. add in comments now, anyone could look at the program and know exactly what it does. sub main()    ' start in the NorthWest Corner at 0,0 Dim Sally as Jeroo = new Jeroo()   ' Go along the top (north) edge of the island    While(NOT Sally.isWater(AHEAD))         Sally.hop()    End While     Sally.turn(RIGHT)    ' Go along the right (east) edge of the island    While(NOT Sally.isWater(AHEAD))         Sally.hop()    End While     Sally.turn(RIGHT)  ' Go along the bottom (south) edge of the island    While(NOT Sally.isWater(AHEAD))         Sally.hop()    End While     Sally.turn(RIGHT)    ' Go along the left (east) edge of the island    While(NOT Sally.isWater(AHEAD))         Sally.hop()    End While     ' Note that this last turn is needed to ensure that Sally is facing east    ' again, as required in the problem statement.    Sally.turn(RIGHT) Original problem: Walk all the way around the perimeter of a square island with no obstacles starting in the northwest corner facing east and returning to the original position facing east.

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

  7. look for duplication sub main()    Dim Sally as Jeroo = new Jeroo()     While(NOT Sally.isWater(AHEAD))         Sally.hop() End While     Sally.turn(RIGHT)    While(NOT Sally.isWater(AHEAD))         Sally.hop() End While     Sally.turn(RIGHT)    While(NOT Sally.isWater(AHEAD))         Sally.hop() End While     Sally.turn(RIGHT)    While(NOT Sally.isWater(AHEAD))         Sally.hop() End While     Sally.turn(RIGHT) end sub How about a method named hopToWater()?

  8. look for more duplication sub main()    Dim Sally as Jeroo = new Jeroo()     Sally.hopToWater()     Sally.turn(RIGHT)    Sally.hopToWater() Sally.turn(RIGHT)    Sally.hopToWater()     Sally.turn(RIGHT)    Sally.hopToWater()     Sally.turn(RIGHT) end sub How about another method named TurnAndHopToWater() that does a right turn and then calls HopToWater()?

  9. finished detailed Main program ‘Sally walks all the way around the perimeter of a square island with no obstacles starting in the northwest corner facing east and returning to the original position facing east. Sub Main()‘ start in the NorthWest Corner at 0,0 Dim Sally as Jeroo = new Jeroo()   ' Go along the top (north) edge of the island Sally.hopToWater()  ' Go along the right (east) edge of the island Sally.turnAndHopToWater() ' Go along the bottom (south) edge of the island Sally.turnAndHopToWater() ' Go along the left (east) edge of the island Sally.turnAndHopToWater() ' Note that this last turn is needed to ensure that Sally is facing east    ' again, as required in the problem statement.Sally.turn(RIGHT) End Sub Efficient, readable, and easy to know what it does even if you go back to it a year later!

  10. a different Main program ‘Sally walks all the way around the perimeter of a square island with no obstacles starting in the northwest corner facing east and returning to the original position facing east. Sub Main()‘ start in the NorthWest Corner at 0,0 Dim Sally as Jeroo = new Jeroo() Sally.walkAroundIsland() End Sub The details can go in the methods.

  11. The End The important point is to make some reasonable effort to communicate to your human readers as well as to the computer.

More Related