1 / 25

17 – Modular Design in ASP

17 – Modular Design in ASP. 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.

starr
Download Presentation

17 – Modular Design in ASP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 • 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

  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

  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 If 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 If 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 If 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 + 1.25 a b c - - - - - - - - - 12 - - 12 17 - 12 17 18.25

  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

  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

  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>" 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 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

  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 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

  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: • 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 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.

More Related