110 likes | 302 Views
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()
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 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!
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
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.
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?
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()?
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()?
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!
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.
The End The important point is to make some reasonable effort to communicate to your human readers as well as to the computer.