good software design cohesion - monash university€“ examples: elements all occur ... pseudocode...

5
www.monash.edu.au IMS1906 Programming in VB.NET Week 12 – Lecture 1 & 2 Structures Part II and Test 2 Solutions © Angela Carbone Monash University School of Information Management and Systems www.monash.edu.au 2 Outline Good Software Design Structures in algorithms/pseudo code Test 2 solutions and explanations www.monash.edu.au 3 Good Software Design How can we build software that is: – Robust Clear to understand the code Re-usable in other systems? How can we compare the quality of one module against another module to choose the ‘better’ version? Coupling and Cohesion are some measures we can apply to the design of our modules. www.monash.edu.au 4 Cohesion Cohesion is a measure of how the elements within a single module (procedure) are associated with each other Closely related = high/strong cohesion Seven Levels of Cohesion: – Coincidental – Logical – Temporal – Procedural – Communicational – Sequential – Functional Weakest Strongest www.monash.edu.au 5 Cohesion (cont.) Coincidental: Weakest form Usually occurs when unrelated actions are placed together in one module No meaningful relationship between instructions in module Logical: The module can perform several things that are in some way related, e.g. they all involve reading from a file However, the module will only perform one of these several things, selected on the basis of logic (a Boolean condition) Often selection is based on value of data passed to the module www.monash.edu.au 6 Cohesion (cont.) Temporal: Elements of a module are related by time All those elements occur when module is run – Examples: elements all occur ‘when program is starting’, elements all occur ‘when initialising a new customer entry’ elements all occur ‘when the “Clear Data” button is clicked’ Procedural: Elements are related because they occur in an order that together defines a procedure (sequence) Typical example: the mainline module

Upload: lymien

Post on 18-Apr-2018

218 views

Category:

Documents


4 download

TRANSCRIPT

www.monash.edu.au

IMS1906 Programming in VB.NETWeek 12 – Lecture 1 & 2

Structures Part II and Test 2 Solutions

© Angela CarboneMonash University School of Information Management and Systems

www.monash.edu.au2

Outline

• Good Software Design• Structures in algorithms/pseudo code• Test 2 solutions and explanations

www.monash.edu.au3

Good Software Design

• How can we build software that is:– Robust– Clear to understand the code– Re-usable in other systems?

• How can we compare the quality of one module against another module to choose the ‘better’ version?

• Coupling and Cohesion are some measures we can apply to the design of our modules.

www.monash.edu.au4

Cohesion

• Cohesion is a measure of how the elements within a single module (procedure) are associated with each other

– Closely related = high/strong cohesion• Seven Levels of Cohesion:

– Coincidental– Logical– Temporal– Procedural– Communicational– Sequential– Functional

Weakest

Strongest

www.monash.edu.au5

Cohesion (cont.)

• Coincidental:– Weakest form– Usually occurs when unrelated actions are placed

together in one module– No meaningful relationship between instructions in

module• Logical:

– The module can perform several things that are in some way related, e.g. they all involve reading from a file

– However, the module will only perform one of these several things, selected on the basis of logic (a Boolean condition)

– Often selection is based on value of data passed to the module

www.monash.edu.au6

Cohesion (cont.)

• Temporal:– Elements of a module are related by time– All those elements occur when module is run– Examples:

elements all occur ‘when program is starting’,elements all occur ‘when initialising a new customer entry’elements all occur ‘when the “Clear Data” button is clicked’

• Procedural:– Elements are related because they occur in an order

that together defines a procedure (sequence)– Typical example: the mainline module

www.monash.edu.au7

Cohesion (cont.)

• Communicational:– The elements of module collectively operate on the

same piece of data– Examples: Validation modules

> Check that a product id was entered> If so, check that product id is actually numeric> Also check that it is a valid product id for the system

– The ‘data’ operated on may be an object / data structure– thus consisting of component data.

> For example: a Date consists of Day, Month, and Year

www.monash.edu.au8

Cohesion (cont.)

• Sequential:– Like an assembly line– Later steps depend on processing of earlier steps– e.g. Calculate total cost, then calculate GST payable on

total• Functional:

– All elements contribute to perform a single specific task– Module can be concisely described as a verb followed by

a one or two word object/noun– Examples:

> ComputeSalesTax> ReturnUserID> GetCustomerName

www.monash.edu.au9

Coupling

• Coupling is a measure of the extent of information interchange between modules

– Loose coupling = independence = better coupling• Myers identifies five levels of coupling:

– Common– External– Control– Stamp– Data

Tight

Loose

www.monash.edu.au10

Coupling (cont.)

• Common coupling:– Two distinct modules reference identical global data

structure– One module could change part of that data structure

without the other knowing• External Coupling:

– Two distinct modules reference identical global data item– One module could change the value of the data item

without the other knowing.• Control Coupling:

– The calling module passes data (a flag) to called module to control how called module is to behave

– Implies caller is aware of the logic of called module –L

www.monash.edu.au11

Coupling (cont.)

• Stamp Coupling– One module passes non-global data structure to

another module– Called module does not necessarily need or use all

components of the data structure passed to it• Data Coupling

– One module passes non-global data item to another module

– Only the data needed by the called module is passed to it

www.monash.edu.au12

Guide to ‘Good’ Module Design

