Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 565335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:51:40+00:00 2026-05-13T12:51:40+00:00

I came across the following code recently and would like to optimize it: Public

  • 0

I came across the following code recently and would like to optimize it:

Public Shared Function ComputeLabel(ByVal Action As Integer, ByVal Flag1 As Boolean, ByVal Flag2 As Boolean) As String
   Dim Prefix As String = ""

   If Flag2 Then
      Prefix = "Conditional "
   ElseIf Flag1 Then
      Prefix = "Held "
   End If

   Select Case Action
      Case 0
         Return ""
      Case 1
         Return Prefix & "Cancelled"
      Case 2
         Return Prefix & "Discontinued"
      Case 3
         Return Prefix & "Suspended"
      Case 4
         Return Prefix & "Unsuspended"
      Case 6
         Return Prefix & "Collected"
      Case 7
         Return Prefix & "Released from Hold"
      Case 8
         Return Prefix & "Modified"
      Case 9
         Return Prefix & "Discontinued for the Future"
      Case 10
         Return Prefix & "Verified"
      Case 11
         Return Prefix & "Modified And Verified"
      Case 12
         Return "Hold " & Prefix & "Cancelled"
      Case Else
         Return ""
   End Select
End Function

Note that Action 0 is the most common case.

Okay, I’ve already cleaned up this function a bit–it was using a variable and returning it at the end, and using Return seems better. But additionally, I think it would be better code to build an array at the beginning of the report execution, and then just access array elements each time this function is called, instead of using a Select statement. But case 12 is making things more complicated (as you can see, it adds the prefix in the middle instead of at the beginning.)

What do you think would be the best way:

  • One time building a 39-element array for the three cases:

    Private Shared OrderActions() As String = {"", "Cancelled", ...}
    

    Then in the function accessing it like so:

    If Action < 0 OrElse Action >= 13 Then Return ""
    Return OrderActions(Action - Flag2 * 13 - (Flag1 AndAlso Not Flag2) * 26)
    
  • Using a 13-element array with a Replace (something like Return Replace(LabelList(Action), "{Prefix}", Prefix)?)

  • Using a 12-element array with a special case for Action 12.

  • Something else I haven’t thought of.

?

Update 1: my formatting was off so the options might have been unclear. It should be more readable now.

Update 2: I see what you mean that from a performance perspective, fully expanding all the cases and using simple variable assignment is probably fastest. So… let’s say top speed is not the priority, but overall elegance is (a combination of clean code and speed). Any chance people could give their take on that, too? I’ll vote everyone up who gives reasonable help for all aspects of the question.

Update 3: One additional consideration I was ignoring is that some non-experienced programmers are going to be maintaining this long-term, so it does need to be easy to understand. I guess my examples of trying to shorten the code really aren’t good from this perspective.

Update 4: TESTING IS KING OVER ALL!!! Once I was inspired to do some speed tests, I got some interesting results. See my answer below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:51:41+00:00Added an answer on May 13, 2026 at 12:51 pm

    actually, if you put them into arrays, it brings the execution time from 6589ms on my machine to 1174ms using the following method:

    Below is from a console app, but you get the general idea. The arrays are loaded once, then accessed as many times as you want, in this case I was using the for loop that Steve Wortham posted as a test.

    Dim _ConditionalLabels(12) As String
    Dim _HeldLabels(12) As String
    
    Sub Main()
        Dim sw As New Stopwatch()
        sw.Start()
    
        _ConditionalLabels(0) = ""
        _ConditionalLabels(1) = "Conditional Cancelled"
        _ConditionalLabels(2) = "Conditional Discontinued"
        _ConditionalLabels(3) = "Conditional Suspended"
        _ConditionalLabels(4) = "Conditional Unsuspended"
        _ConditionalLabels(6) = "Conditional Collected"
        _ConditionalLabels(7) = "Conditional Released from Hold"
        _ConditionalLabels(8) = "Conditional Modified"
        _ConditionalLabels(9) = "Conditional Discontinued for the Future"
        _ConditionalLabels(10) = "Conditional Verified"
        _ConditionalLabels(11) = "Conditional Modified And Verified"
        _ConditionalLabels(12) = "Hold Conditional Cancelled"
    
        _HeldLabels(0) = ""
        _HeldLabels(1) = "Held Cancelled"
        _HeldLabels(2) = "Held Discontinued"
        _HeldLabels(3) = "Held Suspended"
        _HeldLabels(4) = "Held Unsuspended"
        _HeldLabels(6) = "Held Collected"
        _HeldLabels(7) = "Held Released from Hold"
        _HeldLabels(8) = "Held Modified"
        _HeldLabels(9) = "Held Discontinued for the Future"
        _HeldLabels(10) = "Held Verified"
        _HeldLabels(11) = "Held Modified And Verified"
        _HeldLabels(12) = "Hold Held Cancelled"
    
        Dim str As String = ""
        For i As Integer = 0 To 10000000
            str = ComputeLabel(0, True, False)
            str = ComputeLabel(1, False, False)
            str = ComputeLabel(0, False, False)
            str = ComputeLabel(2, False, False)
            str = ComputeLabel(1, False, True)
            str = ComputeLabel(2, True, True)
            str = ComputeLabel(4, False, True)
            str = ComputeLabel(7, True, True)
            str = ComputeLabel(12, False, True)
        Next
    
        sw.Stop()
        Console.WriteLine(sw.ElapsedMilliseconds & " ms")
        Console.Read()
    End Sub
    
    Public Function ComputeLabel(ByVal Action As Integer, ByVal Held As Boolean, ByVal Conditional As Boolean) As String
        If Conditional Then
            Return _ConditionalLabels(Action)
        ElseIf Held Then
            Return _HeldLabels(Action)
        End If
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.