In Progress
Unit 1, Lesson 21
In Progress

Null Object

Video transcript & code

Captains log, star date 2013.6.4. Something is wrong with my neutronic tea kettle. It no longer makes Earl Grey tea, and as a result I have lost my commanding English accent. I have instructed commander LaForge to look into the matter. He is being assisted by Lieutenant Barclay, who is monitoring sensor readings. I have further instructed them to use the ship's log to record every aspect of the procedure, so that future crews can learn from this exercise.

require 'logger'

class Picard
  def make_it_so(log=Logger.new($stdout))
    log.info "I have instructed engineering to fix my tea kettle"
    Geordi.new(log).fix_it
  end
end

class Geordi
  def initialize(log)
    @log = log
  end

  def fix_it
    @log.info "Reversing the flux phase capacitance!"
    @log.info "Bounding a tachyon particle beam off of Data's cat!"
    Barclay.new(@log).monitor_sensors
  end
end

class Barclay
  def initialize(log)
    @log = log
  end

  def monitor_sensors
    @log.warn "Warning, bogon levels are rising!"
  end
end

Picard.new.make_it_so
# >> I, [2013-06-04T16:10:03.056063 #11351]  INFO -- : I have instructed engineering to fix my tea kettle
# >> I, [2013-06-04T16:10:03.056286 #11351]  INFO -- : Reversing the flux phase capacitance!
# >> I, [2013-06-04T16:10:03.056336 #11351]  INFO -- : Bounding a tachyon particle beam off of Data's cat!
# >> W, [2013-06-04T16:10:03.056393 #11351]  WARN -- : Warning, bogon levels are rising!

Captain's log, supplemental: Per starfleet regulations, our logs are routinely broadcast over the subspace standard output channel. We have been informed by Starfleet that logs of minor personal appliance repair is, quote, "not efficient use of available subspace bandwidth". I have misgivings about Starfleet's opinion of the importance tea to the smooth operation of this vessel, however I have decided to comply.

Unfortunately, my crew is so in the habit of keeping detailed logs that despite my orders to discontinue logging, they keep forgetting to pass the word down the chain of command. As a result, their entries are being recorded and sent out anyway. Nearby vessels have started to complain about all the subspace message traffic.

class Picard
  def make_it_so(log=Logger.new($stdout))
    log.info "I have instructed engineering to fix my tea kettle"
    Geordi.new(log, quiet: true).fix_it
  end
end

class Geordi
  def initialize(log, quiet: true)
    @log = log
  end

# ...

Just in the nick of time, ensign Crusher appears to have come up with a solution. While researching ancient earth programming manuscripts he ran across something called the "Null Object pattern". Based on his research, he has constructed a "null logger" device. This device mimics the protocols of our usual ship's log subsystem, but simply discards its input unused.

class NullLogger
  def debug(*); end
  def info(*); end
  def warn(*); end
  def error(*); end
  def fatal(*); end
end

log = NullLogger.new

puts "before"
Picard.new.make_it_so(log)
puts "after"
# >> before
# >> after

We have installed the unit into our primary computer core. I have high hopes that this will resolve our overcommunication problem with minimal inconvenience to the crew. If this works, this may be the last you hear from me for a while. So to all the life-forms out there: live long and prosper… and happy hacking!

Responses