Inline Function
Category A — Simplifying and restructuring functions
Replace calls to a function with the function's body, then remove the function.
Inline Function is the inverse of Extract Function. Use it when a name no longer earns its keep: the reader has to jump to the definition and learns nothing they could not see at the call site.
Motivation
The smell
- The body is as clear as the name.
return age >= 18does not needis_adult()if the domain already speaks in ages at that call site. - Needless indirection. A wrapper delegates to another function or returns a constant/simple expression without adding policy, validation, or vocabulary.
- One-off helper. A function is called once, is unlikely to be reused, and makes the local flow harder to follow.
- Bad factoring. A group of functions is split in the wrong places. Inline them back into their callers, then extract the right abstractions.
- Dead abstraction after change. A function used to matter, but refactoring left it as a thin shell.
Fowler's test: when a function body is as clear as its name, inline it.
The design principle
- Names must earn indirection. A function call is useful when it compresses intent, hides a meaningful decision, or creates a stable boundary. Otherwise it is navigation cost.
- Local readability. Sometimes the clearest code is the code right there, not behind another name.
- Behavior preservation. Inlining changes structure, not observable behavior: same arguments evaluated in the same order, same side effects, same return value, same errors.
Inline Function is not:
- a license to delete useful domain vocabulary;
- a way to make large functions bigger without a follow-up plan;
- a mechanical expansion of every small helper.
When not to inline
- The name adds domain meaning.
isEligibleForDiscount(order)may be clearer than its boolean expression. - The function is a boundary. Public API, plugin hook, override point, test seam, or polymorphic method.
- The function is overridden or implements an interface/contract where removing it changes behavior or type structure.
- Multiple callers rely on one clear abstraction. Inlining duplicates knowledge.
- The body is complex. Inlining would make callers harder to read. Consider re-extracting differently instead.
- Dynamic/reflection use. The function may be called by name, framework, serialization, or external consumers.
Mechanic
Before you start
- Read the whole file and locate every caller. Search for references, including imports, re-exports, tests, and dynamic uses.
- Confirm the function is not part of a required contract: overrides, interface implementations, public API, framework hooks, or reflected calls.
- Find existing checks (tests/type checker/linter/run). Use what exists. Do not add tests unless asked.
- Work on one function at a time. Run checks after each removal.
Steps
- Pick one function. Prefer a trivial, private, single-caller, non-polymorphic function.
- Check the call sites. Count callers and note argument evaluation, side effects, and return use.
- Copy the body mentally or literally. Map parameters to arguments. Preserve evaluation order.
- Replace one caller. Substitute the call with the inlined body, adapting names to the caller's scope.
- Run checks. If behavior changes, stop and report.
- Repeat for remaining callers.
- Remove the function definition only when no callers remain and it is not externally required.
- Clean up. Remove now-unused imports, variables, or helpers exposed only for the old function. If inlining made a caller too large, name that as a follow-up Extract Function; do not silently re-extract unrelated code.
Resolving variables and call shapes
| Situation | Action |
|---|---|
| Argument is a simple name/value | Substitute directly. |
| Argument is an expression with side effects | Evaluate it once in the same place/order as before. Assign to a local if needed. |
| Parameter name collides with caller local | Rename the inlined local to a clear, non-colliding name. |
| Function returns a value used in an expression | Inline as an expression when simple; otherwise assign to a well-named local. |
| Function has multiple return points | Use early returns only if caller control flow stays equivalent; otherwise restructure carefully or do not inline. |
| Function mutates state or performs I/O | Preserve order and frequency exactly. |
| Caller passes different argument shapes across sites | Inline each site carefully; do not force a bad shared abstraction. |
Rules that keep the move safe:
- Preserve argument evaluation order.
- Preserve side effects and their order.
- Do not inline polymorphic/contract methods.
- Do not change public behavior while "cleaning up".
- One function at a time; keep the diff attributable.
Example
Before
def get_order_status(paid: bool, shipped: bool) -> str:
if not paid:
return "Pending Payment"
if shipped:
return "Shipped"
return order_ready()
def order_ready() -> str:
return "Ready to Ship"
order_ready() adds no vocabulary beyond the string it returns. The indirection makes the reader jump for no gain.
After
def get_order_status(paid: bool, shipped: bool) -> str:
if not paid:
return "Pending Payment"
if shipped:
return "Shipped"
return "Ready to Ship"
Behavior is unchanged. The call site is clearer because the value is immediately visible.
A non-example
def is_eligible_for_discount(order: Order) -> bool:
return order.customer.is_premium and order.total > 100
Even if used once, this name may carry domain meaning. Inline only when the name does not earn its keep.
Related refactorings
- Extract Function — the inverse; after inlining bad factoring, extract the right abstraction.
- Replace Temp with Query — can create small functions that later become inline candidates, or vice versa.
- Remove Dead Code — once inlined and unreferenced, the old function becomes dead code.
- Substitute Algorithm — sometimes clearer than inlining a tangled body into callers.
Running this skill on a file
When invoked with @inline_function:
- Confirm the target. Use the file named by the user. If none is named, ask for one. Read it completely before proposing anything.
- Inventory candidates. For each candidate, report:
- function name and location,
- caller count and locations,
- why the indirection is weak (trivial body, wrapper, one-off, clearer inline),
- contract risk (public API, override, dynamic use, import/export surface),
- risk: low (private, single caller, pure), medium (multiple callers or side effects), high (contract/polymorphism/dynamic use).
- Propose before editing. Present the plan and wait for approval. Do not change files on the first pass.
- Apply one function at a time. Inline one caller at a time when practical. Keep edits small and preserve style.
- Run existing checks after each step. If no checks exist, show the diff and state what was not verified.
- Report. Say what was inlined, what was removed, what remains, and any follow-up (such as Extract Function if a caller grew too large).
- Stop and ask if the function is polymorphic, externally used, dynamically referenced, or if inlining would change behavior or readability significantly.
Checklist
- Every caller was found and updated.
- Argument evaluation order and side effects are preserved.
- The function is removed only when no required references remain.
- The inlined code is at least as readable as the indirection.
- Checks pass, or the unverified risk is stated.