What do you call nested If...Then search loops?

  • Thread starter Thread starter GY2
  • Start date Start date
G

GY2

I writing some documentation and I want to describe a common code structure
which is used to step through all the items in a collection (e.g. each file
in a subdirectory) while applying more and more restrictive filters so that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
 
Ugly? :-)

I would guess it would be called "Short Circuit Evaluation via Nested
If Then blocks"

Thanks,

Seth Rowe
 
GY2 said:
I writing some documentation and I want to describe a common code structure
which is used to step through all the items in a collection (e.g. each file
in a subdirectory) while applying more and more restrictive filters so that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
This method is so obvious and common it must have a name. What is it or at least, what
is the best (short) way to describe it?

It is usually called a (SELECT) CASE structure. For those who do not
use CASE, it is called "Nested-IFs", and also "Bad Programming".

B.
 
Thanks for the 'Nested-Ifs' language.

Brian Tkatch said:
GY2 said:
I writing some documentation and I want to describe a common code
structure
which is used to step through all the items in a collection (e.g. each
file
in a subdirectory) while applying more and more restrictive filters so
that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is
the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
This method is so obvious and common it must have a name. What is it or
at least, what
is the best (short) way to describe it?

It is usually called a (SELECT) CASE structure. For those who do not
use CASE, it is called "Nested-IFs", and also "Bad Programming".

B.
 
Thanks. I like the 'short circuit evaluation' languague.

And btw, it often times doesn't look ugly at all, in fact the indented
structure makes it quite easy to grasp and to explain. It also doesn't run
too ugly either if the successive tests are ordered correctly. In fact it
can be pretty efficient because it minimizes the number of tests needed to
filter down to whatever one is seeking.

rowe_newsgroups said:
Ugly? :-)

I would guess it would be called "Short Circuit Evaluation via Nested
If Then blocks"

Thanks,

Seth Rowe

I writing some documentation and I want to describe a common code
structure
which is used to step through all the items in a collection (e.g. each
file
in a subdirectory) while applying more and more restrictive filters so
that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is
the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
 
The other two posters who replied are being hyper-critical. There is a place
for nested if's. And, contrary to what one of them said, 'Select Case' is
not a general purpose replacement for nested if's.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
Thanks for the support, David. Yes, obviously Case statments would not work
for the multi-layered filtering I'm trying to document, but not receiving
perfect replies is no reason not to ask the question. It's good to be able
to receive something sent with a sense of humor in the same way. I thought
the one word reply--Ugly--was pretty funny.

David Anton said:
The other two posters who replied are being hyper-critical. There is a
place
for nested if's. And, contrary to what one of them said, 'Select Case' is
not a general purpose replacement for nested if's.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


GY2 said:
I writing some documentation and I want to describe a common code
structure
which is used to step through all the items in a collection (e.g. each
file
in a subdirectory) while applying more and more restrictive filters so
that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is
the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
 
In the interest of not confusing GY2 even more... it is never called a
(SELECT) CASE structure. :-) All the tests need to be performed to
determine if [Found it] is reached. I'm not sure there is any term for this
except NESTED IF's. You could write it as a compound IF statement as well.

If [test 1] AndAlso [test 2] AndAlso [test 3] Then
[Found it]
End If

What matters most is what your tests consist of. If there are one or two
simple tests the compound form works fine. If there are a few more or they
are a bit more complicated then the nested format works. But if the tests
get "crazy" move all the comparison code into a method with a name that
identifies the set of tests like IsValidCustomer() and call that method from
within your For Each loop. Any overhead incurred by a method call is
outweighed by the advantage of skipping over the details when reading the
code and that the darn tests now have a name.

Oh and (when possible) place the most restrictive (i.e. unlikely to pass)
tests first. If earlier tests fail others don't have to execute.


Brian Tkatch said:
GY2 said:
I writing some documentation and I want to describe a common code
structure
which is used to step through all the items in a collection (e.g. each
file
in a subdirectory) while applying more and more restrictive filters so
that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is
the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
This method is so obvious and common it must have a name. What is it or
at least, what
is the best (short) way to describe it?

It is usually called a (SELECT) CASE structure. For those who do not
use CASE, it is called "Nested-IFs", and also "Bad Programming".

B.
 
David said:
The other two posters who replied are being hyper-critical.

Actually, i answered the question. I also added a comment, because it
cannot be stressed enough. In most cases of nested-ifs, a CASE
statement should be used.
There is a place for nested if's.

It is rare. Besides, there are limits on how nested an IF can be. And,
it is *very* hard to follow in real code.
And, contrary to what one of them said, 'Select Case' is not a general purpose replacement for nested if's.

Yes it is. It also makes the code more clear. It is a rare case where
CASE cannot be used.

---

It could be that i'm so senstivie about this right now becaise i'm
fixing someone's code who indeed used nested IFs where CASE would
clearly be the choice. And since there are no comments, it makes
understanding the code twice as hard.

B.
 
We'll have to agree to disagree then. I find 'Select Case' more appropriate
where you care about many possible values of an expression.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
Thanks, Tom, but actually I'm not confused at all. I've been writing code
for 35 years and understand the concepts involved and now it looks as though
I may have answered my own question reasonably well in my original Subject
line. I really just wondered if this type of very common, practical, and
useful algorithm had a name these days.

Of course you're exactly right that a Select Case structure is completely
inadequate for this situation in which >ALL< tests need to be performed in
order to run all the filters.

And you also see that, of course, the way to make this most efficient is to
start with the most restrictive tests and step into the successively less
restrictive filters with each deeper nested If-Then.

