Oddbean new post about | logout
 Stop using recursive functions, please. 
 Stop using recursive functions, please. 
 Stop using recursive functions, please. 
 Skill issue 
 Stop using recursive functions, please. 
 sometimes are necessary  
 Yes, with some tree structures, for example. 
 iterator enjoyer here

#golang doesn't have tail recursion so it's not a good idea for performance reasons and the usual places you use recursion are short, tight loops which is where the performance hit is gonna be greater

most functional languages and some OOP languages optimize with tail recursion but it's really overrated

if i see it's gonna call itself more than a few dozen times it's gonna be performance issue for chewing up stacks and triggering heap allocations

anyway, tail recursion, in the machine code, practically is the same as a for loop without a condition anyway 
 Yes, that's my understanding, as well. 
 you can construct tree walks with iterators as well, they aren't as elegant, but fuck elegant

simple and efficient matters more

sometimes elegant isn't either of these 
 yeah, as a rule of thumb, if I can write a function in an iterative way, I do.

But sometimes it's just not possible  
 i had one time i wrote an algorithm with recursion but it was for descending a configuration settings tree... it never got to more than a few levels of depth, and each level was an iterator itself

graphs are very problematic in this respect... but i think a performant iterative walker could be written 
 One function I had to build iterative is about building the network graph on Nostr.


You start with an npub, let's say fiatjaf, then you fetch all the people he follows. Calling this set N1.

Then you fetch all people followed by N1 and you get N2.

If you want to have a parameter `depth` that you can set, you have to use a recursive function.

it was painful to debug, which is another problem with recursion 
 What's a function? I write machine code.