notexOPENOverview
Notex/Languages/Code Review: defer and call by value
03.16LESSON 16 OF 18 · 1 MIN READ

Code Review: defer and call by value

Always free to read
  • Use defer to make sure that resources are cleaned up when a function exits.

  • For functions that allocate a resource, consider returning a cleanup closure. The unused-variable rule reminds the caller to defer it.

  • Use named return values if a deferred function can modify a function’s return value.

  • Keep your functions shallow; don’t nest the indentations too deeply. Using defer and early returns helps avoid the deep indentation of try/finally-style cleanups.

  • Unlike primitive values and structs, map and slice parameters can be mutated. Use comments on your functions to document when your function mutates the values of its parameters so users of the function know that passed-in values aren’t read-only.

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K