Although Clarity generally supports declarative programming, the code examples resort to imperative programming with statements mutating the state of the contract as side effect.
Would it be possible for Clarity to support a declarative programming style also for functions that determine the next state of the contract?
It can be beneficial to avoid or at least restrict side effects by keeping most functions pure. The Welcome to Clarity guide even recommends a naming convention calling out functions with side effects:
Functions that mutate data by convention terminate with an ! exclamation point.
Yet the Clarity tutorial defines functions that mutate the contract state as side effect:
(define-map tokens ((account principal)) ((balance int))) (define-private (token-credit! (account principal) (amount int)) (if (<= amount 0) (err "must move positive balance") (let ((current-amount (get-balance account))) (begin (map-set! tokens (tuple (account account)) (tuple (balance (+ amount current-amount)))) (ok amount))))) (define-public (mint! (amount int)) (let ((balance (get-balance tx-sender))) (token-credit! tx-sender amount)))
In this case,
token-credit! mutates the persistent
tokens map as a side effect. How could this functionality be implemented in Clarity using a declarative style?