In a blog post about unconditional programming Michael Feathers shows how limiting if
statements can be used as a tool for reducing code complexity.
He uses a specific example to illustrate his point. Now, I've been thinking about other specific examples that could help me learn more about unconditional/if
less/for
less programming.
For example, using OptionParser I made a cat clone that will upcase the stream if the --upcase
switch is set:
#!/usr/bin/env ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: cat [options] [file ...]"
opts.on("-u", "--upcase", "Upcase stream") do
options[:upcase] = true
end
end.parse!
if options[:upcase]
puts ARGF.read.upcase
else
puts ARGF.read
end
How would I handle that switch without an if..else
block?
Also interested in links to other illustrative specific examples.
See Question&Answers more detail:os