I recently worked on legacy vb.net code, and during a peer-to-peer code review, it was recommended not to use Exit Sub / Function, but instead to put all the functionality into IF statements.
When I initially began to develop, I did it so instinctively (Nest IF), it not only seemed more logical, but it seemed less confusing to him.
However, at some point I was working with a team that saw nested IF as “evil,” and therefore the “Exit” premise / functions that I was told were preferable. I am sure that they have prepared MS material for best practice to support this.
So this question is for experienced developers, which path is really preferable? If you give an answer, can you also indicate your sources or just mention that this preference is preferable to your team / company / personal staff and an explanation of the reasons.
Thanks in advance.
EDIT as requested: Sample code
Sub Output:
Private Sub DoSomeWork() if not conditionMetFromAnotherFunction() then exit Sub end if 'Method work starts here End Sub
Nested IFs:
Private Sub DoSomeWork() if conditionMetFromAnotherFunction() then 'Method work starts here end if End Sub
Jl.
source share