Archive for May, 2007

h1

Are design patterns a code smell?

May 29, 2007

Are design patterns a code smell? Are design patterns bad? That’s the rather bold claim made by Stuart Halloway in this post over at Relevance. At first blush, Stuart seems to have a pretty good case. As he points out, most if not all of the design patterns in the classic “Gang of Four” book are tightly tied to a particular family of programming languages, languages similar to Smalltalk, C++, and Java. Many of these patterns supposedly disapear or become trivially simple in more powerful programming languages like Lisp or Scheme (or I’m assuming, Ruby).

That may be true, but the conclusion drawn, that design patterns are a code smell, is just plain wrong. Design patterns, like any tool, can be misused. And the misuse of a tool is most definitely a code smell. Many of the design patterns in the GOF book probably are inappropriate for use in a functional language like Lisp or Scheme. However, I think an expert on either of those languages could easily write a book about design patterns in functional software design! Think of all the patterns that exist around when and how to use recursion, when and how to use macro’s, the use of higher order functions, etc.

h1

Some motivation (hopefully) to learn emacs

May 21, 2007

Those of you who are regular readers of this blog will know that I’ve recently begun trying to learn how to use the emacs editor, and have been blogging a set of emacs tutorials based on what I’ve learned so far.  There’s nothing quite like a teacher that’s only a couple chapters ahead of the students right? :-)

One thing I realize I haven’t communicated particularly well is *why* I’ve decided to learn emacs, much less why *you* should learn emacs.  So today, I’m going to post links to a few of the articles and screencasts that helped to inspire me to learn emacs.

First up is an awesome screencast from  Marshall Vandegrift where he quite ably demonstrates how slick a fully customized and turbo’ed out emacs can be.   His screencast demonstrates emacs as a Ruby on Rails development environment, with specific comparisons to featurs of Textmate, but most of the features he demonstrates are available in any language supported by emacs.

Another good Rails on emacs screencast is this one from the author of one of a very cool Rails minor mode for emacs.

Here’s a third emacs screencast.  This one from Derek Slager demonstrates the how to reformat a really ugly CSS file using some basic emacs features, mostly regex’s and macros.

