Inline Function
August 1, 2018 ยท View on GitHub
{{< Concepts >}}
Inline Function
This recipe is essentially a reverse of Extract Function (fix link).
Constraints
You can't inline if it's polymorphic.
#Recipe
- For member functions, move the definition in to the class declaration
- If overloaded, rename to eliminate the overload
- Convert the function to use ->ret syntax for its return type
- If the caller uses the return value a. Introduce Variable on the return value b. Split declaration and initialization c. Surround the call with a block.
- Copy the function definition. Paste immediately above the call. Add
= [this]or= []after the name, and a semicolon at the end. - If the lambda returns a value,
a. Add the result variable to the capture list, by reference (
[this, &result]) b. Replace eachreturn blah;statement in the lambda withresult = blah; return result;c. Change the lambda return type tovoid - If there is an early return, eliminate it (only return should be the last statement)
- Eliminate parameters
- Option 1: capture calling arguments by reference + move parameters to locals and initialize with those arguments, renaming if they are the same
- Option 2: move by-ref parameters/arguments to capture by ref, renaming if they are different
- Delete the lambda declaration & the call and compile.
For example:
auto Foo() -> void { blah(); };
Foo();
Becomes:
blah();
- Compile