Side Effects

Side Effects

  • The normal way to code with imperative programming
    • repeated updates to variables
    • updates to structures like arrays and key-value tables
    • printing
  • With medications, side effects are the bits in tiny print that they hope you don't read

Problems with Side Effects

  • To reason about a program, you have to simulate running it, step by step
  • Order matters. You can't move steps around without checking for side effects.
  • Presence matters. You can't delete steps without making sure some side effect isn't needed elsewhere.
  • Destructive updates can affect code nowhere near the code you are editing

Waiver

I understand that use of any of the techniques shown may be hazardous to the health of my code.

Place setting

(setf place value)

Stores value in the place given. Place can be a varible or, destructively, a location within a structure.

Also works for incf, decf, push, pop, and several other operators

Standard places

(setf place value)

Place can be one of a large number of location accessing forms, including

Destructive list operations

  • rplaca and rplacd -- replace car and cdr -- use setf instead
  • nconc -- destructive append
  • mapcan
  • delete, sort, nreverse nsubstitute, and other functions often starting with "n"