2

This link looks very practical and "hands in the dirty". But in the example of Python code for node discovery and connections, it often gives warning about the fact it is of "educational purposes" and not real world.

Could you please explain what the caveats of the implementation showed in the link are, and how to correct them in the real world:

Example of such limitations highlighted:

Example of code

Is the limitation about the fact that "except Exception" excepts nearly everything and that is not a good practice? Would putting a security (like don't call recursively indefinitely) be a solution?

3 Answers 3

2

It's bad practice to use recursion when a while loop would suffice. Recursive code is harder to read, harder to debug, harder for the compiler to optimize and it may cause a stack overflow.

Besides, connections shouldn't be made from the main thread.

EDIT: Maybe it's because there's no limit of trials after which the code should assume that the user went offline. Otherwise the stack will grow and if the stack doesn't overflow the iterator may go out-of-bounds.

0
1

Using recursion is not a bad practice IMO, they can be used to do lot of interesting things although I am not the best person to comment on such things.

There are few interesting comments in this thread about recursion vs while loop: https://www.reddit.com/r/csharp/comments/rnoy7h/difference_between_tail_recursion_and_while_loop/

As far as the code is concerned, I could not find a line that would stop recursion at some point so adding an if statement with some condition to terminate could make it usable in production.

0

Is the limitation about the fact that "except Exception" excepts nearly everything and that is not a good practice?

I don't think "except Exception" is bad practice necessarily. Maybe recursing immediately on it happening, like this snippet, is.

Would putting a security (like don't call recursively indefinitely) be a solution?

Yes, but such security would not be a "don't call recursively indefinitely" if Python supported tail call optimization. You can mind that if you see a snippet like this in a different programming language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.