school of computer science & information technology g6dicp - lecture 9 software development...

22
chool of Computer Science & Information Technology G6DICP - Lecture 9 G6DICP - Lecture 9 Software Development Techniques Software Development Techniques

Upload: alicia-jacobs

Post on 01-Jan-2016

220 views

Category:

Documents


3 download

TRANSCRIPT

School of Computer Science & Information Technology

School of Computer Science & Information Technology

G6DICP - Lecture 9G6DICP - Lecture 9

Software Development TechniquesSoftware Development Techniques

2

Software DesignSoftware Design

There is a difference between program design and coding.There is a difference between program design and coding. Effective programming requires Effective programming requires both.both. Program design should be a formalised process:Program design should be a formalised process:

Describe the problem in terms of the actions that the computer needs to Describe the problem in terms of the actions that the computer needs to perform.perform.

Test the description.Test the description. Refine the description to provide more detail.Refine the description to provide more detail. Test the refinements... etc.Test the refinements... etc. Code the program.Code the program.

Coding without design should always be avoided.Coding without design should always be avoided.

3

Top-Down DesignTop-Down Design

The most widely favoured method of program The most widely favoured method of program development.development.

The task is described as a problem.The task is described as a problem. This problem is divided into sub-problems.This problem is divided into sub-problems. Sub-problems are further divided, until the Sub-problems are further divided, until the

stages are easily represented by program code.stages are easily represented by program code.

4

Program DescriptionProgram Description

Sate the problem in hand.Sate the problem in hand. This statement must be clear and This statement must be clear and absolutelyabsolutely

unambiguous.unambiguous. Clarification of the problem statement frequently Clarification of the problem statement frequently

requires iteration with the “client”.requires iteration with the “client”. The problem statement can frequently be broken The problem statement can frequently be broken

down into partly or completely autonomous down into partly or completely autonomous components.components.

Functional Specification.Functional Specification. For any large programming project this For any large programming project this mustmust be a be a

written document that is agreed by all parties (eg written document that is agreed by all parties (eg “programmers”, “clients” etc).“programmers”, “clients” etc).

5

FeasibilityFeasibility

The functional specification can be tested for The functional specification can be tested for feasibility.feasibility.

There are several possible problem areas:There are several possible problem areas: Completely infeasible problems (ie problems that Completely infeasible problems (ie problems that

have no solution - eg division by 0, calculating the have no solution - eg division by 0, calculating the square root of negative numbers etc).square root of negative numbers etc).

Practically infeasible problemsPractically infeasible problems Infeasible in terms of hardwareInfeasible in terms of hardware Infeasible in terms of timeInfeasible in terms of time Infeasible in terms of costInfeasible in terms of cost

6

AlgorithmsAlgorithms

An algorithm is the solution for a problem.An algorithm is the solution for a problem. Algorithms must be:Algorithms must be:

FiniteFiniteThere must be a There must be a measurable measurable point at which the point at which the problem can be said to be solved.problem can be said to be solved.

OrderedOrderedThe algorithm must consist of a series of steps.The algorithm must consist of a series of steps.

UnambiguousUnambiguousEach step must follow on from a previous step - if Each step must follow on from a previous step - if choices are made they must be based upon conditions choices are made they must be based upon conditions determined earlier in the sequence.determined earlier in the sequence.

7

Successive RefinementSuccessive Refinement

First - develop an First - develop an initial solutioninitial solution This does not need to go into much detailThis does not need to go into much detail It does not need to be functional program codeIt does not need to be functional program code It It mustmust describe the solution correctly describe the solution correctly

Secondly - refine this solutionSecondly - refine this solution Each stage should be considered as a problem in its own right - a solution Each stage should be considered as a problem in its own right - a solution

developed and described.developed and described. The description must again describe the solution correctlyThe description must again describe the solution correctly

Continue until the problem is completely solvedContinue until the problem is completely solved ““Successive Refinements”Successive Refinements”

PseudocodePseudocode

8

PseudocodePseudocode A detailed description of a program A detailed description of a program

Ignore the syntactic rigours of a formal languageIgnore the syntactic rigours of a formal language

Provide sufficient detail so that each part of the Provide sufficient detail so that each part of the description will have a correspondence with real description will have a correspondence with real source codesource code

For OO programming, try to design your For OO programming, try to design your pseudocode in an OO manner (ie use objects and pseudocode in an OO manner (ie use objects and methods)methods)

9

Example - 1. The ProblemExample - 1. The Problem

ProblemProblem““Write a program to find and display the total of nine Write a program to find and display the total of nine

