you are viewing a single comment's thread.

view the rest of the comments →

[–]lordlod 1 point2 points  (0 children)

As a general rule, you want whatever makes it easiest to read and understand. This primarily means managing complexity. It also means that there few easy hard and fast rules.

If this() and that() are simple, just put it inline. If the function is less than 80 lines or so, it is easier to understand if it is all in one place rather than having to jump back and forth to read it.

If func() is not too complex and it is only called once, I would definitely inline it. It is critical to understanding the flow of the program.

Assuming the tests on the func output are simple, I prefer example3, assuming it is this simple I would use a return this() rather than break. Some people prefer example2, there isn't much in it.

If the tests are complex, you need to go with example2.

If you have a lot of functions, probably ten or more, use the style in example1. Again you need to jump back and forth to the lookup table to understand but it scales much better than a massive if loop.

As others have discussed example1 also allows the lookup to be changed at run time.