0

What's best practice with the following piece of code?

If Condition = False Then End
' Rest of my code.

OR

If Condition = True Then
    'Rest of my code.
Else
    End
End if

If Condition is not met in my code, I don't want to needlessly run all of my code. Is it better practice to use an If statement above my code to prevent it from running if Condition is not met, or is it better to use a more fleshed out If statement to handle this?

The latter piece of code doesn't technically require 'End', it just circumvents the execution of my code.

Community
  • 1
  • 1
Nicolas
  • 31
  • 4
  • 2
    That depends and is opinionated. – Scott Craner Aug 21 '17 at 13:29
  • I understand that there is no hard and fast rule, but is there no best practice regarding the issue? For instance I've read that Early Out code is good in functions, does the same apply to your main module? – Nicolas Aug 21 '17 at 13:31
  • It really is subjective, your example is too trivial to make a case either way. *Lots* of levels of nesting can be considered an [Arrow Anti-Pattern](http://wiki.c2.com/?ArrowAntiPattern) – Alex K. Aug 21 '17 at 13:35
  • 1
    I would say that using `End` on its own is generally not good practice unless you fully understand all the consequences. – Rory Aug 21 '17 at 13:58
  • 1
    @Rory I guess the OPs intention was not `End` but using `Exit Sub` or `Exit Function` instead. @Nicolas have a look at [What's the deference between “end” and “exit sub” in VBA?](https://stackoverflow.com/questions/36491908/whats-the-deference-between-end-and-exit-sub-in-vba) to understand the difference. – Pᴇʜ Aug 21 '17 at 14:05
  • Depends. If performance is very important, make sure the result that happens most frequently is in the True part of the If statement. Also, "If Condition = True Then" is inefficient, "If Condition Then" is better. – jkpieterse Aug 21 '17 at 15:32

2 Answers2

0

I would say this depends on the type of the variable Condition. This might look the same at first but only if we assume that Condition is of type Boolean which is not defined here.

  1. If the type is Boolean:
    Then it is up to you which one you choose. Both are safe and I would say this is opinion based which one you prefer.

  2. If the type is not Boolean:
    For Condition being e.g. of type Long or Integer and having a value that is not 0 (which means false) and not -1 (which means true) for example for Condition = 1 they behave differently. The …

    • first solution will run the rest of the code
    • but the second solution will end the code.
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
0

See it that way : wich condition is the most likely to happen often (true or false). And than you use this condition in your code. So when your sub\function runs it will have to act on less code.

Or said an other way: having a if condition then A else B, Be sure to place the A code first if it is happening more often than B, so your code will be faster.

Patrick Lepelletier
  • 1,596
  • 2
  • 17
  • 24