sometimes
i dream in code


  class Book:
    def __init__(self, a, t, p):
      self.author = a
      self.title = t
      self.published = p
  
    def description(self):
      return self.title + " by " + self.author + " published in " + self.published
  

bits of programs
glide around
in my head


  function Book (t, a, p) {
    this.title = t;
    this.author = a;
    this.published = p;
  
    this.description = function() {
      return this.title + ' by ' + this.author + ' published in ' + this.published;
    };
    return this;
  }
  

i close my eyes
and there it is
code


  0900 Dim thisBook As book
  

always the code
pointy k's
and bumpy m's

it follows me
from one rem cycle
to the next


  01  BOOK
      05  TITLE           PIC X(15).
      05  AUTHOR          PIC X(15).
      05  YEARPUB         PIC 9(4).
  

i've used
a lot of
programming languages


  class Book {
    private String title;
    private String author;
    private int year_published;
  
    public Book(t, a, p) {
      title = t;
      author = a;
      year_published = p;
    }
  
    public getDescription() {
      return title  + " by " + author + " published in " + year_published;
    }
  }
  

from BASIC to FORTRAN


  0100 author = "Tolstoy"
  0200 title = "War and Peace"
  0300 published = 1869
  

to Ruby and Python


  class Book
    attr_accessor :author, :title, :published
  
    def initialize(a, t, p)
      @title = t;
      @author = a;
      @published = p;
    end
  
    def description
      "#{@title} by #{@author} published in #{@published}"
    end
  end
  

sometimes
it all
runs together


   0100  class Book:
   0200    attr_accessor :author, :title, :published
   0300
   0400    public Book(t, a, p) {
   0500      title = t;
   0600      author = a;
   0700      year_published = p;
   0800    }
   0900  end
  

these dreams
of code
they are hell


  var codeBook = Book("Inferno", "Dante", 1320);
  

coding is my job
five days a week
i'm coding man

but i don’t want to code
when i'm sleeping

i just want to sleep
when i'm asleep

hell


  pride = Book.new("Pride and Prejudice", "Austen", 1813)
  

since i can't
get away from the code
i try to make it
as easy as i can

after all
i’m tired
in fact i’m asleep
life should be easy
when i’m asleep


         NOW = TIME(0)
         CALL MAKEEASY(NOW)
  

sometimes
when i’m dreaming
in code
i make an array


  pride = new Book("Pride and Prejudice", "Austen", 1813);
  war = new Book("War and Peace", "Tolstoy", 1869);
  
  books = [pride, war];
  

and i let your code
borrow my array


  your_nefarious_code(books);
  

and your code
changes my array


  // Your nefarious code.
  
  public your_nefarious_code(Book[] books) {
    zombies = new Book("Pride and Prejudice and Zombies", "Grahame-Smith", 2009);
    books[0] = zombies;
  }
  

it's my array
i didn't want you
to change it

truth be told
sometimes
i do it to myself


  // An honest mistake
  
  public my_function(Book[] books) {
    jaws = new Book("Jaws", "Benchley", 1974);
    books[0] = jaws;
  }
  

i have an excuse though
i’m asleep


  # Don't modify this code unless you are unconscious.
  

this changing
behind my back
makes me
thrash in my sleep


  def []=(idx, value)
    raise "YouCantChangeMyArraySucker"
  end
  

so in my dream
i make
a new rule
no more
changing arrays
ever


    // Your nefarious code foiled.
    // Blam! Throws a YouCantChangeMyArraySuckerException!
  
    books[0] = zombies;
  

once i create an array
that's it
you can’t change it
neither can i


    your_impossible_to_be_nefarious_code(books);
  

now
i don’t
have to worry
about your code or threads
messing with my arrays

or my code
or my threads
it’s just easier


  0600 REM I like zombies
  

but sometimes
i need
to change my array


  GoTo 0000
  

but if i do
i’m back to square one
i don’t want to deal with it


  function justLikeTheOldOneExcept(a, i, x){
    result = a.slice(0);
    result[i] = x
    return result
  }
  

so i'll let arrays make
copies of themselves
copies that are
the same as the original

