For centuries, Buddhists have practiced a meditation technique called
Visualization. The practice involves reconstructing every minute detail of
a complex Thangka painting (e.g. www.tibetshop.com/gs363.html)
from memory in one’s mind. The ultimate aim is to be able to hold an
entire complex image in your mind at once, as if actually looking at the
painting.

As with many (if not all) of the Buddhist meditative practices, the goal of
this meditation is to practice focusing and controlling the mind.
Scientific studies have shown that Buddhist meditative practices such as
visualization have a signficant physical effect on where and how activity
occurs in the brain. Additionally, it has been proven that intense practice
in various disciplines signficantly increases the physical development of
the brain in areas associated with positive thought.(1) Mental practice and
development is analogous to physical exercise, with your mental
"muscles" getting materially stronger with practice.

What to do

(If you are new to the term "Code Kata", visit Dave’s site:
www.pragprog.com/pragdave/Practices/Kata.
This Kata will require more than one sitting. Start small and increase in
complexity over time. Continue with a single piece of code until
you’ve mastered it or until you see no value in continuing.)

Pick a piece of code that you consider to be of extremely high quality.
Ideally, this should be sized somewhere between the huge (the Linux kernel
source) and the tiny ("Cookbook" snippets). For maximum
utilitarian benefit, find a piece of software that you would like to
enhance your understanding of. Open source programmers might choose, for
example, the standard library of their programming language of choice.

Find a quiet place where you won’t be disturbed. Start with a
moderately small piece of your chosen code base and study it intensely for
20 to 30 minutes. You can start out with as small a chunk as you like.
After your period of study, sit back, close your eyes (if you like), relax,
and try to reconstruct the code in your mind. You might start with classes
(in an object oriented program), fill the classes out with methods and
instance variables, and eventually fill out the method bodies. Follow the
logical flow from one method to the next. Enumerate the classes, variables,
and methods in your mind.

Alternatively, you could spend sessions trying to reconstruct a graphical
representation of the code—perhaps UML. Class diagrams and sequence
diagrams would probably be a good start, though converting source to UML in
your head might prove to be harder than remembering the code itself. A mix
of code and visual representation might be a good place to end up. Feel
free to experiment and season to taste.

Set a time limit for the exercise. 10-15 minutes would probably be a good
start. Adjust as your experience dictates. Try not to let your mind wander
during the time that you have allotted. As other thoughts enter your mind
(e.g. "Why am I doing this? This is silly. What should I have for
dinner?") push them gently away and focus on the code. (As the
Buddhist medidators would probably add, we all get distracted. Don’t
be distracted by the fact that you’re getting distracted. Don’t
punish yourself. Just go back to the code.)

Slowly add more and more of the full code base as you practice in
subsequent days and weeks. Add code one "module" at a time, until
you eventually (if ever) are able to hold your entire chosen code base in
your mind.

Goals

There are two primary goals to this exercise, one with immediate visible
benefit—the other with a potentially longer term effect:

Immediate/Visible

Great conductors commit entire symphonies to memory. That might be 30
minutes worth of black dots written for potentially 100+ simultaneous
(instrumental) voices. I’ve heard some say that it greatly enhances
their ability to hear the piece. If these pieces happen to be the
work of a "master", what better way is there to stamp a piece of
that master’s work on your mind?

In music, this has the very pragmatic side effect that a conductor can
quickly and efficiently work with an orchestra that is playing a piece that
the conductor has committed to memory. The same is true in software. No
need to look up that function from the API documentation. And you’re
more likely to find the "optimal" solution to a problem, if you
are intimately familiar with the tools available to do the job. You can
drive a screw into a board with a hammer, but if only you knew about that
screw driver, life would be much easier.

Longer Term/Esoteric

Unless you have photographic memory, reconstructing large pieces of code in
your mind is an extremely difficult thing to do. If you’re like me,
you have trouble committing even small pieces to memory. This is like
weight lifting for the mind. You will develop new "muscles" that
you never knew you had. Your mind will find new ways to associate,
generalize, and ultimately simplify the complexities of the software for
you. Be warned that you might not see it or feel it after one
attempt—or even 10. Another analogy from music: Do you think jazz
improvisers actually think of the name and finger position of every note
they play before they play it? The answer is "no". Their minds
can’t work that fast. The details get grouped into gestures and
generalizations—most of them completely subconcious.

And, of course, if you believe the scientists and Buddhist scholars
involved with the Mind and Life Institue (www.mindandlife.org), you may
potentially develop your mental abilities in a way that would contribute to
any activity requiring concentration, including the quest for happiness. :)

Questions

  • Are there pieces of code or styles of design that are easier to visualize
    than others?
  • What seems to drive your ability to hold certain pieces of code in your
    mind over others?
  • Do you think these drivers are the same for everyone, or do they vary by
    individual? Might this realization affect the way you write your own code?
  • What mental tricks might you subconciously be developing to manage this
    complexity?

(1) "Destructive Emotions: A Scientific Dialogue with the Dalai
Lama" – Daniel Goleman (www.amazon.com/exec/obidos/tg/detail/-/0553801716/qid=1054992768/sr=8-1/ref=sr_8_1/104-2641040-4275932?v=glance&s=books&n=507846)

Sidenote

This isn’t really a "Kata" (www.pragprog.com/pragdave/Practices/CodeKata.rdoc,v).
It doesn’t really fall into the Kumite or Koan categories (www.pragprog.com/pragdave/Practices/MoreKata.rdoc,v)
either. But, it’s definitely an example of Practice (www.chadfowler.com/index.cgi/Computing/Programming/Bowling.html),
though a somewhat strange one. And in case you’re wondering,
I’m not Buddhist.