Skip to content

Should there be guidelines for preserving global state? #44

@yurikhan

Description

@yurikhan

Elisp has a lot of mutable global state. Some of it is highly shared. And it is very easy to modify global, highly shared state inadvertently. This includes point, mark, narrowing, match data, current buffer, selected window, selected frame, and who knows what else.

For example, calling (looking-at "[ \t]") to check if we are on whitespace clobbers match data. If this is done in a function, the caller has to wrap its invocation in a (save-match-data) if match data is important.

Proposal:

  • If your function is not specifically intended to modify global shared state, preserve it by wrapping your function body in the appropriate (save-*) or (with-*) macros.
  • If you do modify global shared state, at a minimum, document the fact; ideally, document how you modify it.
  • If you accept function arguments and expect them to modify global shared state, document your expectations. If they are called in a modified shared state context, document that context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions