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

来源:互联网 发布:淘宝交易指数在哪里看 编辑:程序博客网 时间:2024/06/08 15:16

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

w.IsHasSpecialTime = TruedMints = ComClass.TimesLapse(dSub.ToDate, dSub.FromDate).TotalMinutesIf w.TimePeriod IsNot Nothing ThenFor Each d In AttTimeListIf ComClass.TimeInScope(d, ComClass.TimesCombine(w.Day, w.TimePeriod.CheckInTime1Date), ComClass.TimesCombine(w.Day, w.TimePeriod.CheckInTime2Date)) Thenw.AttInDate = dExit ForEnd IfNextIf w.IsOverDay ThenFor i As Integer = AttTimeList2Day.Count - 1 To 0 Step -1d = 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)) Thenw.AttOutDate = dExit ForEnd IfNextElseFor i As Integer = AttTimeList.Count - 1 To 0 Step -1d = AttTimeList.Item(i)If ComClass.TimeInScope(d, ComClass.TimesCombine(w.Day, w.TimePeriod.CheckOutTime1Date), ComClass.TimesCombine(w.Day, w.TimePeriod.CheckOutTime2Date)) Thenw.AttOutDate = dExit ForEnd IfNextEnd IfIf w.AttInDate = MinDate OrElse w.AttOutDate = MinDate Thenw.IsCalculate = Truew.AttHours = 0w.InValidHours = (w.TimePeriod.WorkMinutes - dMints) / 60dSub.ValidHours = dMints / 60ElseIf w.AttInDate > MinDate And w.AttOutDate > MinDate Thenw.IsCalculate = Truew.AttHours = (w.TimePeriod.WorkMinutes - dMints) / 60dSub.ValidHours = dMints / 60End IfEnd If

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

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


w.IsHasSpecialTime = TruedMints = ComClass.TimesLapse(dSub.ToDate, dSub.FromDate).TotalMinutesgetAttIn(w, ComClass.TimesCombine(w.Day, w.TimePeriod.CheckInTime1Date), ComClass.TimesCombine(w.Day, w.TimePeriod.CheckInTime2Date))If w.IsOverDay ThengetAttOut2Day(w, ComClass.TimesCombine(w.Day.AddDays(1), w.TimePeriod.CheckOutTime1Date), ComClass.TimesCombine(w.Day.AddDays(1), w.TimePeriod.CheckOutTime2Date))ElsegetAttOut1Day(w, ComClass.TimesCombine(w.Day, w.TimePeriod.CheckOutTime1Date), ComClass.TimesCombine(w.Day, w.TimePeriod.CheckOutTime2Date))End IfIf w.AttInDate = MinDate OrElse w.AttOutDate = MinDate Thenw.IsCalculate = Truew.AttHours = 0w.InValidHours = (w.TimePeriod.WorkMinutes - dMints) / 60dSub.ValidHours = dMints / 60ElseIf w.AttInDate > MinDate And w.AttOutDate > MinDate Thenw.IsCalculate = Truew.AttHours = (w.TimePeriod.WorkMinutes - dMints) / 60dSub.ValidHours = dMints / 60End If

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

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

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

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

加油!


原创粉丝点击