CoffeeScript Implicit Return: A Clearer Contract for Small Functions
CoffeeScript implicitly returns a function's final expression. Use that convention for simple transformations, but make validation guards and multiple exits explicit so the return contract stays reviewable.
29 Jan 2026, 05:07 UTC

The ambiguity implicit return should solve
A short CoffeeScript function can still be hard to review when its return behavior is hidden inside syntax. The problem is not merely missing return keywords; it is not knowing whether a function is a single-expression transformation or a routine with validation and multiple exits. The practical rule is to let implicit return handle simple transformations, while making important exits explicit.
This article assumes CoffeeScript 2.x. The compiler turns source into JavaScript, so the runtime must support the syntax in the generated file.
In CoffeeScript, the final expression in a function body is returned automatically:
square = (x) ->
x * x
Equivalent JavaScript:
function square(x) {
return x * x;
}
The generated function contains a return even though the source does not. That is convenient for a pure calculation, but it does not make the function pure, and it does not apply to top-level script code.
Arrow choice is a separate decision. -> is a thin arrow and creates a normal function with its own this. => is a fat arrow and keeps the surrounding this. CoffeeScript 2.x emits a native ECMAScript arrow function for =>, so the target runtime must support that syntax. Use fat arrows for callbacks that need lexical this, not simply because they look shorter.
A useful review table:
| Situation | Return style | Reason |
|---|---|---|
| Single-expression transformation | Implicit | The final expression is the result. |
| Validation guard | Explicit | The early exit should be visible. |
| Several branches or exits | Explicit | Reviewers can see every exit. |
| Callback with an early exit | Explicit | return ends only that callback. |
A worked example: subtotal with a guard
Consider a subtotal helper that rejects invalid input before reducing a list. Amounts are integer cents, which avoids floating-point representation errors for this particular calculation.
subtotalFor = (items) ->
return 0 unless Array.isArray items
items.reduce(((sum, item) -> sum + item.amountCents), 0)
module.exports = subtotalFor
The explicit return 0 is a guard: it stops the function before the reducer runs. The final expression is returned implicitly. The thin-arrow callback passed to reduce has its own return, so each iteration adds one item's amount to the running total.
Equivalent JavaScript:
function subtotalFor(items) {
if (!Array.isArray(items)) {
return 0;
}
return items.reduce(function(sum, item) {
return sum + item.amountCents;
}, 0);
}
This example does not handle taxes, rounding, or currency validation beyond the array check. Keeping those rules outside the helper makes the return contract easier to test.
Compile and verify the generated module
Run the following from the project root. A local development dependency avoids modifying the machine's global Node.js installation, but it requires network access and write access to package.json and the lockfile:
npm install --save-dev coffeescript@2
Save the example as src/order-subtotal.coffee, then compile and inspect it. Replace src and build with paths your project already owns:
npx coffee --compile --output-dir build src/order-subtotal.coffee
test -f build/order-subtotal.js
sed -n '1,120p' build/order-subtotal.js
Expected checks are that build/order-subtotal.js exists and that the generated code contains the guard's return 0 plus a return inside the reducer callback. Exact formatting can vary by compiler version. The compile command writes into build and can overwrite a file with the same name, so use a clean, project-owned build directory in CI.
Run the generated module with a Node.js release that supports the emitted syntax:
node -e "const subtotalFor = require('./build/order-subtotal.js'); console.log(subtotalFor([{ amountCents: 1250 }, { amountCents: 750 }]))"
The expected value is 2000. A second check can exercise the guard:
node -e "const subtotalFor = require('./build/order-subtotal.js'); console.log(subtotalFor(null))"
The expected value there is 0. These checks verify both compilation and runtime behavior; they are not a substitute for a test suite. If this was only a local trial, revert the package-file changes and remove only the newly generated build artifacts through your normal version-control workflow.
A return-style rule reviewers can apply
Use implicit return when a function is a single-expression transformation with no early exit. Use explicit return for validation guards, multiple exits, and callbacks where an early exit matters. Keep that decision separate from -> versus =>: lexical this is a binding question, not a return-style question.
For a new or upgraded CoffeeScript project, compile in CI, inspect the generated JavaScript once, and align the Node.js version with the syntax the compiler emits. That keeps the return convention reviewable without turning every small function into a wall of explicit returns.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.