News for hackers. Stuff that matters.
Re:All About Recursion and Tail Calls in JavaScript by xg15 on Saturday May 13, 2017 @07:19PM (#14333483)
What I don't understand with STCs is, how would I, as a developer, decide when to use them and when not?

As program correctness is concerned, tail calls and recursive calls should behave exactly the same (except stack use), so going by correctness alone, either the choice doesn't matter at all or tail calls are preferable. So the logical thing to do would be to always use tail calls.

The post goes on to list a number of disadvantages, but those seem to be either debugging concerns or platform-specific implementation concerns.

The former doesn't seem to be something that should be solved in the code - the document itself lists a number of solutions. The latter is not something that I as developer could judge as I likely don't have knowledge about the specific implementation of the platform the code is running on (if I know the platform in advance at all)

So it seems to me, STC shifts the problem of deciding to the developer even though the developer has no good tools to actually solve it.

Re:All About Recursion and Tail Calls in JavaScript by mikekchar on Saturday May 13, 2017 @08:27PM (#14333652)
I think that TCO (and similar optimisations) are a bane to most junior developers. I vividly remember pulling my hair out when trying to single step through optimised C++ code early in my career. But in my experience, all good developers come to this point where they realise -- Hey, I don't need to single step through this code. I can read it and understand what it is supposed to do. These days, with the popularity of techniques like unit testing, debuggers are not really necessary. I've spent the last 4 1/2 years writing ruby and JS code and I don't even know how to use the various debuggers -- never needed to.

So I'm with you. Just give me TCO every single time -- or give it to me never. Optimising it by hand is not that hard, and it's what most non-functional programmers do most of the time anyway. You'll see loops where recursion would be clearer, but then when you look at it hard you'll realise that it's just the TCO optimised version of the same code.

Re:All About Recursion and Tail Calls in JavaScript by ecma_horse on Saturday May 13, 2017 @08:51PM (#14333705)
You absolutely should have knowledge of your target platform and whether it does TCO (and whether it kicks in for your code).

Otherwise, you may decide to just use recursion everywhere and then your stack overflows when your N is bigger than expected.

When in doubt, don't use recursion and don't expect that you get TCO.

Re:All About Recursion and Tail Calls in JavaScript by xg15 on Sunday May 14, 2017 @06:31AM (#14335031)
> You absolutely should have knowledge of your target platform and whether it does TCO

If I write web apps, as far as I'm concerned, my platform is V8, Whatevermonkey, Trident and every other hypothetical​ javascript engine out there, any of which might or might not support TCO under different circumstances.

> When in doubt, don't use recursion and don't expect that you get TCO.

I think relying on TCO is always a bad idea, exactly because you can't be sure your platform supports it. But STC doesn't change that. You can't expect the platform to support tail calls because you did some magic incantations.

On the other hand, it doesn't make sense to me to forbid tail calls either. If you know your recursive code will work without TCO, it will work with TCO, too.