5 ms·
Remember to keep track of the call stack in your head as you write code. The trap to avoid is to invert control and then fail to invert (return) back to the mai
by dhx 15y ago
Remember to keep track of the call stack in your head as you write code. The trap to avoid is to invert control and then fail to invert (return) back to the main program flow.
In the example referenced, the following call stack could be created:
0. authenticateUser
1. login
2. authSucceeded
If authSucceeded continues to call other functions as if it were the new main flow of control you could end up with a very deep call stack (it's not uncommon to see software with a call stack 40 calls deep). This is why I would have preferred "authSucceeded" to be renamed as "registerUserSession" or something else that screams "this function will do a well defined bit of work and _return quickly_" rather than "this is a loosely defined event callback that may do anything".
- Encryptor 15y agonot necessarily. you could implement these as function calls with no return addresses (think goto-s or JMP instructions). If you never need to return from some of those functions, there's no need to keep an entry in the call stack for them.
- chalst 15y agoIn the context of the OA's first sentence ("Why should an object-oriented programmer care about tail-call elimination?"), I think the author is thinking more of Scala and Ruby than Java, and there's no expectation that the tail call will return at all.