Recursive method calling (is it wrong?)
-
I have a search method that calls it's self until a condition is true. Is this wrong? private void TriggerSearch() { *** other misc code *** if(!blnTriggerFileWritten) { TriggerSearch(); } } Thanks. www.lovethosetrains.com
-
I have a search method that calls it's self until a condition is true. Is this wrong? private void TriggerSearch() { *** other misc code *** if(!blnTriggerFileWritten) { TriggerSearch(); } } Thanks. www.lovethosetrains.com
It's not wrong, but could cause deadlocks if the condition is never true. Also, recursive functions are less efficient than a while loop because each function call in a recursive function must be added to a method call stack, whereas while loops do not. while(blnTriggerFileWritten == false) { blnTriggerFileWritten = ...; }
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Who is Jewish, the Trivia Game! Judah Himango
-
I have a search method that calls it's self until a condition is true. Is this wrong? private void TriggerSearch() { *** other misc code *** if(!blnTriggerFileWritten) { TriggerSearch(); } } Thanks. www.lovethosetrains.com
It depends on what you're trying to do. The most readable version of a binary search is recursive. You should however be aware that there's overhead involved in the additional calls and that as a result the recursive version will take longer to execute than the nonrecursive one. If performance isn't an issue however the recrusive version will be easier to understand and maintain.
-
I have a search method that calls it's self until a condition is true. Is this wrong? private void TriggerSearch() { *** other misc code *** if(!blnTriggerFileWritten) { TriggerSearch(); } } Thanks. www.lovethosetrains.com
Thanks for the input. Was working a 'C' port and the other programmer was surprised that I could call my function from within my function. I will relook at the code and probably impliment your suggestions. Thanks again. www.lovethosetrains.com