Sunday, November 25, 2007

This Copyright Story is Copyrighted and Links to Copyrighted Material

ArsTechnica brings us a couple of holiday week stories about copyright and what's going on with it. First up, a law professor estimates that he posts over twelve million dollars worth of copyright violations every day. For some reason, he concludes from this that copyright law has gotten out of hand. Meanwhile, on the campaign trail, the various presidential candidates are being asked for stricter enforcement and even stronger copyright laws.

And since it's kind of a downer to end a wonderful holiday by posting about this stupidity, I leave you with a link to the wacky lolcats.

Monday, November 19, 2007

Syntax Versus Solution

But which came first, the Word or the Thought behind the Word?
--Lorien, Babylon 5 "Whatever Happened to Mr. Garibaldi"

Our language frames our thoughts, and this is as true for computer languages as it is for any other written form. Like everything else under the computer science umbrella, programming languages are evolving quickly. If you want to keep up as a programmer, you have to be prepared to evolve too. Today I'm going to show a trivial example of the sort of things that we run across, when moving from one language to another. For this exercise, I'll start off with a simple C++ program, and move it to Ruby.

Statically typed, compiled, and designed for efficient computer cycle usage, the C family of languages (C, C++, Objective-C, etc.) is the only game in town for whole classes of applications from tiny devices to huge online games to operating systems. A simple program written in C++ looks like this:


#include <iostream>
#include <cstdlib> //For random number generator.
#include <ctime> //To seed the random generator.

int d100(){
  return ( (rand() % 100) + 1 );
}

void main(void){
  srand(time(0));
  
  int roll = d100();

  char * type;
  if ((roll >= 1) && (roll <=5)){
    type = "stormy";
  }//if
  else if ((roll >= 6) && (roll <= 20)){
    type = "rainy";
  }//else if
  else if ((roll >= 21) && (roll <= 40)){
    type = "cloudy";
  }//else if
  else if ((roll >= 41) && (roll <= 60)){
    type = "mostly cloudy";
  }//else if
  else if ((roll >= 61) && (roll <= 95)){
    type = "fair";
  }//else if
  else if ((roll >= 96) && (roll <= 98)){
    type = "windy";
  }//else if
  else if (roll == 99){
    type = "snowy";
  }//else if
  else{
    type = "raining toads";
  }//else

  cout << "The weather today will be: " << type << endl;
}


Ruby's dynamic typing and interpreted code belongs to a very different paradigm than C++, but it is a testament to the pervasiveness of C that when changing the program to use the Ruby language, you can use almost the same syntax:


def d100()
  return (rand(100) + 1)
end


roll = d100()

type = nil

if ((roll >= 1) && (roll <= 5))
  type = "stormy"
elsif ((roll >= 6) && (roll <= 20))
  type = "rainy"
elsif ((roll >= 21) && (roll <= 40))
  type = "cloudy"
elsif ((roll >= 41) && (roll <= 60))
  type = "mostly cloudy"
elsif ((roll >= 61) && (roll <= 95))
  type = "fair"
elsif ((roll >= 96) && (roll <= 98))
  type = "windy"
elsif (roll == 99)
  type = "snowy"
else
  type = "raining toads"
end#if

puts("The weather today will be: " << type)


Whee, we have a program in two languages that works identically and looks pretty much the same too. What exactly have we gained here? Nothing, yet. Just as you can abuse a written language to produce grammatically acceptable gibberish (bureaucratized corporate memos come to mind), so too can a computer language be used less than efficiently. And this program isn't going to be considered good Ruby style by any regular user of the language. To get things done in a more Ruby-ish way, we use the language's more flexible case statement, built in range support, and statement evaluation behavior to knock the whole thing down to a single expression:


puts("The weather today will be: " << 
  case (rand(100) + 1)
    when 1..5 then "stormy"
    when 6..20 then "rainy"
    when 21..40 then "cloudy"
    when 41..60 then "mostly cloudy"
    when 61..95 then "fair"
    when 96..98 then "windy"
    when 99 then "snowy"
    else "raining toads"
  end#case
  )