• Aim for modules with strong cohesion– Functional, sequential or communicational– Not completely possible, particularly with mainline

included.• Aim for modules with loose coupling

– Use local variables – avoid global variables– Write self -contained modules– Avoid large numbers of parameters

• However, overriding aim is for programs that are:

– Easily understood (by humans)– Easy to modify

www.monash.edu.au13

Structures in pseudo-code

• Structures (User-defined data types) are represent as simple variables in pseudocode algorithms

– structures are a language dependent construct

www.monash.edu.au14

Example Problem

• Design and write a program to store the inventory data for a bookshop. Each book has an ISBN, title, author, edition number, year of publication, and publisher. This information does not change.

• Each book also has pricing information including the cost price paid for the stock item, the recommended retail price from the publisher, the retail price charged and the number of copies in stock. RRP and retail price may change, copies held changes regularly.

• The program should capture all this information and store it in a file.

• The user should be able to update details for a book and save these back to the file.

• Total cost of all stock held can be totalled and displayed

www.monash.edu.au15

Defining Diagram

Book details matching search criteriaTotal stock value

Input detailsSave details to fileLocate and display title termCalculate stock total

Book:ISBNTitleAuthorEditionYearPublisher

Pricing:Cost priceRRPRetail priceCopies held

OutputProcessingInput

www.monash.edu.au16

Add Book Algorithm

addBookPrompt for ISBN, title, author, edition, pubYear, publisherGet ISBN, title, author, edition, year, publisherset validFlag to TrueIF characters in ISBN < 13 THEN

validFlag = FalseENDIFIF title =“” OR author =“” OR publisher =“” THEN

validFlag = FalseENDIFIF edition <=0 THEN

validFlag = FalseENDIFIF pubYear > this year OR pubYear < 1900 THEN

validFlag = FalseENDIFIF validFlag = True THEN

Write ISBN, title, author, edition, pubYear, publisher to fileELSE

Display error messageENDIF

END

www.monash.edu.au17

Add Stock Details Algorithm

addPricingPrompt for costPrice, rrp, retail, copiesHeldGet costPrice, rrp, retail, copiesHeldset validFlag to TrueIF costPrice < 0.1 OR RRP < 0.1 OR retail < 0.1 THEN

validFlag = FalseENDIFIF copiesHeld < 0 THEN

validFlag = FalseENDIFIF validFlag = True THEN

Write ISBN, costPrice, rrp, retail, copiesHeld to fileELSE

Display error messageENDIF

END

www.monash.edu.au18

Example: Student Marks problem

• Problem:Write a program that asks for the name, and three assignment marks for students in a class. After reading all the marks, display this information in a table.

AA1 AA2 AA3 Exam Linda Abrahams 12 17 19 66 Jeno Jando 15 15 11 73 Nadia Morey 19 14 6 51

www.monash.edu.au19

Arrays of Structures

• Use a 1D array of a Structure-type– Define the Structure type (e.g. StudentInfoStruc):

Structure StudentInfoStrucPublic strName As StringPublic intID As IntegerPublic int_AA1_Mark As IntegerPublic int_AA2_Mark As Integer

End Structure

– Declare one array – using the Structure type:Dim udtStudentInfoArray() As StudentInfoStruc

www.monash.edu.au20

Referring to a record field in an array

• To access a field of the Structure record, when using an array of records:

– State the array name– Specify the element (position) in brackets– Use the ‘dot’ operator, to access the fields of that element– Name the field you desire to use

• Example:udtStudentInfoArray(2).strName = “Kylie Schultz”

udtStudentInfoArray(2).intID = 15928767

www.monash.edu.au21

Structures containing Arrays

• It is also possible to use an array inside a Structure– For example, each student may have 3 assignment marks– The marks could be store in a ‘marks’ array:Structure StudentInfoStrucPublic strName As StringPublic intID As IntegerPublic intMarks(3) As Integer

End Structure

Array inside structure

www.monash.edu.au22

Reading

• Study Guide 10 – attempt the exercises

• Zak Chapter 10

www.monash.edu.au

IMS1906 Programming in VB.NETWeek 12 – Lecture 2

Test 2 Solutions

© Angela CarboneMonash University School of Information Management and Systems

www.monash.edu.au24

Test 2 Solutions

• Are available from the unit’s website under the assessment link.

• The lecture will discuss:– Common misunderstandings

> Programming Errors> Pass byRef vs. Pass byVal> Subroutines and Functions> File I/O> List boxes> Try/Catch

– Recap some important concepts

www.monash.edu.au25

IMS1906 Exam Outline

• 2 hour exam (10 mins reading time)– Section 1:

> 20 multiple choice questions [20 marks][One question from each lecture or tutorial]

– Section 2: > Evaluate expressions [30 marks]> Design an algorithm in pseudo-code> programming concepts (desk-checking, Class/Objects, variables,

data-types, constants, structure theorem, etc)

– Section 3: > VB.NET programming [50 marks]> Write a full programming solution to a medium sized problem

involving File I/O, arrays, structures, lists, exception handling, loops, if-statements

www.monash.edu.au26

Review of Unit

• A survey will be distributed reviewing the unit [this will take approximately 15 minutes]

• Thank-you for your contribution.