Derek also has a great post called “The Case for Emacs” where he explains his reasons for preferring emacs as an editor/IDE.  One of Derek’s major points, that with emacs you can use the same editor and the same bag of tricks for any language (java, C#, Ruby, Erlang, etc) is definitely high on my list of reasons for learning emacs.  I like experimenting with new languages, but switching IDE’s is a pain, and once you leave the mainstream languages and start playing with languages like Erlang, Haskell, Lisp, etc. the situation is even bleaker.  Unless you use emacs. :-)

Last but not least is the always enlightening, entertaining, and certainly verbose Steve Yegge.  Steve’s post on Effective Emacs was probably the final catalyst that really got me off my butt and made me decide to learn emacs.  I found items #4 and #6 on Steve’s list to be particularly helpful in understanding why the primitive looking emacs might hold some productivity secrets that newer IDE’s still haven’t caught up with.

Well, there you go. Five resources to (hopefully) motivate you to learn emacs.

h1

XKeymacs

May 21, 2007

If you’re following along with my Emacs tutorials (or even if you aren’t) and you’re looking for a good way to practice your emacs keybindings, you might want to try XKeymacs. It’s an interesting little app that’ll enable emacs keybindings pretty much everywhere in Windows, or just in specific Windows programs if that’s what you’d prefer. But it’s not just limited to providing emacs keybinding in Windows. It’s also got surprisingly flexible capabilities for doing just about any kind of keyboard remapping you can think of. I highly recommend it.

h1

Cut and paste with Rxvt

May 17, 2007

A while back I posted about using Rxvt with Cygwin.  As I mentioned then, using Rxvt instead of the default Cygwin shell solves a number of usability problems with the Cygwin shell, and gives you a much more comfortable, unixy shell environment.

But one thing that can be tricky about Rxvt is it’s cut and paste behaviour, which is quite a bit different than most Windows users are used to.  You can’t use the Windows standard Control-C and Control-V keystrokes to cut from and paste to Rxvt.  So what do you do?

Like many unix apps, Rxvt automatically copies anything you highlight with the mouse to the clipboard. Click and drag across a section of the Rxvt window with your mouse to highlight the text you want and presto! it’s on the clipboard.  There are also a couple of mouse/clipboard shortcuts in Rxvt: doubleclicking in the Rxvt window will highlight the word under the mouse cursor and tripleclicking will highlight the entire line.

The tricky part is pasting into Rxvt.  Instead of using Control-V, use Shift-Left Mouse click.   Odd isn’t it?  But trust me, you’ll get used to it.

I’ve seen a couple of posts on the net about editing .inputrc to re-map the clipboard paste function to Control-V, but I’ve never been able to get it to work quite right.   If anyone out there has figured it out, I’d love it if you could help me.  Please email me or post in the comment section below.

h1

Learning emacs part 4: buffers, windows, and frames

May 4, 2007

So far, we’ve learned how to open files, save files, and navigate around a file. But it’s pretty lame to only be able to work on one file at a time. So let’s look at how to work with multiple files in emacs.

Recall that emacs was created waaaay back in the almost prehistoric times before modern windowing interfaces. Why is this important? We’ll see shortly.

But first, lets talk about buffers. In emacs, each file you open is contained in a separate buffer. However, not every buffer contains a file. You can also have an unlimited number of scratch buffers, which are buffers that haven’t been saved to a file. Additionally, there are other specialized buffers, like the minibuffer, buffers that contain the emacs online help system, buffers that are attached to the input/output of a shell or other process, etc.

Buffers are displayed in windows and frames. And here is where the antique nature of emacs becomes important. Remember that emacs originally didn’t run in a windowing GUI. Instead, it ran full screen in a character mode terminal. So in emacs terminology opening a new window didn’t mean creating an overlapping, independent window like you’d expect in a modern GUI. Instead, it meant splitting the screen horizontally and/or vertically two or more times. Within each “window” you can either display separate buffers, or you can display multiple views into the same buffer. What everyone else in the world calls windows are called “frames” by emacs.

So now that we know what buffers, windows, and frames are (in emacs terminology at least), how do we use them? To begin, lets open the alice1.txt file that we worked with in the last tutorial. If you don’t still have the file saved somewhere, go ahead and download it from the link above. Open it in emacs (C-x C-f). Congratulations! you’ve just created a buffer. In a moment we’ll look at how to create more buffers, but first let’s look at how to work with windows.

Emacs Windows

It’s important to learn how to work with emacs windows, as many emacs command will open windows to display their output or to accept input or interaction beyond what can be done in the minibuffer.

The simplest way to create new emacs windows is to split the current window in half, either horizontally or vertically. The commands to do so are:

C-x 2: Split vertically
C-x 3: Split horizontally

When you split the current windows into two windows with C-x 2 or C-x 3, you see that each of the new windows contains a view into the same buffer (in this case, the buffer containing the alice1.txt file). You can scroll around in each of the windows independently, but because they are two windows into the same buffer and not two buffers, any change you make to the text in one window will also affect the text seen in the other windows. Go ahead and try it. Scroll the buffer in each of the two windows so that you’re looking at the same line of text. Then start typing. You’ll see the characters you type “echoed” in the opposite window. Pretty cool huh?

Now that you have two (or more) windows open, how do you switch between them? The command C-x o (that’s a lower case O, not a zero) moves the cursor to the Other window. Using C-x o repeatedly will cycle the cursor through all of the current windows.

To close the current window, type C-x 0 (zero this time, not O). To close all windows except the current one, type C-x 1.

Emacs Buffers

Of course opening multiple windows into the same buffer is only of limited utility. I’d be rather disappointed if that’s all we could do, but like most editors, emacs lets you do a lot more than that. You can open a new file, thereby creating new buffers, in either window, and you can switch which buffer is displayed in your current window.

To list what buffers are available, use the list buffers command, C-x C-b. This command will split open a new window. In that window is displayed the list of currently open buffers. It also displays some additional information about each buffer as well. The buffer list displays 6 columns. From left to right they are:

Column Contents
M An asterisk (*) is displayed in this column if the buffer has been modified since it was last saved
R A percent sign (%) is displayed in this column if the buffer is read-only
Buffer The name of the buffer
Size Size of the buffer in Bytes
Mode The Major mode active in the buffer. We haven’t discussed modes yet, so don’t worry if this column doesn’t make any sense just yet. It will soon. :-)
File The name of the file, if any, load into the buffer

