Sat, 30 Jan 2010 21:39:00 GMT

BEncode Version 1.0 Released

Over the past two weeks I have been working on developing a BEncode library in ruby. BEncode is the protocol that Bit Torrent uses to communicate. Well today I just pushed the gem and posted the documentation. If you're interested in finding out more about this project you can check out my project page.

Fri, 25 Dec 2009 14:07:00 GMT

Fear of Committing

When I worked at my last company we used CVS. The central repository was holy and no one could commit changes until they had been reviewed. This led to a fear of committing. What that means is that developers would develop entire features without version control as a backup and a way to undo mistakes. If you went down one path and then decided it wasn't a good idea, you had to manually back out your changes. Also, with such large change sets, developers were almost certain to have conflicting changes. That meant merges were necessary, which could lead to more errors.

Not too long ago, I went to a talk about Git. Git is very different from CVS and even its successor Subversion. Git is distributed. What that means is that everybody has their own copy of the repository. If you want to commit you can. You no longer need to fear committing to the repository because you own it. That means you can commit more often. Git also tracks your history and keeps it intact between repositories. So if you've committed 50 times and another person pulls from your repository, they get all 50 commits. Compare that with CVS or Subversion, where everything is committed as one big change and it will become apparent why Git is much better at handling merges.

A lot of people will tell you Git is fast and Git is distributed. However, those answers are vague and don't describe why it is worth abandoning what you currently have. I think better selling points would be that Git allows developers to use their version control and makes merging the job of the version control rather than the developer. Those are what sold me.

Sun, 11 Oct 2009 17:42:00 GMT

Software That Talks

The other day in my Data Communication and Networks class my teacher asked us a very uninteresting question. What is necessary for a network application? The students in the class suggested a connection, a protocol to communicate, and data to transfer. The teacher said yes to each of those and then he opened up Firefox and downloaded a file. He asked us why does Firefox show a progress bar? Because network latency isn't zero.

He intended to teach us an important lesson about not assuming that communication is not instantaneous. However, every story has more than one moral and the one I got from his lecture was that software needs to communicate with the user. Why does Firefox show a progress bar? Because otherwise the user wouldn't know that something is going on, they would assume it was broken, and they wouldn't have faith in it.

Developers have the tendency to think of their software as a tool to accomplish a job. I think a better mindset would be to anthropomorphize your software. Pretend it is another person; one that your users will interact and communicate with and that will talk back. When I ask a person to do something I expect them to acknowledge what I said, give me feedback on what I asked them to do, and to tell me when they are done. If you treat your software like it is another person you begin to put constraints on it that you might not have done otherwise.

Thu, 10 Sep 2009 20:29:00 GMT

Where to Start?

When you run a race the rules are simple. You start at the start line and you finish at the finish line. Your only goal is to get from one point to the other. Software development is nowhere near as simple.

Yesterday an underclassmen friend of mine asked, "I want to build an application, where do I start?" Being in his position at one time, I can see where he's coming from. You have no defined start line or finish line and there are an innumerable amount of paths you could take. Schools do a very excellent job of showing you how to get between two points that they specify, but in the real world those points aren't nearly as well defined. That is why software development is more than just coding. Software development is about defining the constraints of the race and then, running the race.

Every project starts with a problem that needs to be solved. It is necessary to translate that problem into a set of goals. Once you have defined what your goals are, you have established a finish line. To pick your start line, select the most important goal that is the core of your project. For example, if you're creating a to-do list application, the most important feature is obviously the ability to create a list. Something like search may be nice, but it isn't critical. To build the path between start and finish, keep selecting a new start line after each goal is completed. Every goal is in essence its own race. Each race can be broken down into smaller goals. By breaking a project down into its most basic elements, it becomes much simpler to build than when thinking of the project as a whole.

Mon, 31 Aug 2009 15:30:00 GMT

Setting Up a Repository on GitHub

GitHub offers free open source project repository hosting using the git protocol. Git is specifically designed for distributed development of software.

Creating the Repository
To create repositories on GitHub, you'll need to create an account. Once you have an account you can go to your GitHub homepage and click the link to create a new repository. Fill in the name of your project and optionally, a description and project URL. Now you have a repository.

Setting Up Shared Keys
Git connects much like ssh and uses shared keys to confirm the remotely connected computer is who they say they are. For your local git to connect to GitHub it is necessary to generate a shared key and tell GitHub what it is. To generate you own shared key, follow the instructions below:
cd ~/.ssh/
ssh-keygen -t dsa
You can hit enter through all of the prompts. You will now have two files: id_dsa is your private key and id_dsa.pub is your public key. The contents of id_dsa.pub is what GitHub need to know. On your GitHub account page there is a link to add a public key. Click that and copy the contents of id_dsa.pub into the key field and add a name to designate what computer the connection will come from. Then click the add key button. You can now connect to GitHub.

