many Exits or Nested IFs - which is the better choice?

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 18619 Views - Last Post: 10 January 2012 - 06:13 AM

#1 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

many Exits or Nested IFs - which is the better choice?

Post icon  Posted 04 January 2012 - 08:06 PM

Hi guys ,

I have been wondering whether using exits multiple times or by using Nested IFs is the preferred way.
Well.. I know that nowadays developers throw Exit Sub/Function out the window and just the return keyword .

What does it impacts , performance ? Memory ?

Greatly Appreciated if some Opinions and Suggestions are given.

This post has been edited by AdamSpeight2008: 05 January 2012 - 10:00 AM

Is This A Good Question/Topic? 1
  • +

Replies To: many Exits or Nested IFs - which is the better choice?

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: many Exits or Nested IFs - which is the better choice?

Posted 04 January 2012 - 08:13 PM

I've never looked into many exits - I do use lots of nested Ifs in my code. I don't know how it affects memory - exits would be this:
If ...
Exit
End If

Nested ifs to me means this:
If...
If...
If...
End If
End If
End If

It's just the way you want to do it - at least to me
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: many Exits or Nested IFs - which is the better choice?

Posted 04 January 2012 - 08:40 PM

Could you give an example and the expectation as that will lead to a better answer for you?
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 12:37 AM

Personally I hardly ever find a place to exit whether it's a function, an if, a sub routine, or a while. I think it might come down to preference. Carefully planning your logic, and how your code should come out to get the job done efficiently, can work with both the exits or without them. I don't think the exit itself will have an effect on memory unless you have a ton of objects being instances, and variables being thrown into memory for a condition which does not present itself anymore.
Was This Post Helpful? 0
  • +
  • -

#5 TechGnome  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 04-January 12

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 08:01 AM

Yeah, I'm not a fan of the exit keyword either and try to avoid if I can. I subscribe to the single-entry, single-exit philosophy... a function/method/sub/etc has a single entry point (the sub declaration) and a single exit point (the return at the bottom) ... the only time I violate that was back in the VB6 days with error handling, where I'd exit sub/Func just before the error handler at the bottom.
Was This Post Helpful? 0
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 08:46 AM

View PostTechGnome, on 05 January 2012 - 09:01 AM, said:

Yeah, I'm not a fan of the exit keyword either and try to avoid if I can. I subscribe to the single-entry, single-exit philosophy... a function/method/sub/etc has a single entry point (the sub declaration) and a single exit point (the return at the bottom) ... the only time I violate that was back in the VB6 days with error handling, where I'd exit sub/Func just before the error handler at the bottom.


Vb6 days is where I definitely used to use it haha.
Was This Post Helpful? 0
  • +
  • -

#7 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 10:25 AM

I use the same philosophy as TechGnome most of the time. I don't use multiple return statements or exit statements because they make debugging harder. For example between these two codes, which is easier to Debug:

return x * y + a - i



dim val as integer = x * y + a - i
return val



That one extra line doesn't hurt anyone but make it much easier to debug since you will know what the function returns if you set a breakpoint there and go to the return line and read the value of variable val. In the first example you'd have to use immediate window to get the outcome which is always a bit slower.

Performance-wise you shouldn't care about performance until it becomes a problem for you. And even then the performance problem isn't most likely in that if statement, it's what's happening inside the if or before the if, like some calculation.

Also there shouldn't be any memory issues unless your if statement calls some function which causes memory problems, like this:
if IsTrue() then

endif

sub IsTrue()
  addhandler myobject.click, sub()
                               console.writeline("yay")
                             end sub()
  return true
end sub


Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 11:45 AM

Example of locking a file by a return statement:

Private Function GetFileString(filepath As String) As String
   Using sr As New StreamReader(filePath)
      Return sr.ReadToEnd
   End Using 'we never close the stream here
End Function

Was This Post Helpful? 0
  • +
  • -

#9 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 641
  • Joined: 03-February 10

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 12:12 PM

View Post_HAWK_, on 05 January 2012 - 12:45 PM, said:

Example of locking a file by a return statement:

Private Function GetFileString(filepath As String) As String
   Using sr As New StreamReader(filePath)
      Return sr.ReadToEnd
   End Using 'we never close the stream here
End Function


Actually Hawk, since you are using Using the stream should be closed, since it acts as a Try...Catch...Finally.
Was This Post Helpful? 3
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 02:10 PM

What about sanity checks?

I find the multiple single line IF and easier to read then nesteed IF-Else blocks.

Function Fib( x as integer) as integer
 If x<0 then throw New OutOfBoundsException()
 if x=0 then return 0
 If x<2 then return 1
 return Fib(x-2) + Fib(x-1)
end function

Was This Post Helpful? 0
  • +
  • -

#11 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 02:35 PM

@demausdauth - your right, I thought I had tested this some time ago - as the End Using line is never executed. I guess my memory isn't what it used to be...
Was This Post Helpful? 1
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 02:45 PM

If your doing stuff inside a using statement then practice single point of exit, after the end using
Was This Post Helpful? 0
  • +
  • -

#13 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1051
  • View blog
  • Posts: 4,076
  • Joined: 02-July 08

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 05:16 PM

View PostAdamSpeight2008, on 05 January 2012 - 02:45 PM, said:

If your doing stuff inside a using statement then practice single point of exit, after the end using


Yeah, I've moved onto this approach:

Private Function GetFileString(filepath As String) As String
   Dim result As String = ""
   Using sr As New StreamReader(filePath)
      result = sr.ReadToEnd
   End Using 
   Return result
End Function

Was This Post Helpful? 0
  • +
  • -

#14 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 05:44 PM

Thanks alot guys , it was quite informative for me .

Well , the thing is I am trying to rewrite a program which was written in vb6 language . I am now currently using VB.NET to rewrite the whole program.

I cant reveal the codes here , so i created a sample of the VB6 code with multiple Exits. The Nested IF adapts a single entry and single way out with no Exit Functions or Return Keywords.

Assuming the runProcess(), runProcessC() returns a string "pass" if successfully executed else return "fail" if it fails


Multiple Exits
Function funcName(byval tmpResult as String , byval tmpBool as String ) As String

      If Not tmpResult = "pass" Then
            funcName = "Fail"
            Exit Function
        End IF

      If tmpResult = "pass" Then
          funcName  = RunProcess()

          If funcName  = "Fail" Then
             Exit Function
           End If
      End If
  
        If tmpBool = true then
            funcName = runProcessC()
           
          If funcName  = "Fail" Then
             Exit Function
           End If

       End If

  return funcName 
End Sub




Nested IF
Function funcName(byval tmpResult as String , byval tmpBool as String ) As String

      If tmpResult = "pass" Then
          funcName  = RunProcess()

          If funcName  =  "pass" Then
            
            If tmpBool = true then
                funcName = runProcessC()
             End IF

          End If

      Else

       If Not tmpResult = "pass" Then
            funcName = "Fail"
       End If       

      End If

    return funcName 
End Sub



The code above only shows a few conditions whereas the real application has even more conditions. The thing is I am afraid the Readability of the code becomes harder and messier when more conditions were added to the nested if .
Was This Post Helpful? 0
  • +
  • -

#15 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: many Exits or Nested IFs - which is the better choice?

Posted 05 January 2012 - 06:06 PM

The code does look messier - you're evaluating more conditions in one block of code which will inevitably affect readability as it grows in size and length.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2