The Pragmatics Programmer

Shawna Mattison

BA in Linguistics from UC Santa Cruz

What does linguistics have to do with programming?

Pragmatics

Context

is the study of how

contributes to meaning

reflux

JavaScript library

gastroesophageal disease

or

Configuration

Environments

Scope

var n = 21;

function timesTwo() {
    n = n * 2;
}

timesTwo();
console.log(n);
> 42
function timesTwo() {
    var n = 21;
    n = n * 2;
}

timesTwo();
console.log(n);
> Uncaught ReferenceError: n is not defined

I destroyed the computer with a virus

virus = Virus.new

computer.destroy_with(virus)

Conversational Implicature

beyond explicit meaning

"Shawna, can I have a beer?"

"They're in the fridge"

Computers speak in 1s and 0s

but your coworkers don't

(probably)

ice_creams = [chocolate, strawberry]


shawna.devour(chocolate)
shawna.devour(strawberry)


ice_creams.each do |ice_cream|
  shawna.devour(ice_cream)
end

We use implicature during programming all the time

Interfaces

how we want code to be used

Abstractions

the truths we hold about the world

Tests

as documentation of your business logic

Goal:

Give new vocabulary to describe your instincts

How does conversational implicature work?

Cooperative principle

Is it going to rain today?

The sky is looking pretty gray

Grice's Maxims

Quality

Quantity

Relation

Manner

Maxim of Quality

Be truthful 

Tests should be right

Tests should pass

Maxim of Quantity

Be as informative as you can

"6 time Oscar nominee Glenn Close"

Business requirement:

$1000 withdrawal limit

describe BankAccount do

  let(:account) { BankAccount.new }

  it 'has a $1000 withdrawal limit' do
    account.withdraw(5)
    expect(account.withdraw(1000)).to raise_error
  end
end

describe BankAccount do

  let(:account) { BankAccount.new }

  it 'has a $1000 withdrawal limit' do
    account.withdraw(5)
    expect(account.withdraw(1000)).to raise_error
  end
end

describe BankAccount do

  let(:account) { BankAccount.new }

  it 'has a $1000 withdrawal limit' do
    account.withdraw(5)
    expect(account.withdraw(1000)).to raise_error
  end
end

describe BankAccount do

  let(:account) { BankAccount.new }

  it 'has a $1000 withdrawal limit' do
    account.withdraw(5)
    expect(account.withdraw(1000)).to raise_error
  end
end

describe BankAccount do

  let(:account) { BankAccount.new }

  it 'has a $1000 withdrawal limit' do
    account.withdraw(5)
    expect(account.withdraw(1000)).to raise_error
  end
end

And we're done

But wait, is that inclusive or exclusive?

describe BankAccount do

  let(:account) { BankAccount.new }

  it 'has a $1000 withdrawal limit' do
    account.withdraw(5)
    expect(account.withdraw(1000)).to raise_error
  end
end

describe BankAccount do

  let(:account) { BankAccount.new }

  it 'has a $1000 withdrawal limit' do
    account.withdraw(999)
    expect(account.withdraw(1)).to raise_error
  end
end

Maxim of Relation

Be relevant

"Can you watch the kids on Friday?"

"My aunt Trudy is coming into town"

Business requirement:

Employees must throw out old ice cream toppings

describe Employee do
  let(:employee) { Employee.new }

  it 'rejects expired toppings' do
    caramel = Topping.new('8 oz', '.5 oz', 'caramel', .25, 1.00, '01/01/2012')
    expect(employee.approve(caramel)).to be false
  end
end
describe Employee do
  let(:employee) { Employee.new }

  it 'rejects expired toppings' do
    caramel = Topping.new('8 oz', '.5 oz', 'caramel', .25, 1.00, '01/01/2012')
    expect(employee.approve(caramel)).to be false
  end
end
describe Employee do
  let(:employee) { Employee.new }

  it 'rejects expired toppings' do
    caramel = Topping.new('8 oz', '.5 oz', 'caramel', .25, 1.00, '01/01/2012')
    expect(employee.approve(caramel)).to be false
  end
