Sunday Scaries

Late Sunday afternoons and evenings can be run by the Sunday Scaries. Pushing back thoughts that are really building anxiety; a growing inbox, the ambiguous meeting on Tuesday, the project that was supposed to be done by now.

A few years ago I started dealing with it differently. I just started to work on Sunday. Sometimes it was 15 minutes and other times it was hours. I balanced this out by taking time for myself through the week.

I didn’t know it then, but today I would talk about this with the Backwards Law from Alan Watts. Where pursuing a positive experience ends up being a negative experience. Accepting a negative experience turns it into a positive experience.

If I’m trying not to think about work related problems on Sunday, that is still work. I’m salaried, which legally, means I’m working anyways and always. There shouldn’t be any internal conflict here.

People have misinterpreted this conversation and said it will lead to burnout. Part of being an experienced developer, is listening to your brain & body. That Sunday night work is a free ticket to an early afternoon, long lunch, or late start. Some of my most productive weeks were started on Sunday and followed by a half day later on in the week.

Null Object Pattern Example in JS

I’ve been taking JS courses on Execute Program, which as been excellent.

I’m currently learning about Array’s in-depth and wondering if this is a readable way to achieve the null object pattern. No conditionals!

const sales_tax = [
  { name: 'WA', tax: 0.10 },
  { name: 'OR', tax: 0.9 },
  ];

sales_tax[-1]  = { state: 'nan', tax: 0 }
// -1 index is not in `keys` so forEach and other enumerators will not access it

sales_tax.forEach( state => console.log(`State: ${state.name}`));
// State: WA
// State: OR
// See, no `nan`!

console.log( sales_tax.map( state => state.name))
// [ 'WA', 'OR' ]
// no `nan` here either

const customers = [
  { name: 'Travis', state: 'WA' },
  { name: 'Tiny Travis', state: 'XX' },
  ];

const payable_tax = (state) => {
  // if I did a look up with `find` it would return `undefined`. 
  // By retrieving the index for look up, I instead get -1  

  const index = sales_tax.findIndex( e => e.name === state );
  return sales_tax[index].tax; // No nil check, just call tax!
}

customers.forEach( customer => console.log(`${customer.name} will pay ${payable_tax(customer.state) * 5.00} in taxes`));
// Travis will pay 0.5 in taxes
// Tiny Travis will pay 0 in taxes

This JS code is confident. It never has to check state , handle nil or undefined.

Effective Testing with RSpec 3, Structuring Code Examples

Notes from Effective Testing with RSpec 3, chapter 7.

In Chapter 7 we go in-depth on the different ways of organizing code and making specs readable.

“…readable specs are crucial for communicating your ideas and for long-term maintainability. They show the intent behind the code.”

Excerpt From: Myron Marston, Ian Dees. “Effective Testing with RSpec 3”

Ways of organizing specs

  # Use a class.
  RSpec.describe Class do

  # Use an object.
  RSpec.describe object do

  # Provide more context in a string.
  RSpec.describe object, 'an interface' do

  # Add a tag for configuring in Hooks and filtering.
  RSpec.describe Class, 'an adapter library', issue: ABC-123 do
  
  RSpec.describe 'a class' do
    # `context` is an alias for `describe`.
    context 'in this environment' do
      it 'does one thing'
      it 'does another'
    end
    
    describe 'data structure' do
      # `example` is an alias for `it`.
      example 'in ordered form'
      example 'in unordered form'
    end
    
    describe 'api config' do
      # `specify` is an alias for `it`.      
      specify 'initializer fails when api is not defined'
      specify 'APIClient.gem specifies dependencies'
    end
  end

If the above aliases are not enough to make your specs readable you can add your own aliases. Create an alias on groups or examples and add metadata for filtering.

Ways of Sharing Logic

This section is mostly review of what we already know.

  • let method, which uses memoization.
  • hooks, run code for setup & teardown. Hooks can happen on examples, groups, the entire suite, or configured for tags.
  • Helper methods, regular ruby method, to reduce complexity.
  • Modules, regular ruby module, to reduce complexity.
  • Sharing Context and Examples to share code in the test context.

We have to be careful when sharing code in our test suites. Remember that it’s not production code. If I sacrifice readability and maintainability for optimization it is a step backward not forward.

Specs should be modular. When it fails we should have everything we need to understand the failure in front of us to resolve the issue.

Refactoring Exercise

I was a little ambitious in my refactoring. After comparing my first refactoring to the books example I realized I had missed the point. The goal of our test suit is to be easy to read in the code and execution.

This is the rspec output of my first attempt. I extracted ‘behaves like Parser’ to share between the specs.

Addressable::URI
  parses the scheme
  parses the path
  behaves like Parser
    identifies schemes
      host
      port

URI
  behaves like Parser
    identifies schemes
      host
      port
  port
    defaults http to 80
    defaults https to 443

It doesn’t read very well as each line is missing context. We cannot randomly pick up a line and understand what we are testing. We have to scan up to know what is happening. The next refactoring has better readability.

Addressable::URI
  parses the scheme
  parses the path
  behaves like a URI Parser
    identifies the host
    identifies the port

URI
  behaves like a URI Parser
    identifies the host
    identifies the port
  port
    defaults http to 80
    defaults https to 443

I’ve uploaded my refactoring to GitHub.