A
A
Alexander Buliterov2017-06-16 15:52:45
Algorithms
Alexander Buliterov, 2017-06-16 15:52:45

How to write and handle functions with multiple result options?

Good afternoon!
Task: You need to make several checks on several parameters. And depending on the result, perform the appropriate actions. Let's say N possible results of checks and, accordingly, N actions.
Now the code (check function) in C has several return results, where return 0 - everything is fine, return 1..(N-1) - types of check failures. Parameters in the function are checked one by one (nested if-s) and the result of checking for true and false is different. Checks fit in one if.
The result of the function is processed by the switch.
For convenience, the return type of the function is of type enum, but that's not important.
But for some reason I don't like this approach (using switch and several results)! Probably because the result of the function is used to either 0 - everything is fine, or! 0 - it means bad.
Attention a question: whether such approach of processing of results of function normal? Could there be another "beautiful" approach to solving this problem?
The ultimate goal of the question is to write beautiful code))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Buliterov, 2017-06-19
@bullitufa

And another question: is it worth using enum as function return? In my opinion, the result is much more readable, say RES_OK than 0!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question