Clever For-loop
-
I used to work with a guy who was clever enough to write this:
For i = 1 To 5
If i = 1 Then
...
Else If i = 2 Then
...
Else If i = 3 Then
...
Else If i = 4 Then
...
Else If i = 5 Then
...
End If
Next iI guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)
-
I used to work with a guy who was clever enough to write this:
For i = 1 To 5
If i = 1 Then
...
Else If i = 2 Then
...
Else If i = 3 Then
...
Else If i = 4 Then
...
Else If i = 5 Then
...
End If
Next iI guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)
Maybe there is a i++ in some states in the if blocks to skip steps?
James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
Mitch Hedberg -
Maybe there is a i++ in some states in the if blocks to skip steps?
James Simpson Web Developer imebgo@hotmail.com P S - This is what part of the alphabet would look like if Q and R were eliminated
Mitch HedbergNope, the i did nothing else ;)
-
I used to work with a guy who was clever enough to write this:
For i = 1 To 5
If i = 1 Then
...
Else If i = 2 Then
...
Else If i = 3 Then
...
Else If i = 4 Then
...
Else If i = 5 Then
...
End If
Next iI guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)
-
I used to work with a guy who was clever enough to write this:
For i = 1 To 5
If i = 1 Then
...
Else If i = 2 Then
...
Else If i = 3 Then
...
Else If i = 4 Then
...
Else If i = 5 Then
...
End If
Next iI guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)
While a select-case statement would probably be better than the cascaded if/else-if, the illustrated construct may not be unreasonable in certain cases where it is expected that the inside of the loop will be pulled out into a separate routine for use as a state machine. Having the code within a for-next loop may be handy during development while the code is being perfected, even if the final construct will be something else. To be sure, when running on a PC it's often easier to use threads rather than state machines, but state machines can often have a much lower 'footprint'. If an application will require hundreds or thousands of objects each with a multi-way state machine, and all have to be operable concurrently, a select-case state machine may be a reasonable approach. Delegates should be considered as well, since they have some advantages (but also some disadvantages), but state machines are reasonable in simple cases.
-
I used to work with a guy who was clever enough to write this:
For i = 1 To 5
If i = 1 Then
...
Else If i = 2 Then
...
Else If i = 3 Then
...
Else If i = 4 Then
...
Else If i = 5 Then
...
End If
Next iI guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)
Take brain – Turn off brain – Start coding. :doh:
yvind Bratland wrote:
We don't work together anymore
Surprise! Surprise! :laugh:
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
While a select-case statement would probably be better than the cascaded if/else-if, the illustrated construct may not be unreasonable in certain cases where it is expected that the inside of the loop will be pulled out into a separate routine for use as a state machine. Having the code within a for-next loop may be handy during development while the code is being perfected, even if the final construct will be something else. To be sure, when running on a PC it's often easier to use threads rather than state machines, but state machines can often have a much lower 'footprint'. If an application will require hundreds or thousands of objects each with a multi-way state machine, and all have to be operable concurrently, a select-case state machine may be a reasonable approach. Delegates should be considered as well, since they have some advantages (but also some disadvantages), but state machines are reasonable in simple cases.
Wow... Just wow... Where in the original WTF did it mention a problem that would require State Machines or Threads to solve? You've created a mountain out of a grain of dirt.
------------------------------- Carrier Bags - 21st Century Tumbleweed.
-
Wow... Just wow... Where in the original WTF did it mention a problem that would require State Machines or Threads to solve? You've created a mountain out of a grain of dirt.
------------------------------- Carrier Bags - 21st Century Tumbleweed.
Tristan Rhodes wrote:
Where in the original WTF did it mention a problem that would require State Machines or Threads to solve?
My point was that there are circumstances where code like that illustrated may be useful. While it is true that there was nothing to indicate that such circumstances applied here, I would not wish to condemn code without knowing that such circumstances did not apply. BTW, another circumstance where such code may be useful is in cases where the loop will evolve into something like:
for i=1 to 5
.. do some stuff in every case
if i=1 then
.. handle first case
else if i=2 then
.. handle second case
... etc
end if
.. do some more stuff in every caseIn many circumstances, that style of code could be handled better by putting the different parts into subroutines and just calling them:
everycase_prep()
.. handle first case
everycase_cleanup()
everycase_prep()
.. handle second case
everycase_cleanup()In some cases, however, the prep and cleanup would need to share many local variables, thus requiring either long parameter lists or awkward class constructs.
-
I used to work with a guy who was clever enough to write this:
For i = 1 To 5
If i = 1 Then
...
Else If i = 2 Then
...
Else If i = 3 Then
...
Else If i = 4 Then
...
Else If i = 5 Then
...
End If
Next iI guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)
Of course you two don't work together anymore. This guy's a visionaire! You simply cannot understand the superior maths under this construction. :-D
Kazz
"Users are there to click on things, not think. Let the archs do the damn thinking."
-
I used to work with a guy who was clever enough to write this:
For i = 1 To 5
If i = 1 Then
...
Else If i = 2 Then
...
Else If i = 3 Then
...
Else If i = 4 Then
...
Else If i = 5 Then
...
End If
Next iI guess the coder wanted to do something "in five steps". He certainly does. We don't work together anymore ;)
yvind Bratland wrote:
We don't work together anymore
I bet that's a good thing :-D
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon