HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

Optimize a Date and Timespan function

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
functiontimespandateoptimizeand

Problem

I have a working function that may be optimizable.

Currently processing 4 objects gives me 0.0459822 for the elapsed time in seconds - used for testing purposes. Once implemented into the app I will be checking > 1000 objects at a time and would like to speed this up if possible. The format of the fd.dataTime and the other TimeSpans are from a database and I have no control over the format. The reason for adding a day to some Dates is due to the time going over midnight and would yield an incorrect minimum timespan as a result.

Required: manually adding a day to the date if the time is less than the date - only case is presented below(before midnight date - after midnight timespans). Only store valid timespans. nullField is a String that = 0:00:00 an empty TimeSpan found in DB.

Here is the Function:

Private Function GetMinTimeSpan(fds As List(Of Field)) As String
 Dim result As New List(Of Date)
 For Each fd In fds
  Dim dt As Date = fd.dateTime
  Dim s1 As TimeSpan = TimeSpan.Parse(dt.ToString("HH:mm:ss"))
  Dim s2 As TimeSpan = TimeSpan.Parse(fd.sg)
  Dim s3 As TimeSpan = TimeSpan.Parse(fd.os)
  If Not fd.sg = nullField Then
    If s1 > s2 Then
      result.Add(Date.Parse(dt.AddDays(1).ToShortDateString & " " & fd.sg))
    Else
      result.Add(Date.Parse(dt.ToShortDateString & " " & fd.sg))
    End If
  End If
  If s1 > s3 Then
    result.Add(Date.Parse(dt.AddDays(1).ToShortDateString & " " & fd.os))
  Else
    result.Add(Date.Parse(dt.ToShortDateString & " " & fd.os))
  End If
 Next
 Return result.Min.ToString("HH:mm:ss")
End Function


Here is a sample testing:

```
Dim sw As New Stopwatch
sw.Start()
Dim fd1 As New Field With {.dateTime = Date.Now, .sg = "23:59:00", .os = "00:03:00"}
Dim fd2 As New Field With {.dateTime = Date.Now, .sg = "00:01:00", .os = "00:03:00"}
Dim fd3 As New Field With {.dateTime = Date.Now, .sg = "0:00:00", .os = "00:04:00"}
Dim fd4 As New Field With {.dateTime = Date.Now, .sg = "00:12:00", .os = "00:05:00"}
Di

Solution

I'm not sure how to optimize your code, but let's talk about those variable names.

Naming is hard. It's doubly hard in vb.net, because it's case insensitive. That fact makes it difficult not to step on keywords like Field. I don't like recommending this, but without a being able to use fields and field to differentiate from the keywords Fields and Field, you could de-vowel it to flds and fld and still have a variable name that's easier to understand than fd.

dt I get, but what does that date actually represent?

s1,s2,s3? What do those mean? I vaguely understand what s1 is, but then I hit this.

Dim s2 As TimeSpan = TimeSpan.Parse(fd.sg)
Dim s3 As TimeSpan = TimeSpan.Parse(fd.os)


Field.sg and Field.os? Why do we have a stargate address and an operating system stored in those fields?

TL;DR: Stop over abbreviating variable names. They make sense to you (now), but will they make sense to Mr. Maintainer? Remember, that poor confused maintainer could be a future version of yourself.

Code Snippets

Dim s2 As TimeSpan = TimeSpan.Parse(fd.sg)
Dim s3 As TimeSpan = TimeSpan.Parse(fd.os)

Context

StackExchange Code Review Q#55385, answer score: 15

Revisions (0)

No revisions yet.