Wednesday, March 26, 2008

Laziness

I remember when I first realized I wanted to be an engineer, I was working at Target and I was constantly thinking of ways I could replace myself with a machine. I was fascinated with the "LRT" scanners and thought of so many ways to make the process better. "Efficiency is intelligent laziness" has been my motto ever since.

This is in stark contrast to the first time I actually tried programming, my eventual major and career. It was so hard, and took so long to do anything that I hated it. I think it was one summer during middle school when I went across the street to learn wood working with my neighbor's father and my neighbor came over to learn programming with my Dad. I think it's pretty cool that we carried the tradition of apprenticeship and I hope my own son will have a similar experience. But I digress. This experience of programming was to write "Hello World" to the screen using c++. It required so many files, a header and an implementation, not to mention the CodeWarrior project file and I was a hunt and peck typist at the time so it took over 2 hours to copy the program from a paper print-out. If it was so hard to write two words to the screen, how would I possibly write a game with graphics?!

One way or another, I stuck through it and now I am a software engineer and I have found ways to apply my motto to school and work. I still hate all the setup of programming though, especially for scripting languages. I wrote these programs to a.) learn them and b.) have a mkscript script that I can use to create a template for new programs. I think it's pretty cleaver that the template comes from the program itself!

Okay so I know perl, that will be obvious by how much better the perl program looks (or maybe perl is better??). But I thought it was lame to claim this is a good method for learning and not put it into practice, so I did Python and Ruby too.


#!/usr/bin/perl

use strict;

foreach my $file (@ARGV) {
if(not -e $file) {
open (FILE, ">$file");
open (READ, "$0");
while(my $string = <READ>) {
chomp $string;
print FILE "$string\n";
}
system("chmod u+x $file");
}
}





#!/usr/bin/python
import sys
import os.path

def main(argv):
for i in range (1, len(sys.argv)):
if False == os.path.exists(sys.argv[i]):
writeFile = open(sys.argv[i], 'w')
readFile = open(sys.argv[0], 'r')
try:
for line in readFile:
writeFile.write(line);
finally:
readFile.close()
writeFile.close()
os.system("chmod u+x " + sys.argv[i])

if __name__ == "__main__":
main(sys.argv[1:])





#!/usr/bin/ruby

for file in (ARGV) do
puts file
if not File.exist?(file)
writeFile = File.new(file, "w")
readFile = File.new($0)
while (line = readFile.gets)
writeFile.puts(line)
end
system "chmod u+x #{file}"
end
end


If it means anything, I knew perl so that one was easy. I knew Python in College, hated it then and hated it now as I wrote this script. Ruby, I have never used and I must say, I'm impressed. I learned and wrote that script faster than I wrote the Python script, which I should still remember. I still like Perl best, but I'd give Ruby a shot if I needed to.