Wednesday, November 10, 2010

Who Doesn't Have To Worry?

Sixty Minutes did a recent story on unemployment in Silicon Valley. Some discussion boards are full of American software engineers complaining that Indians, from the subcontinent not the Navaho Reservation, are taking their jobs. There's a trend to advise young people to go into medicine or law rather than computing.

Several things have contributed to this and offshoring and immigration being only one of many. First, during the dot com bubble more people got into computing than the field could absorb. Second, there is a lot of good software available all ready. Consider, in the mid eighties, someone working for a small business might need a program written to pull data out of a DBase database and perform some analysis on it. Today, the same data would likely be kept in Excel format and easily analyzed by the same person who generated it. The code to do so is already present in Excel and doesn't have to be written by someone else.

This use of building blocks constructed by others even extends to entertainment software. Take Second Life, for instance, Linden Research didn't develop code to simulate physics within their virtual world instead they used the Havoc Physics Engine. While integrating Havoc with the rest of their technology was probably hard, it was likely easier than writing their own physics engine from scratch.

In science, also, there are excellent pre-written tools. Scientists don't need to write or hire someone to write code for storing DNA sequences, they use MySQL. They also often don't need code written to extract specific statistics from their experimental data. Those algorithms are likely to be available as part of numerous statistics packages, including free packages such as R.

So, who doesn't have to worry? The answer for me would be people who work at a high level in computer security as they have human opponents who are always trying to come up with new ways to exploit the internet for ill-gotten gains, software architects with a track record of successful projects, and, in some cases, people with specialized skills such as working with embedded systems. The problem is that you can't just go school to land of these jobs. Everybody I've talked to with such skills has been very smart. My expectation is that at the highest levels software will still be a very good field to be in even in the United States, but that if you are not one of the smartest people around you will trouble making a long term career of it.

As very smart people who are interest in computers rather than something else are relatively rare, despite existence of many average programmers who have less work than they want, companies will still find themselves paying more than they'd like for the kind of talent they need. Thus, I expect both most programmers and many employers of programmers to be dissatisfied with the market for some time to come.

Tuesday, November 9, 2010

Let's Forget about UIs For Now -- Opensim

I was going to post about Java Swing as a UI frame work but I got bored with that. Instead I worked on getting an instance of OpenSimulator running on my Mac Pro.

I used use Secondlife, the virtual world developed by Linden Research, a good bit. I am not particularly interested in online role playing in any form, although I used to do table top, and buying tons of virtual clothes doesn't interest me that much, though I admire the artistry behind them.

I enjoyed the surrealism of SL and wanted to find out what went on behind the scenes to make a virtual world work. I quickly learned the basic techniques for building objects out of secondlife primitives and perused the SL Wiki to learn about the basic structure of Linden Scripting Language (LSL) scripts.

I then hit a bit of a wall. To make good looking content you have to be able to upload your own textures and animations into Second Life. Doing this requires making a micropayment to Linden Labs, something I didn't want to do. Also, my children, God bless them, discovered how to use my account to buy virtual goods, something I didn't want.

I probably could have come with a better use of my time, but to be able to privately develop Secondlife content, I decided to load OpenSimulator, an open source package providing similar functionality on to my machine. The first problem with this was that OpenSimulator is written in C# a Microsoft developed language not native to the Macintosh. Now, C#, like Java, compiles not to native machine code, but instead to code for a virtual machine that runs on top of the native hardware.

To run this on a Macintosh, the OpenSimulator web site suggested, you need a program called mono to act as an interpreter for the compiled C# code. I downloaded and installed this and then downloaded an OpenSimulator tar ball. Once I extracted OpenSimulator from the tar ball and tried to run it, it failed, something not unexpected with open source software, which typically doesn't provide a lot of handholding.

Digging around in the opensim.ini configuration file and the OpenSimulator wiki, I discovered the reason for this. OpenSimulator like most modern software uses a relational database to store all sorts of information. Out of the box it was configured to use the SQLite database, the same one provided in IPhones and Droid smart phones.