Setting Up Git
You will need to tell git your user name and email so that it can appropriately attribute your code contributions to the repository. You can do this with these commands, filling in your own information where necessary:
git config --global user.name "My Name"
git config --global user.email "MyEmail@SomeHost.com"
Note: This has set the settings globally. It is possible to set this up per project.

Commiting to the Repository
Now you need to go to the root directory of your project. If you don't have one yet you should create it. Then you can use the following commands to commit your initial project to GitHub, replacing with your GitHub username and with your repository project name on GitHub:
git init
git add --all #only do this if you have files already
git commit -m 'first commit'
git remote add origin git@github.com:/.git
git push origin master
You have officially set up your local repository and set the master to your GitHub repository.

Working with Git
From now on you can commit to your local repository with the following command:
git commit -m "A message explaining what you did"
Then, to push it to GitHub use the following command:
git push

Sun, 30 Aug 2009 14:24:00 GMT

Javascript Key Events

I just recently spent a while learning the mechanics of key events in javascript. There are many incompatibilities between each of the various browsers, which makes the task somewhat difficult.

Getting the Key
The first problem I encountered was how to get the key from the event. Internet Explorer uses event.keyCode to store the numeric representation of the key. Mozilla historically used event.which, but now uses either event.keyCode and event.charCode depending on the circumstance. However, event.which is still supported and always contains the key code, whereas only one of either event.keyCode or event.charCode contain the key code. Because of these oddities, it is necessary to use some logic to figure out exactly what has the key we want. For that we do the following:


We can check if event.which exists. If it does we are on Mozilla and will get the even.which property to get the key code. If it doesn't exist then that means we are on Internet Explorer and we need to check the event.keyCode property.

Difference Between Key Events
Keyup and keydown are rather different from keypress. Both keyup and keydown's key codes map to the specific keyboard that the user is using. Meaning there is no consistent way to determine the key between any two computers. However, the keypress event behaves differently. Instead of the storing the numeric representation of the keyboard key, it stores the UTF-8 encoding of the character which is easy to determine across different systems. The lesson here is that if you want to know if a key was pushed or released, but don't care what it is, you can use keydown and keyup. When you must know the key value that is being typed using keypress is the only reliable option.

Shift Key
All browsers have support to figure out if the shift key is being pressed for each of the event types. The problem is that there is no way to tell what the original unshifted key is, because the event will only tell you the shifted key code. For letters, it is simple to convert back to lowercase with String's toLowerCase() method, however, for the number and symbol keys, the only way I could think of is to have a translation table. As a result I came up with this:
However, I'm using an American standard keyboard. If another keyboard setup has other keys that are modified by the shift key, this will not solve the problem.

I discovered all of this while writing an extension to the Prototype framework. I'll be releasing this on GitHub under the name of blacksmith in the near future. The project home will reside here.

Fri, 28 Aug 2009 23:09:00 GMT

Creating a Rails App (Ritter2 Part 1)

Note: This guide assumes you already have Ruby, Rails, and MySQL set up. For an excellent guide on how to do so look here.

Create a Project
This will be the first part in my series on how to create a Twitterish Rails application. The steps here are for creating this application, but they can also generally be used for any Rails application. Just about every Rails application has the same basic setup. The first thing you want to do is actually create your application. On the command line type:
rails -d mysql ritter2
That will generate all of the basic files you need for your application. I specified that I wanted to use MySQL as my database. The default for Rails is SQLite.

Start Your Application
At this point you have a working Rails site, but it doesn't do much. Go into your ritter2 folder and enter the following command:
script/server
That will start up an instance of your application, which you can access at http://localhost:3000. Nothing too exciting here, except that you have a running server in two commands. Go ahead and kill the server with Crtl-C.

Setup a Database User
Now we're going to setup the database for our Rails application. The first thing I always do is generate a new user for my Rails application to use. You can do that with the following:
mysql -u root -p
You will be prompted for your MySQL root password. After that you can enter the following commands:
grant all on *.* to  "ritter2"@"localhost" identified by "ritter2";
exit;

Setup Rails Communication to the Database
Now we need to tell Rails how to connect to the the database. We do that in the database.yml file in the config directory of our application. Open that up and put in the username and password for each database. Here's an example:

Create the Database
Now we want to tell Rails to create the databases. To do that run the following command from the application root:
rake db:create:all
That will create your databases for you.

In the next part of this series I will go through some of the key aspects of setting up authentication for a Rails application.

Sun, 23 Aug 2009 20:21:00 GMT

Ritter

Once upon a time I wrote a Rails application called Ritter. It was for my Principles of Information Systems class and in my opinion exhibited everything that class was about. It is a twitter clone (approximately) smashed together with my alma mater, RIT. Anyways, the reason I bring this up is because I'm planning on writing a series of articles on how to write a similar application.

If you want to see a live demo you can check it out here.
The source code is available on github.