|

Tips for new programmers

I have been inspired by John D. Cook who does 11 tip accounts on twitter, each updated with a new tip, Monday through Friday.  There is a Unix tool tip, an Algebra fact account, and nine others. How cool is that?

Not being nearly as cool as him, I decided I’d just throw out a random tip now and then, since statisticians like anything with the word “random” in it.

If you got all excited about programming by watching some show about programmers let me tell you two things that Hollywood omits when you see those shots of the programmer typing rapidly writing code at 3 a.m.

  1. They are up at 3 a.m. because they have been working on that program since 11 a.m. that morning and the reason they started working at 11 a.m. is that they were up working on it until 4 a.m. the previous day.
  2. Although they are typing rapidly now, there was an hour of pouring through their code and cursing trying to find the bug before that 30 second spate of typing was filmed.

Buffalo

 

Given those two facts, here are some tips to keep in mind when you begin programming, as illustrated in the little snippet below from a program I wrote to shoot buffalo because a friend of mine asked me could I write a program to shoot buffalo. I have unusual friends.

 

“Draw” your program to be symmetric. This may sound funny coming from someone who failed art in junior high school and aced multivariate statistics in graduate school. I DON’T mean to make your code in rainbow colors (although I may write about that some other time). What I mean is that every time you do something that has a beginning and an end, whether tags in html, a DATA step in SAS or, in javascript (as shown below) functions, loops and if / else statements, indent what goes in the middle.

// DRAW EVERYTHING IN THE ARRAY 
	function drawall() {
		ctx.clearRect(0,0,cwidth,cheight) ;
		var i ;
		for (i=0 ; i < everything.length ; i++) {
			var ob = everything[i]  ;
			if (ob[1]) {
				ctx.save() ;
				ctx.translate(ob[3],ob[4]) ;
				ctx.rotate(ob[2]) ;
				ctx.translate(-ob[3],-ob[4]) ;
				ob[0].draw() ;
				ctx.restore() ;
			}
			else {
				ob[0].draw() ;
			}
		}
	}

Use comments liberally. I have had friends tease me about how many comments I put into programs, but I think this is one area where, if in doubt, put it in. Remember Eagleson’s Law,

Any code of your own that you haven’t looked at for six or more months, might as well have been written by someone else.

Put in whatever comments will make sense to you six months from now.

I don’t know if Eagleson actually said this or if he even exists. You can find some pretty funny quotes about programming in wikiquote, though, if you are in the mood.

Steal stuff. My most useful tip when you are beginning, and when you have been programming for decades is to re-use any good code you can get your hands on. When I first started I thought that was cheating, that real programmers wrote every line themselves, starting with a blank page. Was I ever stupid!

The code above is from a book on javascript. I took a program that shot a cannon and changed it to be one that used a rifle to shoot a buffalo on a hill. When you hit the buffalo, it flips upside down and is dead. If you miss, the buffalo comes off the hill and runs at you. You get another chance to shoot it dead.

I am kind of thinking other people have friends who ask if they can borrow a cup of sugar or wear their shirt, but that is not the point. My point is that half of the program I shamelessly copied from someone else for the price of a book on HTML5 I got on sale for $20 at Barnes and Noble.

My first thought whenever writing a program in any language is what do I have lying around that does something similar to that. If I don’t have anything very close, well, you can find lots of things on the Internet.  There are even (I know this is hard to believe) these objects fashioned from paper known as “books” that have code in them. Books are like a Nook or Kindle except that they dry out better after you drop them in the bathtub. Or so I have heard.

Similar Posts

4 Comments

  1. “Not being nearly as cool as him…”

    uhhh, ok. John D. Cook is pretty awesome, I’ll admit. I found him through you on twitter (and found you through Rowdy Ronda). I pretty much signed up for all 11 of his accounts immediately and am loving it. BUT, until he calls a U.S. Congressman a “lying-ass motherfucker” I’m gonna have to say you win the cool contest! Hahaha

  2. Aw, thank you.

    BTW, I met my husband, the rocket scientist, when I was trying to get in touch with my friend, whose father taught physics at UCR. The nearest person with an email account was this De Mars guy in physics at UCLA. (This was when the Internet was new.)

Leave a Reply

Your email address will not be published. Required fields are marked *