Вопрос

Just a quick question,

What do you think is better of the two:

- A recursive function that returns a value
- A recursive function with an OUT paremeter (which will serve as a return value.)

I tried both approach and they worked out the same.

Please note that the main functionality of this function is to:

-> accept a String parameter.
-> scan through a list (whick have like 1032 items) and find a match of the string parameter above.
-> then get a specific data from the object that matched the string parameter.

Your inputs will be very much appreciated.

Это было полезно?

Решение

For me, returning a value will always be the better choice over an out parameter.
In my opinion it is more clean and elegant.
Take a look at this similar question, and the answer by John skeet.

Also, If you use FxCop, you will get a warning for using out parameters.

Другие советы

To me either approach seems fine, so it comes down to personal preference. You could basically ask the same question about methods in general - recursive or not.

A benefit of the out param is that you can support multiple parameters. However, some might say that using a return value is more readable.

Which approach, much like you found, is context dependent. Sometimes, it won't matter, and others, it will make things less complicated.

Ultimately, the context depends on what problem you are trying to solve. There are times when a recursive function is a vastly simpler way to solve a problem vs a loop-based algorithm, and vise-versa.

From what you said, it sounds like you may be better off with a loop based algorithm, unless your list of words is stored in some sort of complex tree-node structure.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top