Do you declare variables when you need them?
Updated by Brady Stroud [SSW] 1 year ago. See history
123
Should you declare variables at the top of the function, or declare them when you need to use them? If you come back to your code after a few weeks and you no longer need a variable, you are quite likely to forget to delete the declaration at the top, leaving orphaned variables. Here at SSW, we believe that variables should be declared as they are needed.
Private Sub Command0_Click()Dim dteTodayDate As DateDim intRoutesPerDay As IntegerDim intRoutesToday As IntegerDim dblWorkLoadToday As DoubledblWorkLoadToday = Date.Now().....many lines of code....intRoutesPerDay = 2End Sub
❌ Figure: Figure: Bad example
Private Sub Command0_Click()Dim dteTodayDate As DatedteTodayDate = Date.Now().....many lines of code....Dim intRoutesPerDay As IntegerintRoutesPerDay = 2.....continuing code....End Sub
✅ Figure: Figure: Good example