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
on failure.
2) some return 0
on success and -1
on failure.
3) some return 1 on success and 0 on failure (I generally spurn using a boolean type).
4) pointers return 0
on failure (I generally spurn using NULL
).
My confusion arises over the first three -- functions that return pointers always return 0
on failure, that's easy.
The first option usually involves functions which return a length which may only be positive.
The second option, is usually involved with command line processing functions, but I'm unsure it has correctness, perhaps better values would be EXIT_SUCCESS and EXIT_FAILURE?
The third option is intended for functions which are convenient and natural to be called within conditions, and I usually emulate a boolean type here, by using int
values 1 and 0.
Despite this all seeming reasonably sensible, I still find areas where this is not so clear or obvious as to which style to use when I create the function, or which style is in use when I wish to use it.
So how can I add clarity to my approach when deciding upon return types here?