this post was submitted on 11 Nov 2025
586 points (97.6% liked)

Programmer Humor

27341 readers
1105 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] AllHailTheSheep@sh.itjust.works 3 points 5 days ago* (last edited 5 days ago) (2 children)

I think modern compilers do actually compile recursion to be equivalent with an iterative model.

edit: yes when possible they will compile to be iterative, but if it can't be written iteratively it will be a series of calls and returns. depends on the specific type of recursion it looks like.

[–] Buddahriffic@lemmy.world 3 points 5 days ago (1 children)

At one point I developed a habit of converting any recursive algorithm I was writing into a loop instead, since I knew function calls have overhead and all recursion really does is lets you use the calling stack and flow control as an invisible data structure.

Then I got a question about parsing brackets properly during an interview and wrote a loop-based parser to solve it and the guy had to fish for a bit before I remembered recursion and realized that's the answer he was looking for. My mind just wouldn't consider using a whole calling stack when an integer would do the trick faster.

[–] CrypticCoffee@lemmy.ml 4 points 4 days ago* (last edited 4 days ago) (1 children)

Most times overhead is less important than readability and maintainability of code. If someone cannot read your code, they'll mess it up far worse.

Optimisation is for bottlenecks. No point making code run in 0.01ms rather than 0.02 if it later hits at 0.7s bottleneck like file io or DB. For most things, readability is everything unless you're developing operating systems or close to metal libraries. Many compilers will inline functions anyway so the only gain is increased suffering of colleagues and later bugs in production when it's modified by someone else. Cognitive load is very important and why many static code analysis tools pick it up.

[–] Buddahriffic@lemmy.world 2 points 4 days ago

One has a function that takes the next node as an argument, another just sets that up in a loop. Personally, I found the loop one a bit easier to visualize and debug via stepping through code, though admittedly the difference isn't huge.

[–] ryannathans@aussie.zone 2 points 5 days ago* (last edited 5 days ago)

I think you're talking about tail recursion

https://en.wikipedia.org/wiki/Tail_call (see section on equivalence to while statement)

Not the standard case where the stack grows