Can the lines of code be reduced in this vb.net code?
I have the following code, my employer wants me to reduce the lines of code to do the job.
Private Function getClientSheetNames(Optional ByVal type As String = "all") As List(Of String)
If type = "extra" Then
Return clientExtraSheetNames
End If
Dim prev, curr As New List(Of String)
For Each name In clientMonthlySheetNames
curr.Add(name + " (" + currMonth + ")")
Next name
If type = "currMonth" Then
Return curr
End If
For i = 1 To clientMonthlySheetNames.Count - 1
prev.Add(clientMonthlySheetNames(i) + " (" + prevMonth + ")")
Next
If type = "prevMonth" Then
Return prev
End If
If type = "monthly" Then
Return curr.Union(prev).ToList
End If
Return clientExtraSheetNames.Union(curr.Union(prev)).ToList
End Function
Private Function getDevSheetNames(Optional ByVal type As String = "all") As List(Of String)
If type = "extra" Then
Return devExtraSheetNames
End If
Dim sheetNames, prev, curr As New List(Of String)
For Each name In devMonthlySheetNames
curr.Add(name + " (" + currMonth + ")")
Next name
If type = "currMonth" Then
Return curr
End If
For Each name In devMonthlySheetNames
prev.Add(name + " (" + prevMonth + ")")
Next name
If type = "prevMonth" Then
Return prev
End If
If type = "monthly" Then
Return curr.Union(prev).ToList
End If
sheetNames.Add(devExtraSheetNames(0))
sheetNames.AddRange(curr.Union(prev).ToList)
sheetNames.AddRange(devExtraSheetNames.GetRange(1, devExtraSheetNames.Count - 1))
Return sheetNames
End Function
I am not able to make out how can I cut the lines and have the same functionality !
Are there any vb.net construct that can reduce the loc by better开发者_StackOverflow社区 implementation ?
Can I have a new function to be called from getClientSheetNames and getDevSheetNames that will can implement code resue ?
Can I introduce polymorphism in a possible new function ?
Any performance improvements are most welcome !
Please help !!
A function should do one thing. Both of those functions do five separate, unrelated tasks. Draw a flowchart and you'll see you almost end up with five distinct functions. So split each function into five individual functions.
Function getAllClientSheetNames() As List(Of String)
Function getExtraClientSheetNames() As List(Of String)
Function getCurrentMonthClientSheetNames() As List(Of String)
Function getPreviousMonthClientSheetNames() As List(Of String)
Function getMonthlyClientSheetNames() As List(Of String)
The most they correlate is that getAll~
includes both getCurrentMonth~
and getPreviousMonth~
, which you can call from getAll~
.
This will improve logic and readability. It may (no gaurantees) even reduce your LoC.
精彩评论