• (nodebb)

In many cases, recursion ends up being code smell, because people use it when they shouldn't.

The canonical example of "don't use recursion for this" is calculating the "N"th member of the Fibonacci series. There's a "natural" implementation that's recursive, and uses O(n) stack space and O(2-to-the-n) (exponential) time. Memoization makes it O(n) stack space and O(n) other-storage space and O(n-times-search-time) time, but there's a O(1) storage/O(n) time algorithm (keep track of the last two elements as you iterate), and an O(1) storage/O(1) time "simple" calculation (see https://en.wikipedia.org/wiki/Fibonacci_sequence#Computation_by_rounding).

Or the example of a function for doing something with displaying text in a text editor written (in part) and maintained (in total) by one of my many former colleagues. He let me see the source code, and I said, "Why is this function recursive?". He looked at it, and commented that it was a relic of a certain person(1) heavily involved in the creation of Java (a former colleague of his). I had to apologise later, because it took him three weeks to repair the damage done by removing the recursion.

(1) Y'all can probably guess who it is, but I'm not prepared to say it explicitly.

• Long Time Lurker (unregistered)

x, y, z, w(tf?)

• Joe (unregistered) in reply to Long Time Lurker

Probably thought 3 levels would do (x, y, z) and when they realized it wasn't enough, well, why not go backward starting at w from there?

• (nodebb)

While there are certainly cases where recursion is evil it's most likely the sane option when your data is fundamentally a tree. I can think of a couple of occasions where I have used a non-recursive approach to a tree--and that was because I needed a breadth-first traversal.

• (author) in reply to Steve_The_Cynic

Was that also a former colleague of mine, perchance?

• LZ79LRU (unregistered)

Recursion makes the world go round.

• (nodebb) in reply to LZ79LRU

Wouldn't recursion be more of a spiral?

(except optimized tail recursion of course)

• LZ79LRU (unregistered) in reply to Medinoc

I see what you mean. It took me a moment but I can absolutely visualize recursion as being a spiral in 3D space. It's quite elegant even. That image genuinely made my day. :)

• (nodebb)

Ah, I remember those days when pattern matching was not a thing and you have to switch over type names. Good old days, so much horror, so much pain, it was glorious. Then again, I wouldn't be surprised if VB.net doesn't have pattern matching yet, so hurrah for the manager language still dishing out suffering :-)

• Gavin (unregistered)

Concerning the redims, I had the misfortune to use VB6 25 years ago, and one thing that always hurt my brain was that you could have arrays of scalars or collections of objects. Scalars did not go into collections and objects did not go into arrays. Collections could grow and shrink dynamically, arrays had to be redimmed.

• brodie (unregistered)
Comment held for moderation.
• Craig (unregistered)

VBA* had (and has) exactly one collection type (Collection) which could grow on its own. If you wanted anything else, you would have needed to write it yourself (most likely using an array with ReDim Preserve behind the scenes). I vaguely recall writing something that would scale its size internally like a STL vector back in the day.

For me, the biggest thing that grates in reading the code is using strings for the type checks instead of the built-in TypeOf... Is.

• I refer to the language as VBA. VB6 was the development environment plus forms engine (plus native compiler) overlaid on the VBA language.