Friday, December 2, 2016

Refactor, Refactor, Refactor


Original Code

from_file, to_file = ARGV
puts "Copying from #{from_file} to #{to_file}"
# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read
puts "The input file #{indata.length} bites long"
puts "Does the output file exist? #{File.exist?(to_file)}"
puts "Ready, hit RETURN to continue, CTRL-C to abort."
$stdin.gets
out_file = open(to_file, 'w')
out_file.write(indata)
puts "Alright, all done."
out_file.close
in_file.close

Extremely Refactored Code

File.new(ARGV[0], 'w').write(File.read(ARGV[1]))
Acknowledgement: Picture from Flickr 

No comments:

Post a Comment

1,050 hours

It took me 13 working days to complete my first 100 "work" pomodoros as a Junior Software Tester at Profectus Group.  Much of ...