Pcornel
Automation · Scripting

Functions

Cheat sheet · https://pcornel.dk/topic.php?topic=5217

Back to the Topic

Answer

Functions package reusable behavior behind an explicit input/output contract. Reliable functions minimize hidden state, expose failures and separate pure transformation from external side effects where practical.

Remember

  • Function returned ≠ side effect verified; a return value can describe intent rather than real target state.
  • Reusable ≠ context-free; hidden globals, environment and working-directory assumptions reduce portability.
  • No exception ≠ valid result; functions can return partial, empty or sentinel values that callers must validate.
  • Small function ≠ single responsibility automatically; unrelated side effects can still be hidden behind a short wrapper.

First checks

  • declare -F — Bash functions
  • Get-Command -CommandType Function — PowerShell functions
  • Unit tests per function — testability

Verify in this order

input → function → output

Where it usually breaks

A function return proves only what the function chose to report; it does not prove hidden dependencies or external side effects reached the intended state.

Script/input → command/module binding → local/remoting session → request accepted → command result → target object read-back → effective runtime state → workload.

Functions scripting boundary map

BoundaryHealthy evidenceSuspicious evidence
Input / runtimeEffective inputs, runtime, working context and dependency versions are explicitHidden defaults, stale environment, wrong interpreter/path or unvalidated input
Logic / executionBranch/function/module decisions and external actions expose per-operation resultsSwallowed failures, shell reinterpretation, blind retries or last-result-wins behavior
Result / proofExit/exception/log evidence is correlated with independent target read-backProcess success or printed output treated as target success
Recovery / troubleshootingFirst failed boundary is preserved, changed minimally and retestedRandom edits, catch-all suppression or evidence destroyed before diagnosis

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5217

Confirmation

Confirm action

Continue?