A version of of SQLite does come with MacOS, however, OpenSimulator is not compatible with it out of the box. The three choices available were to, configure it work with the older version on the Mac, upgrade my Mac's SQLite software, or install MySQL, an open source database widely used in industry to store small to medium amounts of data. As I'd worked with MySQL before, I chose that route. Along the way I found out that though MacOS Server has MySQL installed out of the box, vanilla MacOS does not. Fortunately, it was easy enough to install from the MySQL website.

Since OpenSimulator seemed to want to log in to a database named opensim under MySQL, I created this and also created an account under MySQL with all privileges for that database. After more futzing around with OpenSimulator configuration I was able to get two regions, a few square miles of virtual land, up and running. What will I do with it, I'm not sure yet.

Thursday, October 7, 2010

How to actually make things run

My example code has been in C and Java, however I've not said how to get a computer to run either a C or a java program.

To run java code you first place it in a file with the extension ".java" on the end.
This file must contain one and only one class declared public and that class must contain a method named main which is declared like this:

public static void main(String args[])

The file also must contain a package declaration at the top like:

package MySillyJavaPackage;

Once you have these, you call the java compiler, either through using the build menu item in eclipse or on the command line. If you use the command line, you type "javac yourfile." If your program compiles without errors, the compiler will produce a directory with the name declared in your package declaration. Inside this directory their will be a file with the extension ".class" and the same base name as your original file. To run it, type "java PackageName/yourfile." Do not use any extension with name of the file you want to run.

With C you must have a main function. As pure C is not object oriented the main function is not part of any object. The incantation for compiling your file varies. If you are using GNU C on the command line, it is gcc filename. If your file is error free the compiler will produce a file called "a.out" in the same directory your source file was in. To run it, just type "./a.out" and away you go.

Now you may wonder what good all this does, after all I've given no way to get output from your code. I've delayed this longer than I should have because I wanted to discuss library routines and input and output at the same time.

To have your program print anything, you need to include or import prewritten code that does output for you. How does that code work? It directly manipulates the hardware interfaces connected to your screen and keyboard. Writing it is both highly specialized and fairly tedious for most people, which is why if you aren't an embedded systems hacker you usually don't bother with it.

Anyway the library used for input and output in C is called stdio. To gain access to it you use a preprocessor directive. The particular directive is

#include<stdio.h>

Which means get the file called "stdio.h" from one of a few standard places. You can use this include other libraries such as the C Math library. Included in stdio are definitions for the C functions printf and scanf. Printf is used like

printf("%d silly dogs played with the ball\n",&dogs);

The first argument to the printf function is called the format string. %d means insert an integer value into the format string. The "&" in front of the variable dogs, means that we are passing it's address rather than it's value into printf, another example of a C pointer.

In Java output is a little easier. Instead of using a format string you would just type

System.out.println(dogs," silly dogs played with the ball\n");

The "\n" used in both examples is just a sign for a carriage return.

Now, while raw text output is perfectly good for some things like program logs, most programs written today have other, more intuitive, ways of interacting with a user. How to make a GUI, a graphical user interface will be discussed in my next post.

Monday, October 4, 2010

Who can see data?

Now one way to program in C is to put all the data outside of your methods, called functions in plain vanilla C. To do this C you write a bunch of declarations like:

int age;
double temperature;
char Name[20] = "Maria\0";

By doing this you are declaring what are called global variables. These can be accessed by any function declared in the same file of program code and ,if certain conventions are followed, also by functions declared in other files. At first glance this seems like a good idea, as it easy to pass data between your functions, but it has pitfalls. Without going over the code with a fine tooth comb it's impossible for someone reading your code to know if a particular function alters a particular global.

One way to deal with this is to declare local variables:

int AverageThem(int a, int b)
{
int c;
c = (a + b)/2;
return c;
}

In this pointless snippet of code a,b, and c are all local variables
in AverageThem. Outside of AverageThem those particular storage locations are not associated with the names a,b, and c or the type int. In fact a,b, and c are recreated every time the computer executes the AverageThem function. In C you can declare local variables in every block. A block is anything between two curly "{}" brackets so

main()
{
int a = 2;
{ int a = 3;
printf("%d\n",a);
}
printf("%d\n",a);
}

First prints a "3" and then a "2."

Now suppose you have a different function:

GreetUser(char *Greeting)
{
static char CurrentGreeting[50];
if (Greeting != NULL) {
strcpy(CurrentGreeting,Greeting);
}
puts(CurrentGreeting);
}


