Jack Applin |
Jack’s Ten Commandments of Computer ProgrammingCharlton HestonBen-HurPlanet of the ApesThe Ten CommandmentsToleranceI realize that you may have deeply-held religious beliefs about those same Ten Commandments, and you may find any non-reverent discussion of them to be offensive.
OklahomaThis was at the Oklahoma State Capitol for a while. There was lots of enjoyable arguing about it, and then the Hindus and the Satanists asked to put up their monuments, and now it’s gone. I’m not sure that the original tablets contained a bald eagle and an American flag, but I’m no biblical scholar. MisspellingThe original contained a misspelling, but somebody hit it with a car, and the replacement had “sabbath” spelled correctly. OklahomaHere they are. It’s curious that they’re not numbered. OklahomaOh, that’s why they’re not numbered—there are twelve of them. Different religions deal with this problem in different ways. Jack’s SolutionThe first one is arguably a statement, not a commandment. I’ll call it the “zeroeth” commandment. Also, I’ll merge the last two into one. That will bring it down to ten, kinda sorta. Zeroeth CommandmentI am the Lord your God My interpretation: Have faith in God. Here’s what you should not have faith in: hardware and software. As my pal Susan says, “God may answer prayers, but he doesn’t seem to fix hardware too often.” Everything Fails
Despair?
First CommandmentThou shalt have no other gods before me My interpretation: God is in charge. Monotheism (or tritheism, depending on how you count) is the way to go! He’s number one! Or is he number zero? Gee, counting is harder than it seems. Start numbering with one, as appropriate. No person calls January month zero, so neither should you. What sort of moron starts a list with zero? ctimeConsider this excerpt from the
Cardinal, Ordinal, NominalUnderstand the difference between cardinal, ordinal, and nominal numbers. Cardinal number count: one apple, two apples, etc. Ordinal numbers indicate order in a list: my first wife, my second wife, etc. Nominal numbers name things: your CSUID, 80523, 867-5309 NASCARThere are seven (cardinal) cars. Number 99 (nominal) is number 1 (ordinal). Second CommandmentThou shalt not make to thyself any graven images
PowerPoint & WordInvariably, formats such as PowerPoint or Microsoft Word force one to use a specialized editor, a GUI. Just as invariably, the “editor” has all the massive power and feature set of notepad or gedit. Don’t force me to use your editor. I want to use my own editor. Even worse is data frozen in form as a picture. What are the odds that I’m going to bring up an image editor to improve a picture? Hypocrisy‽These slides are not Microsoft PowerPoint. They are PmWiki markup, translated to S5 by PmWiki. The previous paragraph looks like this, as source: These slides are ''not'' [[Wikipedia:PowerPoint|Microsoft PowerPoint]]. They are [[http://pmwiki.org|PmWiki]] markup, translated to [[Wikipedia:S5_%28file_format%29|S5]] by PmWiki. It’s easy to edit, and (more or less) plain text. Jack Disobeys the Second Commandment
Third CommandmentThou shalt not take the name of the Lord thy God in vain My interpretation: Don’t use Jehovah’s name casually. Also, beware of those who invoke his name. We all know that when a child says, “I swear TO GOD that I’m telling the truth!”, that he’s lying. Faith is sometimes a good thing. Skepticism is also sometimes a good thing. Be Skeptical
Skepticism#! /bin/bash die() { echo "$@" >&2; exit 1; } (($# == 2)) || die "usage: $0 <absolute-directory-path> <count>" dir="$1" num="$2" [[ $dir = /* ]] || die "$0: $dir isn’t an absolute path" [[ -d $dir ]] || die "$0: $dir isn’t a directory" [[ -r $dir ]] || die "$0: $dir isn’t readable" [[ -w $dir ]] || die "$0: $dir isn’t writable" [[ -x $dir ]] || die "$0: $dir isn’t executable" [[ $num =~ ^[0-9]+$ ]] || die "$0: $num isn’t a number" Excess SkepticismDon’t go overboard. If your mother offers you a sandwich, don’t respond, “How do I know that you’re not trying to poison me?”. Similarly, obsessive checking is not always indicated. If you’re writing a script to be executed once, by you, immediately, you can probably trust the arguments. What matters, from a engineering point of view, is how much time can be wasted from the (potentially many) people using your program, vs. the time you spend making it more robust. Fourth CommandmentRemember the sabbath day, to keep it holy Take time off. Don’t sit at the terminal and stare. Let your subconscious work. Student Disobeys the Fourth CommandmentStudent: Mr. Applin, I'm so frustrated! I can’t get this to work! Jack: Why is that? Student: I’ve been in the lab for seven hours now, trying & trying! Jack: What an interesting strategy. You should take a break. Student: I can’t—it’s due in twenty minutes! StandardsSpeaking of the sabbath: when is it, anyway? I can think of at least three religions that follow these rules, but observe three different sabbaths. Religious diversity—swell! However, I see the same thing when software companies don’t adhere to published standards, or “creatively interpret” them. Chaos ensues! Fifth CommandmentHonour thy father and thy mother My interpretation: Have respect for the past. Sometimes, newer is better. Modern phones are certainly more better than being stuck to the wall with a cord. On the other hand, some old things are just as good. Casablanca stands up to current films. LanguagesYou should certainly learn the current hot language. However, you’re a fool if you learn only the most popular language. It is the very rare programmer who gets to choose what programming language they will use. Perhaps if you work alone. On the other hand, if you’re working with other people, they’ve already chosen a language. When you visit Tokyo, do you express surprise that they’re not speaking English, which you regard as a better language? CompatibilityThe gcc people have their act together. They take backward compatibility very seriously. Their typical technique:
Sixth CommandmentThou shalt not kill When I was growing up, my elders said, “Well, when it says ‘kill’, it really means … . It’s ok to kill animals, or in self-defense, or if you’re a soldier, or if you’re an executioner, or a policeman, or …” A lot of waffling! My interpretation in the computer realm: Don’t kill processes. That is, don’t kill processes rudely (on Linux, ResourcesPrograms often have resources open:
Terminate them politely, so they can clean up. Similarly, construct your own programs to terminate gracefully when SIGINTR & SIGTERM are received. Seventh CommandmentThou shalt not commit adultery I interpret this as “Don’t cheat on your spouse”. I suppose it’s not suprising that Mr. Monotheism would be in favor of monogamy, though there certainly seems to be a lot of polygamy in the Old Testament. Or does this just mean to be faithful to all of your spouses? In the programming world, this refers to program “purity”. Don’t be Monolingual
On the Other Hand#! /usr/bin/perl -w use File::Copy; ($src,$dst) = @ARGV; copy $src, $dest; chmod 0600, $dest; unlink $src; #! /usr/bin/perl -w ($src,$dst) = @ARGV; system("cp $src $dest"); system("chmod 600 $dest"); system("rm -f $src"); The left-hand Perl program may as well be a shell script. It uses system() to do everything. It is slow (if that matters) and hard to read. Also, the left-hand Perl program will fail if either filename contains any interesting characters (["'*? ;<>&|]). The right-hand program will not fail. Eighth CommandmentThou shalt not steal Plagiarism is bad. Stealing with attribution is fine (assuming that it’s allowed, in context (not in my class)). Perl & Python are all about stealing features from previous languages. We talk more about in the tenth commandment about coveting. Ninth CommandmentThou shalt not bear false witness against thy neighbour I interpret this as “Don’t lie”. Don’t misrepresent your data. Simple Example of Lying#include <stdio.h> int main() { double d; scanf("%f", &d); printf("d is %f\n", d); return 0; } Cheaters Never Prosper#include <stdio.h> int main() { char *p = "font-size=300%"; puts(p); return 0; } cast.c:5:19: warning: initialization discards ‘const’ qualifier from pointer target type char *p = "font-size=300%"; Cheaters Never Prosper#include <stdio.h> int main() { const char *p = "font-size=300%"; puts(p); return 0; } Works fine now. But, wait … there’s supposed to be a colon there instead of an equals sign. Cheaters Never Prosper#include <stdio.h> int main() { const char *p = "font-size=300%"; p[9] = ':'; puts(p); return 0; } cast.c:5:10: error: assignment of read-only location ‘*(p + 9u)’ p[9] = ':'; Stupid compiler … I’ll cast it! Cheaters Never Prosper#include <stdio.h> int main() { const char *p = "font-size=300%"; char *q = (char *) p; q[9] = ':'; puts(p); return 0; } Segmentation fault (core dumped) Tenth CommandmentThou shalt not covet (neighbor’s house, wife, ass, etc.) My interpretation: don’t be envious I can’t agree with this one. Envy is the basis of our capitalist system! You admire your neighbor’s car? Then work hard to get one of your own! Stealing in LanguagesIs this envy, stealing, or just evolution?
Stealing in ProgrammingHere’s some Perl code. #! /usr/bin/perl -w @ARGV==1 or die "$0: Must have exactly one argument"; if ($ARGV[0] eq "snark") { warn "$0: argument of snark is dangerous"; } I’d like to write the equivalent in bash, but it uses No problem—I will write bash-compatible versions of Porting to BashHere’s my standalone #! /bin/bash echo "$@" >&2 Here’s my die() { echo "$@" >&2; exit 1; }
In BashHere’s my equivalent bash code: #! /bin/bash die() { echo "$@" >&2; exit 1; } (( $# == 1 )) || die "$0: Must have exactly one argument" if [[ $1 = snark ]] then warn "$0: argument of snark is dangerous" fi |