end
describe Employee do
  let(:employee) { Employee.new }

  it 'rejects expired toppings' do
    caramel = Topping.new('8 oz', '.5 oz', 'caramel', .25, 1.00, '01/01/2012')
    expect(employee.approve(caramel)).to be false
  end
end
describe Employee do
  let(:employee) { Employee.new }

  it 'rejects expired toppings' do
    caramel = Topping.new('8 oz', '.5 oz', 'caramel', .25, 1.00, '01/01/2012')
    expect(employee.approve(caramel)).to be false
  end
end
describe Employee do
  let(:employee) { Employee.new }

  it 'rejects expired toppings' do
    topping = double(:topping)
    allow(topping).to receive(:expired?) { true }
    
    expect(employee.approve(topping)).to be false
  end
end

Maxim of Manner

Be clear, brief, and orderly

"We're taking little Johnny to

the D-E-N-T-I-S-T"

"她的小孩非常"

Refactor lengthy setup

Be brief:

Business requirement:

Sundaes have to have a cherry on top

describe IceCreamSundae do
  let(:chocolate)     { Flavor.new }
  let(:sundae)        { Sundae.new(chocolate) }
  let(:caramel)       { Topping.new("caramel") }
  let(:hot_fudge)     { Topping.new("hot fudge") }
  let(:whipped_cream) { Topping.new("whipped cream") }

  it 'should have a cherry on top' do
    first_scoop  = Scoop.new(caramel)
    second_scoop = Scoop.new(hot_fudge)
    third_scoop  = Scoop.new(whipped_cream)

    sundae.add_scoop(first_scoop)
    sundae.add_scoop(second_scoop)
    sundae.add_scoop(third_scoop)

    expect(sundae.valid?).to be false
  end
end
describe IceCreamSundae do
  it 'should have a cherry on top' do
    sundae = build(:sundae, cherry: nil)
    expect(sundae.valid?).to be false
  end
end

Be clear:

No magic strings or numbers

Business requirement:

Ice cream scoop toppings must be valid

describe Scoop do
  it 'has valid toppings' do
    scoop = Scoop.new("bourbon salted caramel")
    expect(scoop.valid?).to be false
  end
end
describe Scoop do
  it 'has valid toppings' do
    scoop = Scoop.new("bourbon salted caramel")
    expect(scoop.valid?).to be false
  end
end
describe Scoop do
  it 'has valid toppings' do
    scoop = Scoop.new("bourbon salted caramel")
    expect(scoop.valid?).to be false
  end
end
describe Scoop do
  it 'has valid toppings' do
    long_topping_name = "bourbon salted caramel"
    
    scoop = Scoop.new(long_topping_name)
    expect(scoop.valid?).to be false
  end
end

Be orderly:

Organize!

describe Sundae do
  it 'should have valid toppings'
  it 'should have at least one scoop'
  it 'is 10% off on Sundae Sundae Sunday'
  it 'should have no more than 3 scoops'
  it 'has a cherry on top'
  it 'has only one topping per scoop'
  it 'has free sprinkles on Sundae Sundae Sunday'
end
describe Sundae do
  describe 'scoop requirements' do
    it 'should have at least one scoop'  
  end
    
  describe 'valid toppings' do
    it 'has a cherry on top'
    it 'has only one topping per scoop'
  end

  context 'on Sundae Sundae Sunday' do
    it 'is 10% off'
    it 'has free sprinkles'
  end
end

But Shawna, good test names could solve all your problems!

it 'has a $1000 withdrawal limit'

vs.

it 'has a $1000 (exclusive) withdrawal limit'
it 'has valid toppings'

vs.

it 'has toppings with valid length names'

Thanks

Mark McDonald

Geoff Geysmann

David Edwards

The Pragmatics Programmer

By shawndromat

The Pragmatics Programmer

  • 823