only different


  var pride = new Book("Pride and Prejudice", "Austen", 1813);
  var war = new Book("War and Peace", "Tolstoy", 1869);
  var zombies = new Book("Pride & Prejudice & Zombies", "Grahame-Smith", 2009);
  
  var books = [pride, war];
  
  var other_books = justLikeTheOldOneExcept(books, 0, zombies);
  

then i can have
my old array
and
my new array


  C  ZOMBIES ARE BACK
  

now i don't have to
expend the effort
to trust you


  /* I trust me more than you. */
  

or me


  /* I don't trust me. */
  

but sometimes you
change my objects too


  public void aNefariousBookMethod(Book b) {
    b.setTitle("Dirk Gently's Holistic Detective Agency");
  }
  

sometimes i forget
and change them myself


  // Going about my sleepy business..
  
  Book war = new Book("War and Peace", "Tolstoy", 1869);
  aNefariousBookMethod(war);
  
  // Wait what?
  
  System.out.println(war.title);
  

it’s been a long night
so let’s have the same rules for objects
and arrays


  def justLikeTheOldOneExcept(v, i, x):
    result = copy(v)
    if isinstance(result, list):
      result[i] = x
    else:
       setattr(result, i, x)
    return result
  

no changing
just modified copies


  Book book1 = new Book("War and Peace", "Tolstoy", 1869);
  Book book2 = book1.justLikeTheOldOneExcept(
                           "title",
                           "Dirk Gently's Holistic Detective Agency");
  

after all
it's just a dream


  // Rules are like spouses. There is an optimal number to have.
  

now there are fewer
rules to remember

sometimes i make an object
with private fields


  class Book {
    private String title;
    private String author;
    private int year_published;
  
    // ...
  }
  

then i discover
that i need
to get at
those private fields


  class Book {
    private String title;
    private String author;
    private int year_published;
  
    public String getTitle(){
      return title;
    }
  
    public String getAuthor(){
      return author;
    }
  
    public int getYearPublished(){
      return year_published;
    }
  }
  

so i make them public


  C TOTAL UP THE EFFORT
  
         EFFORT = PRIVEFFT + PUBLEFFT
  

i had to make the
private fields private
and then i had to make
the private fields public

that’s a lot of work
and then it’s more work


  class Book {
    public String title;
    public String author;
    public int year_published;
  }
  

it’s just easier to
make all the fields public

after all
why am i hiding from the data?
don't hide from the data
it's just data


  class ThoseObjects {
    public String title;
    public String author;
    public String cityPublshed;
    public String countryPublished;
    public int yearPublishd;
    public int editoon;
    public int copiesSold;
  }
  

sometimes
and there are
lots of fields
and i get tired of all the typing
and my fingers make mistakes


  git ci -m 'Fix misspelling of Book fields.' Book.java
  

so i’ll just use a
map or a hash or a dictionary
or whatever we call it in this language
and make an open
name/value thing


  var pride = {"title": "Pride and Prejudice", "author": "Austen", "published": 1813};
  

now i can make
new objects full of data
as fast as i can type
which isn’t very fast
since
my eyes are closed


  var zombies = {"title": "Pride and Prejudice and Zombies", "author": "Grahame-Smith", "published": 2009};
  var war =  {"title": "War and Peace", "author": "Tolstoy", "published": 1869};
  

if all my data
is in
hashes or
dictionaries
or maps
whatever we call them
in this language
then i don’t have a place for my code


  interface Function {
    public Object invoke(Object arg);
  }
  
  class MakeDescription implements Function {
    public Object invoke(Object arg) {
      Book book = (Book)arg;
  
      return book.get("title") +
             " by " +   
             book.get("author") +
             " published in " +  
             book.get("yearPublished");
    }
  }
  

so i’ll just make little
one method
no data
objects
and put my code there

i’ll call these things
functions
that’s a word i learned in
math class
before i feel asleep


  var make_description = function (b){
    return b.title + " by " + b.author   
    " published in " + b.published
  };
  

but before the night was over
the dream continued

– Russ

A version of this article originally appeared on Cognitect blog.