VB.Net程序设计:代码简化过程(备忘录)

前端之家收集整理的这篇文章主要介绍了VB.Net程序设计:代码简化过程(备忘录)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码时候,第一次是为了实现需要功能

w.IsHasSpecialTime = True
dMints = ComClass.TimesLapse(dSub.ToDate,dSub.FromDate).TotalMinutes
If w.TimePeriod IsNot Nothing Then
	For Each d In AttTimeList
		If ComClass.TimeInScope(d,ComClass.TimesCombine(w.Day,w.TimePeriod.CheckInTime1Date),w.TimePeriod.CheckInTime2Date)) Then
			w.AttInDate = d
			Exit For
		End If
	Next
	If w.IsOverDay Then
		For i As Integer = AttTimeList2Day.Count - 1 To 0 Step -1
			d = AttTimeList2Day.Item(i)
			If ComClass.TimeInScope(d,ComClass.TimesCombine(w.Day.AddDays(1),w.TimePeriod.CheckOutTime1Date),w.TimePeriod.CheckOutTime2Date)) Then
				w.AttOutDate = d
				Exit For
			End If
		Next
	Else
		For i As Integer = AttTimeList.Count - 1 To 0 Step -1
			d = AttTimeList.Item(i)
			If ComClass.TimeInScope(d,w.TimePeriod.CheckOutTime2Date)) Then
				w.AttOutDate = d
				Exit For
			End If
		Next
	End If
	If w.AttInDate = MinDate OrElse w.AttOutDate = MinDate Then
		w.IsCalculate = True
		w.AttHours = 0
		w.InValidHours = (w.TimePeriod.WorkMinutes - dMints) / 60
		dSub.ValidHours = dMints / 60
	ElseIf w.AttInDate > MinDate And w.AttOutDate > MinDate Then
		w.IsCalculate = True
		w.AttHours = (w.TimePeriod.WorkMinutes - dMints) / 60
		dSub.ValidHours = dMints / 60
	End If
End If

处理效果还是可以,不过代码很长很长。同时间有10几个类似这样的代码段。加上其他内容差不多1000多行代码

于是就想简化点。弄了好久才得出简化结果。 10几个类似的代码段慢慢改。剩下800多行了。


w.IsHasSpecialTime = True
dMints = ComClass.TimesLapse(dSub.ToDate,dSub.FromDate).TotalMinutes
getAttIn(w,w.TimePeriod.CheckInTime2Date))
If w.IsOverDay Then
	getAttOut2Day(w,w.TimePeriod.CheckOutTime2Date))
Else
	getAttOut1Day(w,w.TimePeriod.CheckOutTime2Date))
End If
If w.AttInDate = MinDate OrElse w.AttOutDate = MinDate Then
	w.IsCalculate = True
	w.AttHours = 0
	w.InValidHours = (w.TimePeriod.WorkMinutes - dMints) / 60
	dSub.ValidHours = dMints / 60
ElseIf w.AttInDate > MinDate And w.AttOutDate > MinDate Then
	w.IsCalculate = True
	w.AttHours = (w.TimePeriod.WorkMinutes - dMints) / 60
	dSub.ValidHours = dMints / 60
End If

结果中间过程有点问题,忽略了很多节点判断,分析,于是结构重新调整,增加几个函数过程。

继续简化,优化。结果不知道能简化成多少行,但是单从字符多少来看,已经减少了很多字符了。

dMints = ComClass.TimesLapse(dSub.ToDate,dSub.FromDate).TotalMinutes
With w
	.IsHasSpecialTime = True
	.CalcWorkTimeRange()
End With
getAttTime(w)
If w.AttInDate = MinDate OrElse w.AttOutDate = MinDate Then
	w.AttHours = 0
	w.InValidHours = ToHour(w.WorkMinutes - dMints)
	dSub.ValidHours = ToHour(dMints)
ElseIf w.AttInDate > MinDate And w.AttOutDate > MinDate Then
	w.AttHours = ToHour(w.WorkMinutes - dMints)
	dSub.ValidHours = ToHour(dMints)
End If

期待能得出自己想要的结果。

加油!

原文链接:https://www.f2er.com/vb/258941.html

猜你在找的VB相关文章