25 2 / 2009

Programming necessitates variables. There’s no getting around it. Some thing needs to remember the number of widgets or the name of a particular widget. Simple programs usually start with simple variable names:

a=3;
b="Deluxe Widget";

a and b are variables that hold the values 3 and “Deluxe Widget” respectively. As a program gets more complicated and the number of variables increases, the variable names get more complex. Many times the name should be more than one word, like “widget count,” but for various reasons, this is not allowed. So we programmers get clever with underscores:

widget_count=3;
widget_name="Deluxe Widget";

Or perhaps use camelCase (I guess they look like camels with all of the humps?):

widgetCount=3;
widgetName="Deluxe Widget";

Arguments over variable naming conventions are common. It’s petty, but so are programmers. Either way, something has to be done.

I like camelCase.

I rarely use it.

Why not? Because I use Emacs to edit my files. Emacs has keystrokes that allow you to delete a single “word” all at once. Hold down the alt key, hit delete and Emacs deletes an entire word. It also allows you to move your cursor forwards and backwards word by word. The problem is how Emacs defines what a word is. If I want to delete or change part of a variable name (which happens all the time) it works fine with underscore separated variable names, but not with camelCase. When I use camelCase naming, I’m resigned to constantly retyping entire variable names.

Until now! After the topic came up on twitter today, I decided that there had to be a solution. Emacs is built to be extended and it has been. There are modules and add-ons to do everything. Literally. You can even play Tetris in Emacs.

So I looked around and immediately found this. A minor mode for Emacs that provides key bindings for camelCase! I dropped it into my personal Emacs directory and set it up to load when ever any file was opened. This is done in my .emacs file (it’s LISP and a little ugly):

;;Include my personal Emacs Directory when looking for files to load
(setq load-path (cons "~/Dropbox/.emacs.d" load-path))

;;Load the camelCase file
(load "camelCase-mode")

;;Start camelCase-mode whenever a file is 'found' (opened)
(add-hook 'find-file-hook '(lambda () (camelCase-mode 1)))

That should have done it. However, when I started up Emacs, I got the following error:

Error in init file: Symbol's function definition is void: first

I dug into the camelCase file and found the problem buried in the function that maps the new key bindings:

(define-key map (first binding) (second binding)))

binding is a list that contains two things: a keystroke sequence and what should happen on that keystroke. What’s wrong here is that to get the first and second items, the functions first and second are called. These functions don’t exist within Emacs! Initially, I fixed this directly within the camelCase file. Then I decided that I’d rather leave that file untouched. In order to make it work, I needed to define first and second as functions that do what camelCase expects. So I added the function definitions to my .emacs file. The final result looks like this:

;;Include my personal Emacs Directory when looking for files to load
(setq load-path (cons "~/Dropbox/.emacs.d" load-path))

;;Define 'first' to return the first element of a list.
(defun first (argList)
  "Return the first element of a list"
  (nth 0 argList))

;;Define 'second' to return the second element of a list
(defun second (argList)
  "Return the second element of a list"
  (nth 1 argList))


;;Load the camelCase file
(load "camelCase-mode")

;;Start camelCase-mode whenever a file is 'found' (opened)
(add-hook 'find-file-hook '(lambda () (camelCase-mode 1)))

And that works just fine! I expect to start using camelCase variable names far more often now.

UPDATE: Original DISQUS Comments