- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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.
Admin
x, y, z, w(tf?)
Admin
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?
Admin
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.
Admin
Was that also a former colleague of mine, perchance?
Admin
Recursion makes the world go round.
Admin
Wouldn't recursion be more of a spiral?
(except optimized tail recursion of course)
Admin
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. :)
Admin
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 :-)
Admin
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.
Admin
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.