One of the finest universities north of Prospect in Fort Collins

Jack Applin

PmWiki

See this page as a slide show

Jack’s Ten Commandments of Computer Programming

Charlton Heston

Ben-Hur

Planet of the Apes

The Ten Commandments

Tolerance

I 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.

  • Rest assured that I have nothing against your religion. I regard it as just as good as all the other ones.
  • This is a university. We discuss things.
  • This is the United States of America. The First Amendment to our Constitution guarantees both Freedom of Religion and Freedom of Speech, in the same sentence.
  • I’m not grading you on this.
  • “Repeat to yourself ‘It’s just a show, I should really just relax.’ ”

Oklahoma

This 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.

Misspelling

The original contained a misspelling, but somebody hit it with a car, and the replacement had “sabbath” spelled correctly.

Oklahoma

Here they are. It’s curious that they’re not numbered.

Oklahoma

Oh, that’s why they’re not numbered—there are twelve of them.

Different religions deal with this problem in different ways.

Jack’s Solution

The 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 Commandment

I 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

  • All hardware fails:
    • Your laptop will fail.
    • Your standalone disk drive will fail.
    • Your backup media will be unreadable.
    • Your network will fail.
    • Your power will fail.
  • All software fails:
    • Your program will misbehave.
    • Your OS will crash.
    • Your system will be compromised by hackers.
    • Your cloud service will collapse.
    • Your disgruntled employee will betray you.

Despair?

  • Do not despair.
  • You’re not entitled to be surprised when it happens, however.
  • Back up your data:
    • Offsite copies (cloud)
    • Redundant backups
  • Redundant hardware:
    • If you need your laptop every day, then you must own two laptops.
    • UPS
    • Extra desktop computers held in reserve.

First Commandment

Thou 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?

ctime

Consider this excerpt from the ctime(3) manual page:

tm_sec
The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.
tm_min
The number of minutes after the hour, in the range 0 to 59.
tm_hour
The number of hours past midnight, in the range 0 to 23.
tm_mday
The day of the month, in the range 1 to 31.
tm_mon
The number of months since January, in the range 0 to 11.

Cardinal, Ordinal, Nominal

Understand 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

NASCAR

There are seven (cardinal) cars. Number 99 (nominal) is number 1 (ordinal).

Second Commandment

Thou shalt not make to thyself any graven images

  • Pictures are hard to deal with, .ppt & .doc doubly so.
  • Binary data is even worse.
  • Use plain text when you can.
  • Compression, on the other hand, ain’t all that bad.

PowerPoint & Word

Invariably, 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

  • Just before Halloween, my wife was off traveling, visiting her family.
  • While she was gone, I bought a pumpkin to carve into a jack-o-lantern.
  • I notified her of this via a text message:
  • “I have a 🎃.”
  • My wife’s phone is an older one, with only a few emojis, so she saw:
  • “I have a .”
  • She responded:
  • “You have a period?”

Third Commandment

Thou 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

  • If your program should have three arguments, verify that it does.
  • If your the first argument should be a floating-point number, verify that.
  • If your backup program’s target should be the root of a empty, mounted, ext3 file system, verify that.

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 Skepticism

Don’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 Commandment

Remember 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 Commandment

Student: 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!

Standards

Speaking 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 Commandment

Honour 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.

Languages

You 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?

Compatibility

The gcc people have their act together. They take backward compatibility very seriously. Their typical technique:

Release N
Mark feature F as obsolescent. That means that it’s supported in this release, but, fair warning–it may become obsolete in future releases, so you should stop using it.
Release N+1
Generate a warning if feature F is used.
Release N+2
Feature F is now obsolete. Generate a special error message if it’s used.
Release N+3
Feature F completely removed.

Sixth Commandment

Thou 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, kill -9). Let them terminate gracefully. Learn about signals and how to catch them.

Resources

Programs often have resources open:

  • temporary files
  • sockets to servers
  • database connections

Terminate them politely, so they can clean up. Similarly, construct your own programs to terminate gracefully when SIGINTR & SIGTERM are received.

Seventh Commandment

Thou 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

  • Use the right language for the job, including the small parts of a bigger job.
    • A shell script is good for rearranging files.
    • A C program is good for numerical analysis.
    • A Perl program is good for string processing.
  • It’s fine to use awk, perl, cut, or grep inside a bash script.

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 Commandment

Thou 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 Commandment

Thou 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 Commandment

Thou 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 Languages

Is this envy, stealing, or just evolution?

BeforeAfter
Fortran: variableseverybody else: variables
Smalltalk: objectsC++, Java, PHP, etc.: objects
C++: auto x=1.2;Go: y := 3.4
English: weekendFrench: le weekend
Japanese: emojiEnglish: emoji
PowerPointS5

Stealing in Programming

Here’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 die and warn, which are swell Perl features that bash doesn’t have. ☹

No problem—I will write bash-compatible versions of die and warn.

Porting to Bash

Here’s my standalone warn script:

#! /bin/bash
echo "$@" >&2

Here’s my die function (why can’t it be a script?):

die() { echo "$@" >&2; exit 1; }

In Bash

Here’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