Member_2_5230414
asked on
VB.NET -> Cutting down on If statements
This is more of a learning exercise as the code works but i wanted to know if i could have done it with less code or better logic
Function Getthescores(ByVal whatwebpage As String, ByVal whattoloop As String, ByVal hometeam As String, ByVal awayteam As String, ByVal setthetime As String, ByVal homescore As String, ByVal awayscore As String) As String
Dim thescorestext As String
Dim webGet As New HtmlWeb() 'open the system
Dim htmlDoc As HtmlDocument = webGet.Load(whatwebpage) '' get the html from the webpage
Dim coll As HtmlNodeCollection = htmlDoc.DocumentNode.SelectNodes(whattoloop)
' do stuff
If coll IsNot Nothing Then
For Each div As Object In coll ' select all the divs within the code that contain *
If Not div.selectSingleNode(hometeam) Is Nothing Then
'set the time to make sure it can be orderd
Dim settime As String = System.Text.RegularExpressions.Regex.Replace(div.selectSingleNode(setthetime).InnerText, "[^a-zA-Z0-9]", " ")
If settime <> "" Then
settime = settime.Substring(1, 2)
End If
Dim orderbytime As Integer
If settime = "HT" Then
orderbytime = 45
ElseIf settime = "FT" Then
orderbytime = 999
Else
orderbytime = Convert.ToInt32(settime)
End If
thescorestext = ""
thescorestext += StringOutNumbers(div.selectSingleNode(hometeam).InnerText) 'get home team name
thescorestext += div.selectSingleNode(homescore).InnerText & " - " & div.selectSingleNode(awayscore).InnerText
thescorestext += StringOutNumbers(div.selectSingleNode(awayteam).InnerText) ' get away teams name
Dim count = scorestable.Rows.Cast(Of DataRow)().Where(Function(n) n("HomeTeam").ToString() = StringOutNumbers(div.selectSingleNode(hometeam).InnerText)).Count()
'Find the count
If count = 0 Then
'if not finished add row to the datatable as no results where found
scorestable.Rows.Add(StringOutNumbers(div.selectSingleNode(hometeam).InnerText), settime, thescorestext, "", "", orderbytime)
Else
'A matching result was found so now check to see if the Scores section of the result matches the value - thescorestext
For Each row As DataRow In scorestable.Rows
If row("HomeTeam").ToString() = StringOutNumbers(div.selectSingleNode(hometeam).InnerText) And row("Score").ToString() <> thescorestext Then
'add scores
row.SetField("Score", thescorestext)
row.SetField("Time", settime)
row.SetField("Lastgoaltime", DateTime.Now())
row.SetField("Kickoff", orderbytime)
ElseIf row("HomeTeam").ToString() = StringOutNumbers(div.selectSingleNode(hometeam).InnerText) And row("Score").ToString() = thescorestext Then
' add time
row.SetField("Time", settime)
row.SetField("Kickoff", orderbytime)
End If
Next
End If
End If
Next
End If
Return thescorestext
End Function
Don't be afraid to add more function definitions when you have functions that start to get unwieldy. Having to scroll down 2 or three pages to see the rest of a block of code becomes tiring; so is scrolling around to try and locate a variable's declaration. Just make sure that functions you create make sense and logically encapsulate a piece of logic--don't create a new function solely for the sake of having another function have less lines.
Both previous comments are correct. I would suggest looking at them both ;-)
the foreach loop seems for me a legit function extraction possibility
the comments are a common issue, but try to write at least for every 2-3 lines 1 line of comment. It's hard (time consuming) for you now but easy for the future and the time-profit can be easly ten times
regards
the foreach loop seems for me a legit function extraction possibility
the comments are a common issue, but try to write at least for every 2-3 lines 1 line of comment. It's hard (time consuming) for you now but easy for the future and the time-profit can be easly ten times
regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
this will make your code more readable.
you might also use regions directives to be able to "collapse" code regions
http://msdn.microsoft.com/en-us/library/sd032a17.aspx