Because this function will behave badly if Greeting is longer than fifty characters or is not terminated by a NULL character don't copy it. However, it illustrates the use of the keyword static what static does is cause the value stored in the string CurrentGreeting to persist between in invocations of GreetUser and only change if a non-null pointer is past to the function.

What is this pointer thingy which hasn't been mentioned before? It's a variable which, instead of storing a numerical, character, or object value, stores the location of that value. In C an array is equivalent to a constant pointer. While pointers are useful, and are used a lot C programs, I intend to use C examples to illustrate programming with C-like languages in general so I won't go into C pointers in detail. Puts and strcpy are simply C library functions. They, again, have certain problems and would probably not be used by a contemporary programmer, I'm just using them here for simplicity's sake.

Now object oriented languages have a different approach to deciding who can see information and who can't. While they still have local,global,and static variables. The creator of an object gets to pick what fields and methods are visible to other code. To do this a C++ or Java programmer uses the keywords private,public, and protected.

For example in my last post I created class PetRecord

public class PetRecord {
Date Birthday;
String Name;
String OwnerLastName;
Date RabiesDue;
public void UpdateRabiesDue(Calendar LastShot);
public String GetOwner() {return OwnerLastName;}
public void SetOwner(String NewOwner);
};

The public modifier means that any method could contain a call GetOwner and the other methods I defined. It doesn't whether that method is part of class PetRecord or not. The data fields, however aren't declared public. Therefore they can only be seen by methods that are part of the same Java package as PetRecord. If you don't want anything outside of PetRecord to see them a better choice might be to declare them private.

I have no static methods or fields in the version of PetRecord above. Let's add one.

public class PetRecord {
Date Birthday;
String Name;
String OwnerLastName;
static integer PetCount = 0;
Date RabiesDue;
public void UpdateRabiesDue(Calendar LastShot);
public String GetOwner() {return OwnerLastName;}
public void SetOwner(String NewOwner);
public PetRecord(String theOwner,String theName) {
Owner = theOwner;
Name = theName;
PetCount = PetCount + 1;
}
};

The method I added, called a constructor initializes a PetRecord at the time one is created. The variable PetCount is also incremented. Why do this? Because PetCount is a static variable there is one PetCount shared between all instances of the PetCount class. By incrementing it, we can keep a count of the number of pets we have.

There are also a protected access specifier in Java and C++, but it isn't used as much as public or private.

In my next post I'm going to back up from the micro-level stuff I've been discussing and talk about how you can actually get programs to compile and produce output.

Friday, October 1, 2010

More About Data And A Bit About Methods

In my last post I had something called 'a'. 'a' was a variable. In computing, unlike math, a variable is not a name for an unknown quantity, instead in denotes a particular location in the computers memory. What a variable can contain depends on the the language you are programming in.

In strongly typed languages, such as C and Java, you tell must declare your variables, telling the computer what kind of thing it will contain before you use it. You can also define a variable when you declare it, setting it's initial contents to what you want it to be. For example:
int a = 23;
Now, in computer hardware numbers that humans would handle similarly can be handled very differently. Int means that the variable should contain an integer, a number with no fractional part. If the number must be capable of holding a fractional part, it should be declared as float or a double if you are using C or Java.

One can also declare variables to hold text data as well numbers. In C one would do so with the lines like:
char C = 'a';
or
char C[10] = "Told ya!\0";
Now we come to something more interesting. Why did I put '[10]' in the second line and '/0' at the end of "Told ya!\0"?

In the second example C is an array, chunk of several consecutive memory locations. The [10] tells the C compiler to reserve enough memory for ten alphanumeric characters. The '\0' represents a null character necessary to tell some C functions that the character string is complete. The use of such special values was commonplace in the late 1960s when C was invented but these days is considered an obsolete approach.

So we have arrays, yay! Now what good are they actually? Well, with arrays one can program something like:

/* Toy example, first 100 Fibbonaci numbers */

int fibs[100];
int index;
fibs[0] = 0; /* C array indices start with 0 not 1 */
fibs[1] = 1;
for(index = 2 ; index <= 100 ; index = index + 1) { fibs[index] = fibs[index - 1] + fibs[index -2]; }



The stuff inside the "/* */" delimiters are comments, bits of text met to be read by humans that computer removes before processing the actual program code. By using arrays a programmer can elegantly have a computer repeated perform the same operation on large blocks of data.

But what if several different types of data should go together. In modern terms you use something called an object. Here's one, declared using Java rather than C syntax since plain, vanilla C, while still excellent for some purposes, doesn't really have objects.

public class PetRecord {
Date Birthday;
String Name;
String OwnerLastName;
Date RabiesDue;
public void UpdateRabiesDue(Calendar LastShot);
public String GetOwner() {return OwnerLastName;}
public void SetOwner(String NewOwner);
};


Once you've defined a PetRecord, you can declare variables, including arrays of that type.
To access individual fields of a PetRecord, you'd write code like:

PetRecord Fluffykins;
Fluffykins.UpdateRabiesDue(Calendar.getInstance());

What in bleep are things with parentheses on the end? Getowner() and UpdateRabiesDue() are methods called functions in C. In Java every method is attached to a certain object of class of objects. This isn't the case in C or even C++ where standalone methods, called functions can be declared. What's special about methods is that the same method can be called with different parameters. For instance if Fluffykins is given to Mrs. Jones, because it urinated on the carpet too much at its old house you can call "Fluffykins.SetOwner("Jones");".

Now you may have noticed that the methods in my PetRecord class had the word "public" as part of their declarations. What exactly is that about? My next post will discuss the notions of scope and encapsulation.

Thursday, September 30, 2010

More On Programming

I learned programming in the nineteen seventies and nineteen eighties.  Since then a lot has changed with the advent of graphical user interfaces and object oriented programming, but the basic constructs of procedural programming are still relevant.

The most basic operation is the assignment, usually denoted with an "=" sign.  For instance in C,Java, Perl , and Python , the statement "a = 2 + 5" means stick the result of adding 2 and 5 into a memory location named "a."

Another of statement controls in what order pieces of your program are executed. C and C-like languages have several of these.  "if..else", "while","do..while","for", and "switch" statements all exist.

If and switch statements are used to choose between two or more alternative paths of execution.  For example:

if (a <= TOO_BIG)   {
   a = a + result(a, do_some_stuff(a,b, uncertainty);
 } else {
     ErrLog.write("a too big\n");
     clean_up()
 }

switch(a)  {

case 1: do_thing_1();
           break;

case 2: do_thing_2();
           break;
default:

}


While, do..while , and for statements are used to repeatedly execute a block of code until a certain condition is met.  For example:
for (a = 0 ; a <= 10 ; a = a + 1) {
    do_some_stuff();
}
a = 0;
while ( a <= 10) {
   do_some_stuff();
   a = a + 1;
}

a = 0;
do {
   some_stuff();
   a = a +1;
while ( a <= 10);

As you may have noticed all three looping constructs are more or less equivalent in meaning but in any given situation, one of the three is likely to be more intuitive than the others. Before the nineties, these constructs plus function calls and data aggregates , which I'll cover in my next post, were close to all there was to programming languages.  Once I cover these basics, I'll start getting into more modern and maybe more interesting ideas, involving GUIs, object oriented programming, and event driven programing. 

Wednesday, September 29, 2010

Multiple Strategies for Learning to Program on Your Own

Programming at a basic level is easy to learn on your own.  There are many references on the Internet.  Things I've done or heard about.

1. PostgreSQL is easy to download for all platforms, Oracle Express is free for download, and MySQL comes with MacOS.  Why learn to use an SQL database? Because most systems use them to store persistent data.

2.  Installing the optional XCode tools on a Mac is easy and gives you access to C, Objective C, C++, and Java compilers.  For a few bucks you can get Apple's professional compiler and intregrated development environment (IDE.)  Another IDE I've used Eclipse, which is again open source and have many useful features for Java development.

3. Even without XCode, Macs come with Python, Perl, and Ruby installed if you want to code in those languages.

4. Finally, one can program JavaScript in your browser itself.

All these tools can help you master the syntax and semantics of a programming language.  Of course, once you know a language or languages you need also to learn the best ways of expressing ideas a programming language, tracking and fixing bugs, and testing your code.  All go over some tools for that in another post.