There are two ways you can switch to a different buffer. While the buffer list is displayed, you can move the cursor to the line of the buffer you want to switch to, then press enter. That will switch the buffer displayed in the window that the buffer list was previously displayed in. If you want to get rid of all other windows, hit C-x 1.

The other, quicker way to switch buffers is with the command C-x b. This command will prompt you in the minibuffer for the name of the buffer you want to switch to. You can type the complete buffer name, or you can type part of it and use the standard emacs tab completion.

You can also close a buffer (emacs calls it “killing” the buffer) with the command C-x k. If there are unsaved changes in the buffer you’ll be prompted to save the buffer first. If you want to save any unsaved changes in all open buffers, use the command C-x s.

Notice the similarity between the keystrokes for the switch buffers command (C-x b) and the list buffers commands (C-x C-b). Emacs is known for having a rather sharp learning curve, but these similarities between the default keybinds for related commands does help a little.

Emacs Frames

If you’re running a GUI version of emacs under X-Windows, MSWindows, or OSX, you also have the option of opening a new frame (which you’ll remember, is what’s called a window to anyone outside the emacs community!). The commands for working with frames are very similar to the command for working with windows. To create a new frame, use the command C-x 5 2. To delete the current frame, use C-x 5 0 (that’s zero, not O). To delete all but the current frame, use C-x 5 1.

These are not all of the emacs commands for working with buffers, windows, and frames., but these are the most common and basic ones. We’ll certainly learn about more of the others as we proceed in our shared journey to emacs mastery.

Speaking of our journey, lets review what we’ve learned so far in this series:

Working with files:

Command What it does
C-x C-f Open (Find) a file
C-x C-s Save the current file
C-x C-w Save As
C-x s Save any unsaved files

Cursor movement:

Command What it does
C-f Move the cursor forward 1 character
C-b Move the cursor back 1 character
C-n Move the cursor to the next line (down)
C-p Move the cursor to the previous line (up)
M-f Move the cursor forward 1 word
M-b Move the cursor backward 1 word
C-a Move the cursor to the beginning of the line
C-e Move the cursor to the end of the line
M-a Move the cursor to the beginning of the paragraph
M-e Move the cursor to the end of the paragraph
C-v Move the cursor one page down
M-v Move the cursor one page up
M-< Move the cursor to the beginning of the file
M-> Move the cursor to the end of the file
C-l Recenter the screen around the current line

Working with buffers:

Command What it does
C-x C-b List buffers
C-x b Switch to named buffer (with tab completion)
C-x k Close (kill) the current buffer

Working with windows:

Command What it does
C-x 0 Close current window
C-x 1 Close all windows except the current one
C-x 2 Split current window in two vertically
C-x 3 Split current window in two horizontally
C-x o Switch to other window

Working with frames:

Command What it does
C-x 5 0 Close current frame
C-x 5 1 Close all frames except the current one
C-x 5 2 Create a new frame

Misc:

Command What it does
C-x C-c Exit Emacs

That’s all for this installment. Join us next time when we’ll explore marking text, working with marked regions, searching, and how to get out of trouble if you accidentally hit the wrong key sequence!

______
Other installment in this series: part one, part two, part three, and part four.

h1

C# from a Java Developer’s Perspective v2.0

May 3, 2007

 Here’s a great resource for anyone who ever has to switch back and forth between Java and C#.  Dare Obasanjo has written a comprehensive comparison/rosetta stone for the two languages called C# from a Java Developer’s Perspective v2.0. Even if you’re firmly in one language camp or the other, it’s interesting to see how certain things are done by the “opposition”.  Definitely worth bookmarking.

h1

What’s so cool about Atom?

May 1, 2007

 I ran across a good article over a Charlie Savage’s blog called “Atom Will Change the World“. It’s a good overview of the Atom protocol, and what makes it cool for things beyond just blog feeds.