17 – modular design in asp
Post on 31-Jan-2016
34 Views
Preview:
DESCRIPTION
TRANSCRIPT
Mark Dixon Page 1
17 – Modular Design in ASP
Mark Dixon Page 2
Questions: Session variables• Write a line of VB code to put 74 into a
session variable called score.
• Write VB code that adds 1 to a variable called g, when a session variable called i is over 25.
If Session("i") > 25 Then
g = g + 1
End If
Session("a") = 74
Mark Dixon Page 3
Session Aims & Objectives• Aims
– Highlight modular design techniques, and demonstrate them in ASP
• Objectives,by end of this week’s sessions, you should be able to:
– Identify dependencies between lines of code– Determine whether a routine is self-contained– Use procedures, functions, parameters, and
modules (shared VB script files) in ASP
Mark Dixon Page 4
Example: Apples (analysis)SPECIFICATION
• User Requirements – help young children learn to count from 1 to 10
• Software Requirements– Functional:
–computer selects number between 1 and 10–computer displays that number of apples–user types digits–computer compares digits to number of apples
– Non-functionalshould be easy to use and interesting
Mark Dixon Page 5
Example: Apples v2 (design)• Functionality:
• computer selects number between 1 and 10• computer displays that number of apples• user types digits• computer compares digits to number of apples
Mark Dixon Page 6
Example: Apples v2 (code)Dim n As Long
Sub Page_Load() Dim html As String Dim a As Long If Request.Form("btnStart") > "" Then n = 1 + Int(Rnd() * 9) html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next quest.InnerHtml = html Session("NumApples") = n msg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False ElseIf Request.Form("btnCheck") > "" Then n = Session("NumApples") If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = "Correct, well done!" Else msg.InnerHtml = "Sorry, please try again." End If btnStart.Disabled = False btnCheck.Disabled = True End If End Sub
Apples.aspxDim n As Long
Sub Page_Load() Dim html As String Dim a As Long If Request.Form("btnStart") > "" Then n = 1 + Int(Rnd() * 9) html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next quest.InnerHtml = html Session("NumApples") = n msg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False
Mark Dixon Page 7
Example: Apples v2 (code)Dim n As Long
Sub Page_Load() Dim html As String Dim a As Long If Request.Form("btnStart") > "" Then n = 1 + Int(Rnd() * 9) html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next quest.InnerHtml = html Session("NumApples") = n msg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False ElseIf Request.Form("btnCheck") > "" Then n = Session("NumApples") If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = "Correct, well done!" Else msg.InnerHtml = "Sorry, please try again." End If btnStart.Disabled = False btnCheck.Disabled = True End If End Sub
Apples.aspx
ElseIf Request.Form("btnCheck") > "" Then n = Session("NumApples") If CInt(txtAns.Value) = CInt(n) Then msg.InnerHtml = "Correct, well done!" Else msg.InnerHtml = "Sorry, please try again." End If btnStart.Disabled = False btnCheck.Disabled = True End If End Sub
Mark Dixon Page 8
Problem Solving Strategies• bottom-up
– Create a detailed solution first– Then look for best solution– refactoring – process of:
• changing internal design of code,• without altering what it does
• top-down– plan overall design– fill in details
in practice mixed – novices favour bot-up, experts top-down
Mark Dixon Page 9
• Useful to understand code:
Dim a As Long
Dim b As Long
Dim c As Double
a = 12
b = a + 5
c = b + 1.25
Dry Running
a b c
- - -
- - -
- - -
12 - -
12 17 -
12 17 18.25
Mark Dixon Page 10
Dependencies: Numeric Variables
• consider the following code:1 Dim h As Long2 Dim q As Long3 h = 54 q = h + 2
• line 3 is dependent on line 1 (it involves h, it needs line 1)
• line 4 is dependent on line 3 and line 2
Mark Dixon Page 11
Dependencies: String Variables• consider the following code:
1 Dim surname As String2 Dim forename As String3 Dim initials As String4 surname = "Jones"5 forename = "Alice"6 initials = Left(surname, 1) & Left(forename, 1)
• line 6 is dependent on lines 4 and 5 (it uses the values in the surname and forename variables)
• line 5 is dependent on line 2• line 4 is dependent on line 1
Mark Dixon Page 12
Question: Variable Dependencies• What dependencies exist in the following code?
Dim q1 As String
Dim q2 As String
Dim u As Long
Dim o As Long
Dim g As Long
q1 = "It is not enough to have a good mind."
q2 = "The main thing is to use it well."
u = Len(q1)
o = Len(q2)
g = u + o
Mark Dixon Page 13
Example: Apples (Dependencies)• Difficult to see
dependencies for lines far apart
Dim n As Long
Sub Page_Load() Dim html As String Dim a As Long If Request.Form("btnStart") > "" Then n = 1 + Int(Rnd() * 9) html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next quest.InnerHtml = html Session("NumApples") = n msg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False
Mark Dixon Page 14
Example: Apples (Dependencies)• Put dependent
lines close together
Dim n As Long
Sub Page_Load() Dim html As String Dim a As Long If Request.Form("btnStart") > "" Then n = 1 + Int(Rnd() * 9) Session("NumApples") = n html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next quest.InnerHtml = html msg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False
Mark Dixon Page 15
Example: Apples v3 (design)• Functionality:
• computer selects number between 1 and 10• computer displays that number of apples• user types digits
• computer compares digits to number of apples
and displays number of apples typed by user
Mark Dixon Page 16
Example: Apples v3 (code)Dim n As Long
Sub Page_Load() Dim html As String Dim msg As String Dim a As Long If Request.Form("btnStart") > "" Then n = 1 + Int(Rnd() * 9) Session("NumApples") = n html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next parQuest.InnerHtml = html parMsg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False ElseIf Request.Form("btnCheck") > "" Then n = Session("NumApples") msg = "" For a = 1 To txtAns.Value msg = msg & "<img src=Apple.gif>" Next If CInt(txtAns.Value) = CInt(n) Then msg = msg & "Correct, well done!" Else msg = msg & "Sorry, please try again." End If parMsg.InnerHtml = msg btnStart.Disabled = False btnCheck.Disabled = True End If End Sub
Apples.aspx
ElseIf Request.Form("btnCheck") > "" Then n = Session("NumApples") msg = "" For a = 1 To txtAns.Value msg = msg & "<img src=Apple.gif>" Next If CInt(txtAns.Value) = CInt(n) Then msg = msg & "Correct, well done!" Else msg = msg & "Sorry, please try again." End If parMsg.InnerHtml = msg btnStart.Disabled = False btnCheck.Disabled = True End If End Sub
• copy + paste
Mark Dixon Page 17
Modular Design• What do lines do (group summary)?
n = 1 + Int(Rnd() * 9) Session("NumApples") = n html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next parQuest.InnerHtml = html parMsg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False
Pick Num. of Apples
Display Question
Prepare for Response
Mark Dixon Page 18
Modular Design (top level)• Top level reads like English algorithm:
Dim n As LongDim html As StringDim msg As StringDim a As Long
Sub Page_Load() If Request.Form("btnStart") > "" Then PickRandomNumberOfApples DisplayApplesQuest PrepareForResponse ElseIf Request.Form("btnCheck") > "" Then n = Session("NumApples") DisplayApplesUser DisplayFeedback PrepareForQuest End If End Sub
Mark Dixon Page 19
Modular Design (detail)• Procedures contain (hide) detail:
Sub PickRandomNumberOfApples() n = 1 + Int(Rnd() * 9) Session("NumApples") = n End Sub Sub DisplayApplesQuest() Dim html As String html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next parQuest.InnerHtml = html End Sub Sub DisplayApplesUser() msg = "" For a = 1 To txtAns.Value msg = msg & "<img src=Apple.gif>" Next End Sub Sub DisplayFeedback() If CInt(txtAns.Value) = CInt(n) Then msg = msg & "Correct, well done!" Else msg = msg & "Sorry, please try again." End If parMsg.InnerHtml = msg End Sub Sub PrepareForResponse() parMsg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False End Sub Sub PrepareForQuest() btnStart.Disabled = False btnCheck.Disabled = True End Sub
Sub DisplayApplesQuest() html = "How many apples are there?<br />" For a = 1 To n html = html & "<img src=Apple.gif>" Next parQuest.InnerHtml = html End Sub Sub DisplayApplesUser() msg = "" For a = 1 To txtAns.Value msg = msg & "<img src=Apple.gif>" Next End Sub
Mark Dixon Page 20
Routines: Self-Contained• Good design principle:
– routines (functions and procedures)should be self-contained(easier to re-use in other programs)
Dim u As Long u = Twice(4)
Function Twice(a As Long) Return a * 2End Function
Dim u As LongDim a As Long a = 4 u = Twice()
Function Twice() Return a * 2End Function
Mark Dixon Page 21
Question: Self-Contained Routines
• Are the following routines self contained?Dim num1
Dim num2
Dim diff
Sub Compare()
diff = num1 - num2
End Sub
Function Half(num As Double) As Double
Return num / 2
End Function
Mark Dixon Page 22
Debugging• key skill:
– locate the cause of a bug• using testing methods
• first step– narrow it down as much as possible
• typical pattern in early tutorials:– student: it doesn't work– lecturer: what doesn't work– student: my code– lecturer: yes, but which bit exactly– student: ????– lecturer: run your program, take me through it, which bits work, and
where exactly does it go wrong– student: when I click this, nothing happens– lecturer: which bit of code is responsible for doing that?– student: this bit
Mark Dixon Page 23
Problem Solving: 9 dots• Join all 9 dots
– with straight continuous lines
Mark Dixon Page 24
Problem Solving Process (Name Split)
• Problem: a variable exists called n. This contains a person's full name (forename, then surname ). It needs to be split into two separate variables.
Dim n As String
n = "Ruth Jones"
• Solution Process:– What do I do to solve this manually (on paper)?– How do I know where the forename ends and the
surname begins?– The space is the key:
• Find the space• everything before the space is the forename• everything after the space is the surname
Mark Dixon Page 25
Tutorial Exercise: Apples• LEARNING OBJECTIVE:
identify dependencies between lines of coderefactor code: dependent lines closerrefactor code: split into routines (procedures and functions)refactor code: make routines self-contained
• Task 1: Get the Apples v3 example (from the lecture) working• Task 2: Modify your page to keep a score.
HINT: Try to identify the routines first, then fill in the code.
top related