Foo's introduction to the coding trance

(All written from my point of view and personal experience, don't take it as gospel, give it a go!)

The jargon file defines Hack mode as

a Zen-like state of total focus on The Problem that may be achieved when one is hacking

Suggesting the technique is quite well known amongst various communities of programmers the world over. The "zen-like" part of this description is worth considering, since the coding trance is unlike many other trance states in that it represents absorption into a single consuming line of thought and the extinction of all others.

The C2 Wiki describes a very interesting similar state in A mental state called flow

I'll be referring to it as "hack mode", "the coding trance" and also "fascination" throughout.

Why bother?

The coding trance is a state of fascination where a single point of focus (your interaction with your program) grows to take over your entire concious mind, or another way to see it is your concious mind shrinks what its paying attention too to the art of programming.

It takes a while to get into this state, and a while to properly awaken from it (and be of much use for other jobs) and so its only really suitable for large programs. However I generally find that it takes a large program (and a correctly sized block of time) to be fascinating enough to get into it anyway so that's not really a problem.

Its worth knowing these kinds of techniques on the basis of the fact that productivity goes right up during bouts of programming in this state. This is partially due to concentration on the subject matter, but its also connected with the fact a fascinated programmer can get more of the program domain into his head (with less abstraction required to understand it) than one who is just hacking away.

This "fitting it into your brain" means you tend to spontaneously generate insightful methods of solving problems. For me this tends to be a visual understanding of how the code fits together, where I can visualise the flow of control as it moves along the lines, and see the functions stacking.

In my case I always see them stacking downwards inside each other in an infinitely deep black tunnel, or more correctly I see the inmost function as the bottom of the tunnel and the functions that encompass it encircle it as my point-of-view pulls back, so for example if we consider the following lines of fictional Lisp.

(cdr (list (generate (data-suck bar))))

I tend to see (data-suck bar) at the bottom, then pull back focus to see its wrapped in the function call (generate ...), pulling back again sees that that's wrapped in (list ...) which in turn is wrapped in (cdr ...) and hence I know what the line will do.

Environment

The environment you find yourself in I find is very important to entering and maintaining a proper bout of hack mode. I've broken up the methods I use into the following areas. You may find these helpful, or hindering, either way try various things.

Music

Music is for me one of the most important things for helping enter and maintain fascination with code. I find there are several characteristics of good music for coding too.

It must be well known to you

Don't use music you aren't familiar with or your brain will be paying attention to the novelty of that instead of riding along with it. This is the most important factor in my opinion.

It should have minimal vocals

Since coding is a lot about words and phrases I find that overly vocal music can be distracting. However this plays into the above point, music your used to your brain will simply tune out the words.

It should have a good beat

The rhythm of an album is useful for determining the kind of coding you do, fast music tends to produce rapid work but it may have more bugs in, slower music produces more concentrated, elegant code in some ways. However above a certain speed music I find tends to become a blur, so especially fast is as good as slow.

There should be minimal style changes

Its important I find to have a minimal number of changes of style, so don't try and use something you've only got a few tracks off, its probably not enough. Instead get something you've a number of albums of, or something you can loop.

With that in mind I recommend good loud rock/metal of a certain style, for example: A Perfect Circle is good due to the amount of rhythmic, loud drumming, however say Muse is less good for me since its more vocal and guitar lead. I find Rammstein good because the vocals are meaningless to me, yet its loud and drum heavy.

However musicals are good (Rocky Horror, Phantom) since they give good continuity and you can be swept along with them provided you know them well enough that the vocals don't affect you. However be aware that they can be bad for maintaining a trance when they end, since it can be an abrupt style change from something like that to another style of music.

A good selection of 90s dance music can be helpful, since this is the point where it had a large number of drums but before it become overly fast. Also electric stuff like Proppellerheads and The Prodigy is also excellent.

Finally Japanese Taiko drumming really works for me, since its got a good flow to it. You may find some of the smaller drums too high-pitched however.

If your in a shared office then headphones are most useful, since they also help cut out all kinds of background noise.

Food & Drink

Theres not much to say on this topic. If you don't eat enough your blood sugar drops and you can't concentrate, so you fall out of trance due to distraction.

If you eat too much stimulating food (caffeine, chocolate etc) then you tend to get a sugar buzz, which is not so good for focus. However you should consume a reasonable amount of stimulants if your used to them, depriving your body of them will lead to poor concentration.

Finally if you eat too much good solid food you'll also knock yourself out as your body will slow up to try and digest it all.

Take regular breaks to eat, eat lightly but enough to keep you from snacking too much. Keep liquids near you but possibly mainly water instead of coke.

Clothing

Clothing should be comfortable, I find loose clothing particularly good, but then I've always liked it. It needs to be warm enough to keep you warm, but not so hot as to overheat you.

I've found on many occasions that removing shoes and socks helps, theres something about airflow to the feet that can help in certain situations.

Posture

Posture should be sensible. You must ensure that your chair, keyboard, desk and monitor are all at complimentary heights. This will help reduce strains to various parts of your body and reduce the risks of RSI. See a qualified professional.

Personally I find I try to keep a reasonably straight back, feet on the floor, chin tucked in a little, however I tend to slip into the classic "leaning forwards, bent back, head back to look at the monitor" pose, which is bad so if I'm aware of it I do my best to correct it.

If you wake up a bit you may find that your hands and feet have gotten cold due to your body being largely immobile, regular stretching (without breaking focus) is important to keeping the flow of blood going.

It should be pointed out that regular exercise is useful for this, having a body that does things on a regular basis will keep the blood going and your brain working better than one that's sedateary

Visual Focus

Visual focus for me is something that tends to happen more as the fascination comes on. Ideally I find you need a screen big enough for a large amount of code to be visible at once, also having a minimalistic window manager and a minimal amount of overlapping windows is important.

Focusing on the flow of code is important, try to avoid having anything that could distract the eyes. This means no flashing phones, no blinking "You have mail!" buttons, and no flashing IM windows.

Check mail and IMs periodically during breaks but don't have any notifications going whilst your trying to work.

Breathing

Steady and regular breathing is useful, I find often it goes with the tempo of the music.

If you've got good posture (i.e. a straightish back) then you should find belly breathing helpful, proper breathing can help bring in oxygen to your system and keep you going.

Consult a doctor before trying to breath and all that.

Programming environment

It doesn't really matter what your programming environment is, as long as its not causing you to think to actually produce action.

You should try for the following things.

A good editor that your familiar with

I normally us vim but recently for my Lisp coding I've been using Emacs with Slime. It took a while to get up to speed with emacs, and I'm not there totally yet, for example I keep hitting ESC-v to try and do visual selection (as is done in Vim) which moves back a screen of text in Emacs.

This is the kind of behaviour you want to avoid, since unexpected behaviour from your tools threatens your concentration.

A powerful editor is useful for programming since you tend to be working on fairly regular text and hence regex based searching or transformations of text become insanely useful.

A good programming language

It doesn't really matter what it is (provided its not badly broken) but it should be something your familiar enough to code mostly without looking at reference material. I used to be proficient enough with C to use that, these days I'm probably proficient enough with Perl or Lisp to get away with those

Good reference material

If you have a problem with finding a library function for a specific task, or remembering the correct arguments for something you need to be able to look up the information fast and without changing your mode of thought too much, otherwise you'll wake yourself up.

The best reference material is that which your editor provides. Slime or Vim 7's omnicompletion are good examples of what can be done with this.

The second best reference material is a good book or website of the standard. This means K&R (C), The Camel (Perl) or The CLHS (Lisp).

A windowing system that doesn't suck

One that fulfils the requirements of the Visual Focus discussion above. I find that minimal window overlaps, large amounts of screen real-estate and the ability to turn off notifications are useful.

Tiling window managers like Ion3 or wmii can be useful provided you can use them naturally (i.e. without distractingly thinking about their commands).

Entering Trance

This section is likely to be the most sketchy.

I find that with proper preparation then fascination can start to occur naturally if your working for long enough. A good idea is to have a window of at least 2 hours of uninterrupted time to work in.

One quote I find that tends to describe it is this exchange:

Ananda saw Sariputra coming afar off, and he said to him: 'Serene and pure and radiant is your face, Brother Sariputra! In what mood has Sariputra been today?'

'I have been alone in Dhyana, and to me came never the thought: I am attaining it! I have got it! I have emerged from it!

If you have too much anticipation in your system you'll be watching your own mind, looking to see when it starts to lose interest in the outside world, however this is a self defeating strategy.

I find sometimes that nodding/swaying, foot-tapping or finger-drumming along to music can help.

Instead focus on your code, provided you've followed the advice in the previous section then you should naturally drop into fascination in between 30 minutes and an hour. As Sariputra above, you will probably not notice until someone prods you to tell you its time to go home, or your body tells you its time for lunch.

Maintaining Trance

Maintaining concentration is easy once its started, simply carry on coding on a large enough problem and your brain should keep itself going in hack mode.

However, should you be interrupted (lets say by an IM window, or by a sudden thought you should check your mail, or the sun glinting through the window, or someone in your office poking you for information, or a need for lunch or xwrits telling you to take a break) then don't panic!

If you pay too much attention to the distraction you'll wake yourself up totally, instead just stay relaxed. Pause your music, take off your headphones. This means you can unpause your music and your brain will try to drop back into things again. Before you do anything else its important that you dump state in a calm way. Quickly type a bunch of comments into some code and some FIXME nodes, scribble things onto scrap (including pictures) and generally get everything important off the top of your brain (the programs structure will at this point still be in your mind so as long as you don't leave it too long it'll be there when you get back).

If its just a short break then simply keep the headphones on, stretch a bit, stand up, stretch your legs, hands, arms, shoulders and neck.

Look out the window a bit, blink your eyes a few times, but don't open them all the way, let them just rest naturally. Focus your eyes on objects at a few distances, but don't concentrate on them, just let them sit in your view. Visualise your current program and mentally flow through the code, you might well spot bugs here. Once the breaks over then setting back down, make sure your comfy, let your eyes rest on the editor again, breath, let the music flow and begin work once more.

If your being asked to solve a technical problem then its not much of an issue provided its something your at least passingly familiar with. Switch to another virtual desktop (to avoid messing with your windows), solve the problem (you may find its absorbing enough to keep your fascination going) then in a measured manner go back to what you were doing. Put the headphones back on, start the music up, breath easy and go over your notes. You should be able to get back up to full speed again fairly soon (which is why interrupting programmers is wasteful and cruel, it disturbs the flow of work for the time it takes to answer the question/solve the problem, and then another 15-30 minutes afterwards).

If you have to go somewhere else physically but want to get back to coding shortly afterwards then things are a bit more complex. Firstly dump-state as before, commit the changes in your files to your versioning system and then your set.

Firstly make sure you have a music player with a similar playlist to the one your using to hack too, put that on as soon as you leave your desk, keep a pen handy as over the next 10-30 minutes or so you'll remember things to note down and you'll need to scribble them.

Walk at a natural pace to wherever your going, its important to get a rhythm of movement going, keep the eyes focused on things that let your brain keep spinning, repeating pavement tiles are excellent for this provided they form a good pattern, I've also found overhead roofing on walkways is good as long as you can walk, lookup and not bump into things.

If you have to talk to people then keep your contribution to a minimum, you may find yourself slow to respond, but if you want to maintain trance don't let it worry you, apologise for being elsewhere and move on. You can with practice wake up a bit, just make sure not to wake up overly or let yourself start to worry about mundane things like the bill and washing as that'll destroy your concentration.

With practice I've found I can go to lunch and provided I'm not overly energetic I can head back to my office and drop back into fascination reasonably quickly.

Leaving Trance

This is again a bit of a vague subject, but is useful at the end of the day or if your finished with coding for the foreseeable future.

Firstly your brain is currently fascinated with a certain mental construction, hence its going to be a bit slow coming back to the Real World(tm).

So when you want to stop coding try the following:

  1. Firstly dump state, this means make comments and notes, draw diagrams, mark places in books and websites
  2. Commit the code
  3. Turn off the music
  4. Close your editing environment
  5. Stretch a bit, blink your eyes, roll them around in their sockets
  6. Stand up, clean your desk, collect your things, enjoy the view, chat with co-workers (since it'll be mostly work related it should be easy conversation).
  7. If your listening to music when walking/driving to wherever your headed then try and pick stuff you don't routinely listen to when coding, make it more upbeat if possible.
  8. Have a pen with you! You will think of things you should have noted down, scribble on notepads as soon as you think about it, remember that diagrams are useful

Let it take its time, I find it can take anywhere up to an hour to be fully awake again.