25 Then g = g + 1 End If"> 25 Then g = g + 1 End If">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

17 – Modular Design in ASP.

Similar presentations


Presentation on theme: "17 – Modular Design in ASP."— Presentation transcript:

1 17 – Modular Design in ASP

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. Session("a") = 74 If Session("i") > 25 Then g = g + 1 End If

3 Session Aims & Objectives
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

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-functional should be easy to use and interesting

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

6 Example: Apples v2 (code)
Apples.aspx 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 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 Sub

7 Example: Apples v2 (code)
Apples.aspx 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 Sub 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 Sub

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

9 Dry Running Useful to understand code: Dim a As Long Dim b As Long
Dim c As Double a = 12 b = a + 5 c = b a b c - - - - - - - - - 12 - - 12 17 - 12 17 18.25

10 Dependencies: Numeric Variables
consider the following code: 1 Dim h As Long 2 Dim q As Long h = 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

11 Dependencies: String Variables
consider the following code: 1 Dim surname As String 2 Dim forename As String 3 Dim initials As String surname = "Jones" forename = "Alice" 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

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

13 Example: Apples (Dependencies)
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 Difficult to see dependencies for lines far apart

14 Example: Apples (Dependencies)
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 Put dependent lines close together

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

16 Example: Apples v3 (code)
Apples.aspx copy + paste 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>" 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 Sub 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 Sub

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

18 Modular Design (top level)
Top level reads like English algorithm: Dim n As Long Dim html As String Dim msg As String Dim 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

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 Sub DisplayApplesUser() msg = "" For a = 1 To txtAns.Value msg = msg & "<img src=Apple.gif>" 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 Sub PrepareForResponse() parMsg.InnerHtml = "" txtAns.Value = "" btnStart.Disabled = True btnCheck.Disabled = False Sub PrepareForQuest() btnStart.Disabled = False btnCheck.Disabled = True 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>"

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 Dim a As Long a = 4 u = Twice() Function Twice() Return a * 2 End Function Dim u As Long u = Twice(4) Function Twice(a As Long) Return a * 2 End Function

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

22 Debugging key skill: first step typical pattern in early tutorials:
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

23 Problem Solving: 9 dots Join all 9 dots with straight continuous lines

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

25 Tutorial Exercise: Apples
LEARNING OBJECTIVE: identify dependencies between lines of code refactor code: dependent lines closer refactor 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.


Download ppt "17 – Modular Design in ASP."

Similar presentations


Ads by Google