Presentation is loading. Please wait.

Presentation is loading. Please wait.

“Discipline is the refining fire by which talent becomes ability.” – Roy L. Smith Thought for the Day.

Similar presentations


Presentation on theme: "“Discipline is the refining fire by which talent becomes ability.” – Roy L. Smith Thought for the Day."— Presentation transcript:

1 “Discipline is the refining fire by which talent becomes ability.” – Roy L. Smith Thought for the Day

2 Criteria for judging software quality: –Correctness –Ease-of-use –Generality –Efficiency –Portability –Clarity –Ease of coding and testing –Ease of modification Writing Good Programs

3 Quality Criteria (cont.) Generality and Efficiency –Often in tension –Try to solve a class of problems, not just one specific problem Portability –Keep to standard features

4 Quality Criteria (cont.) Clarity –How easily can the program be understood? Well-structured program Comments Good names for variables, methods and classes –Follow conventions for identifier names Simplicity of algorithm Good layout –Indentation, etc.

5 Quality Criteria (cont.) Ease of Coding and Testing –Well structured (modular) programs –Leave test code in program (commented out if necessary) Ease of Modification –Well written program –Modular design

6 Techniques for Improving Program Quality Preconditions and Postconditions Assertions Automatic system documentation

7 Preconditions and Postconditions We can characterise a method by the program’s state before and after the method is executed

8 “Before and After”

9 Preconditions and Postconditions Preconditions –Assumptions about the “before” state Postconditions –Statements about the “after” state Often expressed as comments

10 Example A method to calculate a square root public double squareRoot (double x) // PRE: x >= 0 // POST: squareRoot returns an approximation // to the square root of x {... } // squareRoot

11 Another Example Some methods have no preconditions A random number method: public double random () // PRE: None // POST: random returns a pseudo-random // number r in the range 0 <= r < 1 {... } // random

12 Checking Preconditions Can throw exceptions public double squareRoot (double x) // PRE: x >= 0 // POST:... { if (x < 0) throw new IllegalArgumentException("x<0");...

13 Assertions Similar to preconditions and postconditions Introduce the idea of checkpoints in the program –Describe the state of the program at certain points

14 Example A program to play a card game class PlayingCard {... } PlayingCard deck[] = new PlayingCard[52];... shuffleCards(deck); // ASSERT: deck contains 52 cards in // random order

15 Assertions Some programming languages provide mechanisms to check simple assertions Java does (since JDK 1.4) –assert statement –Two forms: assert boolean_expression; assert boolean_expression : expression;

16 Using Assertions in Java... skipSpaces(); ch = (char)System.in.read(); assert ch != ' '; java.lang.AssertionError at MyApp.main(MyApp.java:12) Exception in thread "main"

17 Assertions in Java Assertion checking is disabled by default –Allows checking to be done during development –When program is shipped to customer, checking is disabled for efficiency Full details in Sun documentation

18 Using Assertions Some conditions are too difficult to express as simple boolean expressions –Use assertion comments shuffleCards(deck); // ASSERT: deck contains 52 cards in // random order

19 Assertions and Preconditions and Postconditions Related concepts: –Assertions: general checkpoints –Pre- and postconditions: specific checkpoints (at start and end of methods)

20 Assertions and Preconditions Assertions can be used to check pre- conditions –better to use exceptions public double squareRoot (double x) // PRE: x >= 0 // POST:... { assert x >= 0;...

21 Benefits of Using Assertions, Preconditions and Postconditions Forces the programmer to consider the state of the program at key points Helps document the program –Assumptions and “guarantees” May provide run-time checking

22


Download ppt "“Discipline is the refining fire by which talent becomes ability.” – Roy L. Smith Thought for the Day."

Similar presentations


Ads by Google