Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions: designing arguments #471

Open
practicalli-johnny opened this issue Aug 19, 2024 · 0 comments
Open

Functions: designing arguments #471

practicalli-johnny opened this issue Aug 19, 2024 · 0 comments

Comments

@practicalli-johnny
Copy link
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

1 participant