And that is definitely not something I would want to try with C++.* Such a trivial example doesn't give anyone great insight into the languages in question. However, if you look at where we started and where we ended, you can begin to see how working regularly with either language could make you tend toward slightly different ways of solving the same problem. Luckily, programming languages are vastly easier to learn than human languages, but just learning the syntax of a language often isn't enough. As programmers, we should strive to be as aware of the mental trade-offs needed to make the best use of the different languages as we are of the technical trade-offs.

*In the tradition of lecturers everywhere, I note that it is entirely possible to write this program as a single expression in C++. Actually doing it, I leave as an exercise for the reader. (Feel free to post your solution in the comments!)

Wednesday, November 14, 2007

Power, Robots, Disruptive Tech, and also Zombies

I realize that I haven't had a post about my desire to see chemical batteries go the way of the dodo since switching over to Blogger. Well, it's time for that to change as ultracapacitors begin to come into use for some tasks.

In other power news, it looks like nuclear may be poised for a comeback. An application for the first new nuclear fission plant in three decades was submitted in (where else) Texas.

And with all the new power generation and storage technology coming, what better thing to power than autonomous, urban-driving-capable robots. Coming soon to a military convoy near you. Who knows, they may eventually even give truckers a reason to worry about their jobs.

Unfortunately, I don't have a clever transition to lead up to mentioning the awkwardly named Eee PC from ASUS. This book sized wonder is a pretty obvious example of the first generation of a couple disruptive technologies combining that could eventually lead to an ultra-mobile class PC that is inexpensive enough to undercut the laptop market. I don't recommend being an early adopter, but I for one will be looking hard at the second and third generations of this device as a possible second computer.

Finally, if all this high tech seriousness isn't good enough for you, the folks of the Archaeological Institute of America show they have a sense of humor by publishing the first historical evidence of zombie attacks. They included self-defense techniques for stopping zombies using equipment normally found at a dig site, just in case.

Headline Hunting

"Former pilots and officials call for new U.S. UFO probe." Now that's a good idea, ask the UFOs to probe us...

But seriously, the article states that having a government agency investigating UFOs would reduce anxiety. I tend to think that having the government investigate things under the UFO title just lends credibility to the UFO believers. What do you think?

Sunday, November 4, 2007

Bullet List Considered Slightly Less Boring than Explanation

I had originally intended to write a nice overview of how Blogger templates worked, and how I was changing mine from the default. Luckily for you, I realized along the way why nobody writes about this kind of thing: it’s rather boring. But I don’t want this to be an empty post, so in case there is anyone out there who wonders about making simple modifications to a template, here’s what I did to get from the default Blogger template Scribe to what you see now:
  • Changed all non-link text colors to black.
  • Changed link colors to be something closer to the standard blue and red scheme.
  • Changed the default Georgia, Times New Roman, Sans-Serif font progressions to Verdana, Helvetica, Ariel, Sans-Serif.
  • Changed the body background to black with no image.
  • Removed the side borders from the outer-wrapper div and changed it to a relative width of 90% instead of a fixed pixel size width. Changed its color to burlywood.
  • Removed the background images from divs main-top, main-bot, and wrap2 and set the background color to antiquewhite. Changed their fixed widths to 100% relative.
  • Added text-align: center to h1 and .Header .description.
  • Changed #main's fixed width to 75% relative.
  • Changed #sidebar's fixed width to 25% relative.
  • Removed the background images from #header and #footer and replaced them with a burlywood border at the bottom and top respectively.
  • Removed all formatting for in-post lists (both li and ul).
In the process of making these changes, the Firefox plug-in Firebug was incredibly handy in both exploring the format of the template and testing changes. I used it to view the layout of the CSS and make temporary modifications in-place to see how the page would look. The tool is actually capable of much more, but even these simple functions were an enormous boon to figuring out the template and coming up with a decent (I hope) color scheme.

As always, there will probably be more changes along the way, but that doesn't mean I will subject you to having to read about them.

Also, in a fit of insanity, I have turned on comments for the 'blog. Behave yourselves.

Headline Hunting

"US seeks to remove pirates from ships." Yar. Ye ken try it, but we might keel haul ya.

But seriously folks, these are actual modern pirates endangering real people, and it isn't a joking matter. No matter how much comedy gold there is in that mine.