Tip of the Week #176: Prefer Return Values to Output Parameters
▻https://abseil.io/tips/176
Originally posted as TotW #176 on March 12, 2020
By Etienne Dechamps
Updated 2020-04-06
Quicklink: abseil.io/tips/176
The problem
Consider the following:
// Extracts the foo spec and the bar spec from the provided doodad. // Returns false if the input is invalid. bool ExtractSpecs(Doodad doodad, FooSpec* foo_spec, BarSpec* bar_spec);
Using (or implementing) this function correctly requires the developer to ask themselves a surprising number of questions:
Are foo_spec and bar_spec out or in/out parameters? What happens to pre-existing data in foo_spec or bar_spec? Is it appended to? Is it overwritten? Does it make the function CHECK-fail? Does it make it return false? Is it undefined behavior? Can foo_spec be null? Can bar_spec? If they cannot, does a null pointer make the (...)