Home > Articles > Software Development & Management

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Reversing Message

Symmetry can improve the readability of code. Consider the following code:

void compute() {
  input();
  helper.process(this);
  output();
}

While this method is composed of three others, it lacks symmetry. The readability of the method is improved by introducing a helper method that reveals the latent symmetry. Now when reading compute(), I don’t have to keep track of who is sent the messages—they all go to this.

void process(Helper helper) {
  helper.process(this);
}
void compute() {
  input();
  process(helper);
  output();
}

Now the reader can understand how the compute() method is structured by reading a single class.

Sometimes the helper method invoked by a reversing message becomes important on its own. Sometimes, overuse of reversing messages can obscure the need to move functionality. If we had the following code:

void input(Helper helper) {
  helper.input(this);
}
void output(Helper helper) {
  helper.output(this);
}

it would probably be better structured by moving the whole compute() method to the Helper class:

compute() {
  new Helper(this).compute();
}
Helper.compute() {
  input();
  process();
  output();
}

Sometimes I feel silly introducing methods “just” to satisfy an “aesthetic” urge like symmetry. Aesthetics go deeper than that. Aesthetics engage more of your brain than strictly linear logical thought. Once you have cultivated your sense of the aesthetics of code, the aesthetic impressions you receive of your code is valuable feedback about the quality of the code. Those feelings that bubble up from below the surface of symbolic thought can be as valuable as your explicitly named and justified patterns.

  • + Share This
  • 🔖 Save To Your Account