What is a good general approach for deciding return values in C?

后端 未结 8 1702
庸人自扰
庸人自扰 2021-01-17 20:40

My program is written in C for Linux, and has many functions with different patterns for return values:

1) one or two return n on success and -1

相关标签:
8条回答
  • 2021-01-17 21:26

    So how can I add clarity to my approach when deciding upon return types here?

    Just the fact that you're thinking about this goes a long way. If you come up with one or two rules - or even more if they make sense (you might need more than one rule - like you mention, you might want to handle returned pointers differently than other things) I think you'll be better off than many shops.

    I personally like to have 0 returned to signal failure and non-zero to indicate success, but I don't have a strong need to hold to this. I can understand the philosophy that might want to reverse that sense so that you can return different reasons for the failure.

    The most important thing is to have guidelines that get followed. Even nicer is to have guidelines that have a documented rationale (I believe that with rationales people are more likely to follow the guidelines). Like I said, just the fact that you're thinking about these things puts you ahead of many others.

    0 讨论(0)
  • 2021-01-17 21:32

    Why don't you use the method used by the C standard library? Oh, wait...

    0 讨论(0)
提交回复
热议问题