Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code...

6
Program Style Chapter 22 IB103 Week 12 (part 2)

Transcript of Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code...

Page 1: Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.

Program Style

Chapter 22

IB103 Week 12 (part 2)

Page 2: Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.

Modularity: the ability to reuse code

Encapsulation: hide data access directly but may use methods (the outside world does not “see” data)Names: use good self documenting names for variables, methods, classes, packagesIndentation not required but help readability – also emphasizes structure, not magic can lead to false assumptions (use care and be consistent)

Page 3: Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.

Program Style continued

Blank lines – separate methods improve readabilityNew pages (maybe each new class) Optional

Comments // /* … */ /** … */ used for documentation JDK

Page 4: Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.

More Style continued

Constants Final pi = 3.14159; Star = “*”; only have to change one line

in program in constant ever changes

Method Size no rule but convention about 40 lines (seat of pants rule too long if you start squirming in your seat after trying to follow logic of method)

Page 5: Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.

Classes : one to two pages (no rule convention)Methods: names self documentingField order: variables public then privateConsistency Be consistent (See James Gosling’s libraries)Nested ifs – avoid if complex and difficult to follow logicNested loops – keep simple, keep logic clear to reader – just because a program runs does not make it good but if it is easy to follow it probably is a good program (or can be easily fixed)

Page 6: Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.

Complexity: better to simplify so readers can understandDocumentation: Program specifications Source code – with comments Design information (hierarchy

diagrams) Test schedule – results if available Modification history User manual