Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Code Improvisation

The concept of code improvisation is loosely based on music improvisation. In the most common form of musical improvisation, the performers agree a priori on a set of chord progressions and then take turns soloing over these progressions, which are also known as chord changes, or simply changes. Now to a non-musician who isn't accustomed to hearing the chord progressions, the resulting music sounds entirely impromptu. But in fact the chord progressions are an important, albeit implicit, structure organizing the performance. The chord progressions constrain what notes sound good and when certain notes should be played, to name just some of the ways in which musicians use the progressions to organize their performance.

Similarly, when a programmer sits down and writes a computer program without the aid of some design document (flowchart, data-flow diagram, object diagram, and so on), it may appear that he or she is extemporaneously creating the code. But just like the chord progressions in music improvisation, a hidden structure organizes the programmer's coding—a set of information progressions. To understand this, think of a computer program as a process that takes an input and transforms it into an output, which we can depict as follows:

input-->output

In this diagram, the arrow represents the computer program, and input and output represent the initial and final information states, respectively. However, this input-output transformation typically doesn't happen in one step. The programmer breaks this transformation into one or more intermediate transformations:

input-->s1-->s2-->...-->sn-1-->output

Each intermediate transformation results in some new information state si. When a programmer sits down and improvises a program, he or she always has some information-state progression in mind. What varies from programmer to programmer is the extent of his or her information-state progressions. An inexperienced programmer writing a program for the first time may just know the input (s0) and output (sn) states; determining the intermediate states (s1...sn-1) in a piecemeal fashion as he or she codes. A more experienced programmer writing the same program may know all the intermediate information-states before he or she even sits down to code.

With the information-state progression known, the programmer then codes the progression from one state to the next. For experienced programmers, the coding doesn't happen piecemeal. Experienced programmers have a collection of useful coding "chunks" stored away in their brains that they can piece together to implement the progressions. For example, if you tell an experienced programmer to implement a routine that "sums all the elements in an array," the programmer can usually just whip out the code with very little thought. The more experienced the programmer, the more and the larger the coding chunks they have in their brains.

We should point out that a coding chunk has an analogy in music improvisation. Experienced musicians have acquired a rather large mental library of motifs (guitarists call them licks)—sequences of notes that they like playing—and they string these motifs together to create their solos. We'll adopt the term motif to describe coding chunks as well.

In short, code improvisation is a process where a programmer does the following:

  1. Map out the information-state progressions.

  2. Implement each information-state progression either piecemeal or by combining motifs.

Your goal when you do code improvisation is to have fun and learn a lot of useful motifs that you can apply to other programming tasks. Once again, you keep design to a minimum—map out the information-state progressions—and spend most of your time coding (the fun part).

  • + Share This
  • 🔖 Save To Your Account