Solved

Convert code from unstructured to structured with embedded assignments.

Posted on 2013-12-20
7
238 Views
Last Modified: 2014-01-18
Followup to question

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_28322873.html

Here is some spaghetti code with embedded assignments. I need to restructure the code without goto and line numbers.

If Abs(xi - x1) < Abs(0.00001 * xi) And Abs(yi - y1) < Abs(0.00001 * yi) Then F=1:GoTo 10
If Abs(xi - x2) < Abs(0.00001 * xi) And Abs(yi - y2) < Abs(0.00001 * yi) Then F=1:GoTo 10
If Abs(x1 - x2) < (0.0000001 * x1) Then If (yi - y1) * (yi - y2) < 0 Then F=2:GoTo 10 Else GoTo 20
If (xi - x1) * (xi - x2) < 0 Then F=2:GoTo 10
GoTo 20
10
If Abs(xi - x3) < Abs(0.00001 * xi) And Abs(yi - y3) < Abs(0.00001 * yi) Then F=F or 4:GoTo 30
If Abs(xi - x4) < Abs(0.00001 * xi) And Abs(yi - y4) < Abs(0.00001 * yi) Then F=F or 4:GoTo 30
If Abs(x3 - x4) < Abs(0.000001 * yi) Then If (yi - y3) * (yi - y4) < 0 Then F=F or 8:GoTo 30 Else GoTo 20
If (xi - x3) * (xi - x4) < 0 Then F=F or 8:GoTo 30
GoTo 20

30 F = 1

20 

Open in new window

0
Comment
Question by:Saqib Husain, Syed
7 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 39731996
Hi Saqib,

Give this a shot. I used fanpages' testing sub from the other question and it all matches:
Function F(xi As Double, x1 As Double, x2 As Double, x3 As Double, x4 As Double, _
            yi As Double, y1 As Double, y2 As Double, y3 As Double, y4 As Double) As Long
 
 If (Abs(xi - x1) < Abs(0.00001 * xi) And Abs(yi - y1) < Abs(0.00001 * yi)) Or _
    (Abs(xi - x2) < Abs(0.00001 * xi) And Abs(yi - y2) < Abs(0.00001 * yi)) Then
  F = 1
 ElseIf (Abs(x1 - x2) < (0.0000001 * x1) And (yi - y1) * (yi - y2) < 0) Or _
        ((xi - x1) * (xi - x2) < 0) Then
  F = 2
 Else
  Exit Function
 End If
 
 If Not (Abs(xi - x3) < Abs(0.00001 * xi) And Abs(yi - y3) < Abs(0.00001 * yi)) And _
    Not (Abs(xi - x4) < Abs(0.00001 * xi) And Abs(yi - y4) < Abs(0.00001 * yi)) Then
  If Abs(x3 - x4) < Abs(0.000001 * yi) And Not ((yi - y3) * (yi - y4) < 0) Then Exit Function
  If Not (Abs(x3 - x4) < Abs(0.000001 * yi)) And Not ((xi - x3) * (xi - x4) < 0) Then Exit Function
 End If
 
 F = 1
End Function

Open in new window

Matt
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39733343
Thanks for looking at this Matt :)
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39733359
This is what I came up with:-
Sub Restructured()

    Dim xi As Double, x1 As Double, x2 As Double, x3 As Double, x4 As Double
    Dim yi As Double, y1 As Double, y2 As Double, y3 As Double, y4 As Double
    Dim F As Integer
    
    F = Abs((Abs(xi - x1) < Abs(0.00001 * xi) And Abs(yi - y1) < Abs(0.00001 * yi)) Or _
            (Abs(xi - x2) < Abs(0.00001 * xi) And Abs(yi - y2) < Abs(0.00001 * yi)))
    If F = 0 Then
        If ((Abs(x1 - x2) < (0.0000001 * x1)) And _
           ((yi - y1) * (yi - y2) < 0)) Or _
           ((xi - x1) * (xi - x2) < 0) Then F = 2
    End If
    
    If F Then
        If (Abs(xi - x3) < Abs(0.00001 * xi) And Abs(yi - y3) < Abs(0.00001 * yi)) Or _
           (Abs(xi - x4) < Abs(0.00001 * xi) And Abs(yi - y4) < Abs(0.00001 * yi)) Then
            F = 1
        End If
        If F = 2 Then
            If ((Abs(x3 - x4) < Abs(0.000001 * yi)) And _
               ((yi - y3) * (yi - y4) < 0)) Or _
               ((xi - x3) * (xi - x4) < 0) Then F = 1
        End If
    End If
End Sub

Open in new window

I point out that the original code goes to some length to assign various values to F in its lower portion all of which are converted to =1 at line 30. This renders the jump to 30 unnecessary because the value =1 can be assigned immediately.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39733798
^ You are assuming that it is uninitialised when the code snippet commences.

If the jump to line 20 is valid during the successive tests, then setting F to 1 "immediately" (at the beginning of the code snippet) may be an inappropriate assumption.
0
 
LVL 14

Expert Comment

by:Faustulus
ID: 39735282
That isn't what I am saying.
Since label 30 assigns F=1 and continues with label 20 any jump to label 30 can be replaced with the assignment F=1 followed by continuation at label 20.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39735942
OK.  It was the wording of "immediately" that was confusing.  Thanks for clarifying what you meant.
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 39735991
Sorry for the confusion. I copied the code from the previous question and modified it. I forgot to delete the F=1 in after the label 30. I shall come back when I have tested the solutions. Apparently the first one seems good.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now