Skip to content

Functions: designing arguments #471

@practicalli-johnny

Description

@practicalli-johnny

Write a section on function design, with a specific design guidance on functional arguments. Provide examples and rationale about why and when each approach should (or should not) be used.

Draft

Function calls must supply the correct number of arguments or the call will fail

Fixed arguments

When the function must take specific arguments and the function contract will never change

This is a brittle approach as a function contract is likely to break over time.

Fixed args with opts

Using opts allows arguments to be extended without breaking the function contract.

This approach is still brittle as it will break if one of the assumed fixed arguments changes or even the order of arguments changes.

Polymorphic arguments

A function definition can provide several body experessions each with their own number of arguments

Note: this is a common pattern found in the design of clojure.core

A collection

passing a collection to a function provides a great deal of flexibility in the function contract

the contents of the collection can change and even the type of collection can change without breaking the function contract (interface)

Of course if the contents of the collection changes then the code within the function definition still needs to work. However as that code is local to the function then it can change without breaking the contract.

The same kind of information should be returned to the calling function to avoid breaking that function's operation.

Specify a contract

A function definition can be instrumented with a function specification which defines the contract in a programmatical way, allowing the contract to be tested

  • clojure.spec
  • malli

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions