2

For prototyping convenience I've relied on a number of global variables which are heavily used throughout the code. But now I'd like to make some of them local (but dynamic). Is there any significant downside (eg, efficiency, something else) to declaring them locally special instead of global?

3
  • 1
    I wouldn't expect an efficiency issue. All the unpopular features of special variables apply, obviously. Commented Jun 23, 2019 at 18:11
  • Can you summarize the unpopular features so I could review them? Commented Jun 23, 2019 at 20:41
  • 1
    So the question is about local variables declared locally as special? Commented Jun 24, 2019 at 10:15

1 Answer 1

1

Unpopular features of special variables include:

  • Lack of referential transparency

This makes it harder to reason functionally about your code. It means that your function produces different results with syntactically equivalent calls.

  • Introduce bugs

If a lexical variable is defined somewhere in your code (eg. in a system function), you will overwrite it and cause bugs.

  • Confusing

Special (dynamic) binding is unpopular, and will confuse your readers who are not familiar with it.

  • Unnecessary

Just use lexical binding, or even anaphoric macros instead.

More information:

Anaphoric macros See Let Over Lambda by Doug Hoyte, or Paul Graham's anaphoric macros.

LiSP (Lisp in Small Pieces) has a section on binding and dynamic binding

Elisp has dynamic binding by default, and enforced dynamic binding for a long time

Many early lisps had dynamic binding, but dropped it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for a comprehensive summary--all good reasons for not translating my global specials to local specials. Will investigate other ways to reduce dependence on globals (in addition to passing local versions as arguments).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.