integers input by the user. The user should be integers input by the user. The user should be prompted for input, and the integers should be prompted for input, and the integers should be entered in groups of 3 - with the subtotal displayed entered in groups of 3 - with the subtotal displayed after the entry of each of these groups”after the entry of each of these groups”

Modified from Meyers, 1992.Modified from Meyers, 1992.

Example outputExample outputEnter the first 3 numbers : 3 5 7Enter the first 3 numbers : 3 5 7

Subtotal : 15Subtotal : 15

Enter the second 3 numbers : 1 3 5Enter the second 3 numbers : 1 3 5

Subtotal : 24Subtotal : 24

Enter the last 3 numbers : 5 4 1Enter the last 3 numbers : 5 4 1

Total : 34Total : 34

10

Example - 2. Successive RefinementExample - 2. Successive Refinement Initial DescriptionInitial Description

Prompt user, read 3 input integers and display their sumPrompt user, read 3 input integers and display their sum Prompt user, read 3 input integers and display the sum of all 6Prompt user, read 3 input integers and display the sum of all 6 Prompt user, read in 3 input integers and display the sum of all 9Prompt user, read in 3 input integers and display the sum of all 9

Refinement of first actionRefinement of first action Refinement of second actionRefinement of second action Refinement of third actionRefinement of third action

11

Example - 2. Successive RefinementExample - 2. Successive Refinement Initial DescriptionInitial Description

Prompt user, read 3 input integers and display their sumPrompt user, read 3 input integers and display their sum Prompt user, read 3 input integers and display the sum of all 6Prompt user, read 3 input integers and display the sum of all 6 Prompt user, read in 3 input integers and display the sum of all 9Prompt user, read in 3 input integers and display the sum of all 9

Refinement of first actionRefinement of first action Display prompt for userDisplay prompt for user Read in values for Int1, Int2 and Int3Read in values for Int1, Int2 and Int3 Sum = Int1 + Int2 + Int3Sum = Int1 + Int2 + Int3 Display the value of SumDisplay the value of Sum

Refinement of second actionRefinement of second action Refinement of third actionRefinement of third action

12

Example - 2. Successive RefinementExample - 2. Successive Refinement Initial DescriptionInitial Description

Prompt user, read 3 input integers and display their sumPrompt user, read 3 input integers and display their sum Prompt user, read 3 input integers and display the sum of all 6Prompt user, read 3 input integers and display the sum of all 6 Prompt user, read in 3 input integers and display the sum of all 9Prompt user, read in 3 input integers and display the sum of all 9

Refinement of first actionRefinement of first action Refinement of second actionRefinement of second action

Display prompt for userDisplay prompt for user Read in values for Int1, Int2 and Int3Read in values for Int1, Int2 and Int3 Sum = Sum + Int1 + Int2 + Int3Sum = Sum + Int1 + Int2 + Int3 Display the value of SumDisplay the value of Sum

Refinement of third actionRefinement of third action

13

Example - 2. Successive RefinementExample - 2. Successive Refinement Initial DescriptionInitial Description

Prompt user, read 3 input integers and display their sumPrompt user, read 3 input integers and display their sum Prompt user, read 3 input integers and display the sum of all 6Prompt user, read 3 input integers and display the sum of all 6 Prompt user, read in 3 input integers and display the sum of all 9Prompt user, read in 3 input integers and display the sum of all 9

Refinement of first actionRefinement of first action Refinement of second actionRefinement of second action Refinement of third actionRefinement of third action

Display prompt for userDisplay prompt for user Read in values for Int1, Int2 and Int3Read in values for Int1, Int2 and Int3 Sum = Sum + Int1 + Int2 + Int3Sum = Sum + Int1 + Int2 + Int3 Display the value of SumDisplay the value of Sum

14

Testing a Program DesignTesting a Program Design Desk TracingDesk Tracing

Data values are chosen, and the actions are carried out by hand-Data values are chosen, and the actions are carried out by hand-tracing the program steps using these values. NB this may be tracing the program steps using these values. NB this may be done on paper (with the aid of a calculator).done on paper (with the aid of a calculator).

Assertion TestingAssertion Testing Rather than using test data the logic of the algorithm is tested.Rather than using test data the logic of the algorithm is tested. The state of the program is described at any one point, and the The state of the program is described at any one point, and the

effects of a step upon this is considered. This process is then effects of a step upon this is considered. This process is then repeated for the entire algorithm.repeated for the entire algorithm.

15

Example - Assertion TestingExample - Assertion Testing AssertionAssertion - when the first three numbers are entered the values - when the first three numbers are entered the values

of the 1st, 2nd and 3rd numbers are contained in Int1, Int2 and of the 1st, 2nd and 3rd numbers are contained in Int1, Int2 and Int3.Int3.

AssertionAssertion - when Int1+Int2+Int3 is assigned to Sum, it will - when Int1+Int2+Int3 is assigned to Sum, it will represent the sum of the first group of numbers entered.represent the sum of the first group of numbers entered.

AssertionAssertion - when Sum is displayed, the sum of the first group - when Sum is displayed, the sum of the first group of numbers to be entered is be displayed.of numbers to be entered is be displayed.

AssertionAssertion - when the second three numbers are entered the - when the second three numbers are entered the values of the 4th 5th and 6th numbers are contained in Int1, values of the 4th 5th and 6th numbers are contained in Int1, Int2 and Int3.Int2 and Int3.

AssertionAssertion - when Sum+Int1+Int2+Int3 is assigned to Sum, it - when Sum+Int1+Int2+Int3 is assigned to Sum, it will represent the sum of the first will represent the sum of the first twotwo groups of numbers groups of numbers entered.entered.

Etc.Etc.

16

Coding and TestingCoding and Testing

When the design is considered sound, the When the design is considered sound, the program is coded.program is coded.

Each line of pseudocode is replaced by real code.Each line of pseudocode is replaced by real code. The code is compiled and syntax errors are fixed.The code is compiled and syntax errors are fixed. The program is run with as many different data The program is run with as many different data

sets as possible (including extreme ones).sets as possible (including extreme ones).

17

State the problem

Is it feasible?

Plan and refine an action sequence

Does it solvethe problem?

Write and compilethe source code

Is it free of syntax errors? Run the program

Are the resultscorrect?

Run the program

Does it do the job?YesYesYesYes

NoNoNoNo

YesYesYesYes

NoNoNoNo

YesYesYesYes

NoNoNoNo

YesYesYesYes

NoNoNoNo

YesYesYesYes

NoNoNoNo

Hooray!

18

Coding HintsCoding Hints

Start coding with a template that you know Start coding with a template that you know works!works!

Make very small additions/changes and test the Make very small additions/changes and test the partly finished code:partly finished code:

Syntax errors - ie does it compileSyntax errors - ie does it compile Functionality - ie does it run correctlyFunctionality - ie does it run correctly Fix errors as they occurFix errors as they occur

Don’t write a complete program, and then expect Don’t write a complete program, and then expect it to work first time - it won’t!!it to work first time - it won’t!!

Use a good coding styleUse a good coding style

19

Coding StyleCoding Style

Write code that is easy to understandWrite code that is easy to understand Identifiers should be meaningfulIdentifiers should be meaningful

eg LineCounter rather than neg LineCounter rather than n The presentation of the code should reflect its structureThe presentation of the code should reflect its structure

eg indent code blocks, space lines etc.eg indent code blocks, space lines etc. Actions in the code should be commentedActions in the code should be commented Comments should add meaning, not repeat information Comments should add meaning, not repeat information

already in the codealready in the code

eg eg n++; // increment n n++; // increment n - This adds nothing - This adds nothing

n++; // count lines n++; // count lines - This is a useful comment- This is a useful comment

20

Bad StyleBad Style

public static String mc(int a, char b) {String c=""; while (a>0) {c=c+b; a--;} return (c);}

21

Good StyleGood Style/** * multiChar - method to return a string containing any * one character repeated any number of times. */

public static String multiChar(int number, char character) // number - no. characters to be drawn // character - the character to be drawn { String str=""; // str will contain the multiple characters

while (number>0) // character-writing loop - repeat // for each character to to be drawn { str=str+character; // add a single character to str number--; // reduce the number still to be // written by one } // end of character writing loop

return (str); // return the multiple characters }

22

Java Naming ConventionsJava Naming Conventions The main method of an application should be in a class The main method of an application should be in a class

that is contained in a source file of the same name.that is contained in a source file of the same name. Eg Eg class myprogclass myprog in in myprog.javamyprog.java (which will compile to (which will compile to myprog.classmyprog.class..

Class identifiers should begin with an upper case letterClass identifiers should begin with an upper case letter eg Myclass, String, System etc.eg Myclass, String, System etc.

Variable, and method identifiers should begin with a Variable, and method identifiers should begin with a lower case letter (although a mixture of cases may make lower case letter (although a mixture of cases may make them more readable.them more readable. Eg data1, myData, myObject, myMethod(), println() etcEg data1, myData, myObject, myMethod(), println() etc..