james mckay dot net

Because there are few things that are less logical than business logic

December 2010

06
Dec

The benefits of the command line

The question of the command line is a controversial one, with some people declaring that it should be a core competency without which you should be considered an automatic “no hire,” and others claiming that it’s an anachronistic irrelevance in these days of graphical user interfaces, drag and drop, and integrated everything in Eclipse and Visual Studio.

Now I’ve repeatedly described command line instructions as a bad marketing strategy, and I stand by that. The command line is pretty intimidating if you aren’t already familiar with it: it has a much steeper learning curve than GUI tools, it lacks discoverability, and it only gives you a narrow view of what you’re doing that can seem like peering down the wrong end of a telescope at first. Consequently, there are also some tasks for which it is simply not suited. Furthermore, its support on Windows is abysmal.

But these sentiments of mine are only about marketing, and do not extend to day-to-day use. Over the years, I’ve actually come to appreciate the benefits of the command line. This is probably because I did a lot of work with Linux at my last job, so I had to get to grips with it. (One will recall that I was thrown in right at the deep end by being told to install Gentoo on a spare server.) And in fact, once you’re familiar with it, it offers some significant advantages for many tasks. These are they.

1. Ergonomics and speed.

The command line is keyboard-centric. GUI tools, on the other hand, are mouse-centric.

Mouse-centric environments have a nasty habit of forcing you to reach to and fro between your keyboard and mouse the whole time, which not only slows you down considerably, but can also cause repetitive strain injury. The fact that your keyboard puts a numeric keypad in the way doesn’t help much, and with some otherwise ergonomic keyboards such as the Microsoft Natural 4000, the stretch can be horrendous. That’s why many power users prefer command line instructions and keyboard-focused text editors such as emacs or vim: your hands stay in one place and let you work a lot more quickly and fluently.

Distraction is another factor. When you have to move your hand between the keyboard and the mouse all the time, it can be tempting to focus on mouse-centric tasks such as sorting out your e-mail inbox and surfing the web. On the other hand, when you’re spending most of your time on the keyboard, it’s much easier to stay focused on what you have to do without getting distracted.

2. Scriptability.

Most command-line haters think nothing of writing a long, complex howto document outlining the instructions needed to carry out a particular repetitive task, such as setting up a new project in source control, or building your project, or adding a new component from a template. The only problem with this is that when someone new comes onto the team, they have to follow these instructions through step by step, and the more steps there are to follow through, the more likely they are to misunderstand them, get one of them wrong or out of order, and end up deleting all their files by mistake.

You could write a PowerShell script to automate it — provided, of course, that the tool concerned actually exposes enough functionality through PowerShell or the command line to let you do so — but they generally don’t because that would require going to the trouble of learning a whole new language for something that you don’t do very often. But on the other hand, if you’re used to using the command line (or PowerShell) all the time in the first place, you already have a suitable language at your fingertips, and you don’t need to learn anything new. Scripting your repetitive tasks becomes the easy, logical and natural thing to do.

3. Cut and paste.

Command line instructions are much easier than screenshots to copy and paste into an e-mail, or a howto guide, or a blog post, or an instant messaging conversation. While this is inappropriate for introductory tutorials, it can save a lot of time when you need to Get Things Done.

For the past few days I’ve had to work from home because of the snow. At one point, I had to give a colleague some instructions on how to do one or two simple things with Mercurial. If we had only had TortoiseHg, I would have had to set up a remote assistance session with him, which would have been overkill for what he needed to do. However, with the command line, I was able to simply type the necessary command line instructions straight into Microsoft Office Communicator, and it was all very simple, quick and painless.

In conclusion.

The command line is not a golden hammer. There are some tasks for which it is not well suited, or which it makes considerably more difficult. But for a lot of tasks, it can provide significant benefits. Agility with the command line can still be a useful skill in a developer’s toolkit, and it is quite wrong to write it off completely.