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

本文通过一个具体的案例展示了如何逐步简化和优化复杂代码的过程。从最初的冗长实现到逐步精简,作者分享了代码重构的技巧,并最终实现了更简洁高效的代码结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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), ComClass.TimesCombine(w.Day, 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), ComClass.TimesCombine(w.Day.AddDays(1), 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, ComClass.TimesCombine(w.Day, w.TimePeriod.CheckOutTime1Date), ComClass.TimesCombine(w.Day, 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, ComClass.TimesCombine(w.Day, w.TimePeriod.CheckInTime1Date), ComClass.TimesCombine(w.Day, w.TimePeriod.CheckInTime2Date))
If w.IsOverDay Then
	getAttOut2Day(w, ComClass.TimesCombine(w.Day.AddDays(1), w.TimePeriod.CheckOutTime1Date), ComClass.TimesCombine(w.Day.AddDays(1), w.TimePeriod.CheckOutTime2Date))
Else
	getAttOut1Day(w, ComClass.TimesCombine(w.Day, w.TimePeriod.CheckOutTime1Date), ComClass.TimesCombine(w.Day, 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

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

加油!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值