r/ProgrammerHumor • • Oct 13 '26

Meme howProgrammingHasChanged

Post image
673 Upvotes

108 comments sorted by

View all comments

Show parent comment

171

u/temperamentalfish Oct 14 '26

Yeah I'm cringing at all the recursive solutions. Huge stack and so much repeated work. Just try counting how many times fib(5) would be executed to calculate fib(10). It's so often the go-to example for recursion and it's frankly terrible.

126

u/robin_888 Oct 14 '26

It's supposed to teach the concept of recursion, which is important, but hard to grasp for some.

Discussing performance and optimizations can happen if the principal is clear.

49

u/temperamentalfish Oct 14 '26

I know, but there are so many better examples. Mergesort, for instance. It shows why the concept is important because it breaks the problem into smaller, more manageable problems, without doing repeated work.

Fibonacci should be taught as an example of when a problem can be solved recursively, but shouldn't.

26

u/Silly_Guidance_8871 Oct 14 '26

When I was studying, Fibonacci was used a lot to teach the value of dynamic programming. But agreed, it's a terrible example of when to use recursion.

21

u/fdar Oct 14 '26

DP is just recursion+caching. 

I think it makes sense to first recognize that the problem can be solved with recursion and then decide if DP is needed to improve performance.

5

u/Bliztle Oct 14 '26

Only if you're doing it top down. Bottom up is usually taught at the same time, and is useful to lessen stack and cache size.

6

u/robin_888 Oct 14 '26

I think in education it is also important to take "wrong" approaches to learn why they are "wrong" and what possible solutions are. If you just teach the "good" examples students might not realize why the naive approach to Fibonacci is problematic.

In other words you might want to teach naive recursion before you teach tail-recursion.

3

u/TheThiefMaster Oct 14 '26

Tree iteration is pretty much the only time it's required, and even then you can linearise it if you have back links.

4

u/Mitchman05 Oct 14 '26

Teach Tower of Hanoi as example of recursion, that one actually requires it and can't be done faster

3

u/BitTwiddles Oct 15 '26

Write it in Erlang with tail recursion. In fact fib is the common demonstration case for recursion.