Tom Leylan said:
In the interest of not confusing GY2 even more... it is never called a
(SELECT) CASE structure. :-) All the tests need to be performed to
determine if [Found it] is reached. I'm not sure there is any term for
this except NESTED IF's. You could write it as a compound IF statement as
well.

If [test 1] AndAlso [test 2] AndAlso [test 3] Then
[Found it]
End If

What matters most is what your tests consist of. If there are one or two
simple tests the compound form works fine. If there are a few more or
they are a bit more complicated then the nested format works. But if the
tests get "crazy" move all the comparison code into a method with a name
that identifies the set of tests like IsValidCustomer() and call that
method from within your For Each loop. Any overhead incurred by a method
call is outweighed by the advantage of skipping over the details when
reading the code and that the darn tests now have a name.

Oh and (when possible) place the most restrictive (i.e. unlikely to pass)
tests first. If earlier tests fail others don't have to execute.


Brian Tkatch said:
GY2 said:
I writing some documentation and I want to describe a common code
structure
which is used to step through all the items in a collection (e.g. each
file
in a subdirectory) while applying more and more restrictive filters so
that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is
the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
This method is so obvious and common it must have a name. What is it or
at least, what
is the best (short) way to describe it?

It is usually called a (SELECT) CASE structure. For those who do not
use CASE, it is called "Nested-IFs", and also "Bad Programming".

B.
 
The other two posters who replied are being hyper-critical.

Hey be nice! (joking) I did answer the post and put why nested if's are
important (short circuit evaluation).

The reason I put "Ugly" was more to emphasize how nested if's can
easily become giant scary beasts if used incorrectely. In it's pure
form (like in the example) this structure is very easy to read and
maintain. A lot of times programmers (new ones especially) will add way
to much stuff to the if then tests - like multiple exit points, etc. -
and cause the structure to become confusing and unmaintainable. I'm
sure with your experience you know what I'm talking about :-)
Now-a-days you could even replace the pure if-then structures with just
AndAlso statements if you wanted to.

Am I making more sense now?

Thanks,

Seth Rowe


David said:
The other two posters who replied are being hyper-critical. There is a place
for nested if's. And, contrary to what one of them said, 'Select Case' is
not a general purpose replacement for nested if's.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


GY2 said:
I writing some documentation and I want to describe a common code structure
which is used to step through all the items in a collection (e.g. each file
in a subdirectory) while applying more and more restrictive filters so that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
 
Sure you're making sense now, as you have all along--it's all good. I'm
working with old VB 6 code that's already a done deal. All I wanted to know
was what the hell to call those kinds of structures.

rowe_newsgroups said:
The other two posters who replied are being hyper-critical.

Hey be nice! (joking) I did answer the post and put why nested if's are
important (short circuit evaluation).

The reason I put "Ugly" was more to emphasize how nested if's can
easily become giant scary beasts if used incorrectely. In it's pure
form (like in the example) this structure is very easy to read and
maintain. A lot of times programmers (new ones especially) will add way
to much stuff to the if then tests - like multiple exit points, etc. -
and cause the structure to become confusing and unmaintainable. I'm
sure with your experience you know what I'm talking about :-)
Now-a-days you could even replace the pure if-then structures with just
AndAlso statements if you wanted to.

Am I making more sense now?

Thanks,

Seth Rowe


David said:
The other two posters who replied are being hyper-critical. There is a
place
for nested if's. And, contrary to what one of them said, 'Select Case'
is
not a general purpose replacement for nested if's.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


GY2 said:
I writing some documentation and I want to describe a common code
structure
which is used to step through all the items in a collection (e.g. each
file
in a subdirectory) while applying more and more restrictive filters so
that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is
the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
 
GY2,

I always call it a nested If's inside a loop. As simple as you wrote it.

By the way about the comments, I assume that this is the most simple sample.
In any case nobody would use it like your sample of course.

You see that this is very well documentative when you see how ugly this is
mosly in all from C derived languages.

Cor
 
Thanks Cor. Yes, the format in VB can be pretty nice if you're into
'literate programming'.

Cor Ligthert said:
GY2,

I always call it a nested If's inside a loop. As simple as you wrote it.

By the way about the comments, I assume that this is the most simple
sample.
In any case nobody would use it like your sample of course.

You see that this is very well documentative when you see how ugly this is
mosly in all from C derived languages.

Cor

GY2 said:
I writing some documentation and I want to describe a common code
structure which is used to step through all the items in a collection
(e.g. each file in a subdirectory) while applying more and more
restrictive filters so that only the desired items can fall all the way
through. This method is so obvious and common it must have a name. What is
it or at least, what is the best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
 
David said:
We'll have to agree to disagree then. I find 'Select Case' more appropriate
where you care about many possible values of an expression.

Yes, which is the case in the OPs given example.

B.
 
Just to add another voice to the cacophony,
sometimes you just HAVE to use nested Ifs.

But with VB2005, you can also use the new
OrElse and put all of those together.
Like C#'s usage of Else, it will stop
evaluating options when it hits a false one.

For Each [file or whatever] In [some collection]
If [test 1] _
OrElse [test 2] _
OrElse [test 3] Then
[Found it]
End If
Next

Of course, this assumes you don't care which test
it passed.

Robin S.
 
Please re-read the original post. The OP example shows 3 independent tests,
not 3 possible values of an expression.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
Exactly.

David Anton said:
Please re-read the original post. The OP example shows 3 independent
tests,
not 3 possible values of an expression.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
David said:
Please re-read the original post. The OP example shows 3 independent tests,
not 3 possible values of an expression.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


I did re-read it before i replied:

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next

In this case there's only one "Found it". The assumption is that the
elses will feed the other cases. And that is perfect for a CASE
statement.

B.
 
Back
Top