Option 1: Validating property
Return a structure struct Bundle
. If its property is_valid
is false, discard the rest of the structure’s data.
- [p] Looks very similar to a function with a single return value.
- [p] Very readable, what the function returns
- [p] Multiple invalid states can be implemented.
- [p] Addition data can to attached to an invalid bundle.
- [p] Bundle can be stored as pointer and as local variable.
- [c] Without a validation check, an invalid bundle looks similar to a valid one.
Option 2: Optional pointer
Return the reference to a new dynamically (on the heap) allocated structure struct Bundle *
. If the pointer is NULL
, no bundle was created.
- [p] Accessing properties of an invalid bundle throws an error, thus requiring a validation check.
- [c] The return value must be released later as long as it is valid.
- [c] The bundle must be saved and used as a pointer
bundle->property
Option 3: Output parameter
Save to the bundle given as a parameter. If false is returned, discard the bundle.
- [p] Behavior used by the standard library
- [p] Multiple invalid states can be implemented.
- [p] Return value does not have to be saved.
- [p] Bundle can be stored as pointer and as local variable.
- [c] Hard to read what the function actually does.
- [c] Hard to read which parameters are for input and which for output.
- [c] Hard to read whether and where bundle gets initialized/modified.
Sources:
Related:
Tags:
Blog
C Programming Language
Discussions
Software Development