我有以下代码,我的雇主希望我减少代码行来完成这项工作。
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
我无法弄清楚如何剪切线条并具有相同的功能!
是否有任何vb.net构造可以通过更好的实现来减少loc?
我可以从getClientSheetNames和getDevSheetNames调用一个可以实现代码重新生成的新函数吗?
我可以在可能的新函数中引入多态吗?
欢迎任何性能改进!
请帮助!!
答案 0 :(得分:4)
一个功能应该做一件事。这两个函数都执行五个独立的,无关的任务。画一个流程图,你会发现你几乎最终得到了五个不同的功能。因此,将每个函数分成五个单独的函数。
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)
他们关联的最多是getAll~
包括getCurrentMonth~
和getPreviousMonth~
,您可以从getAll~
拨打电话。
这将改善逻辑和可读性。它可能(没有gaurantees)甚至减少你的LoC。