The Proximity Rule?

Main Thread 2 min read

I noticed a recurring style in my code lately. Actually, my pair noticed and asked me about it. I call it the Proximity Rule. I thought about calling it the Proximity Principle, but the initialism made my inner-child chuckle.

I feel it's easier to explain with code samples. Consider the following function which filters items in an array using a callback.

1function array_filter(array, callback) {
2 var i;
3 var length = array.length;
4 var filtered = [];
5 
6 for (i = 0; i < length; ++i) {
7 if (callback(array[i])) {
8 filtered.push(array[i]);
9 }
10 }
11 
12 return filtered;
13}

This code has a simple, traditional style. It groups similar statements together into blocks of code separated by whitespace. Each group tells a story - initialize, execute, respond.

However, this story is a bit robotic. Fine for the computer, but humans need to read this story too. Let's look at the same code after applying the Proximity Rule.

1function array_filter(array, callback) {
2 var i;
3 var filtered = [];
4 
5 var length = array.length;
6 for (i = 0; i < length; ++i) {
7 if (callback(array[i])) {
8 filtered.push(array[i]);
9 }
10 }
11 
12 return filtered;
13}

By moving the length assignment closer to the for loop I emphasize their relationship. So the Proximity Rule is not just about grouping similar statements of code. Its also about grouping related code.

Let's look at another example. Consider the following tests for our array_filter function.

1describe("array_filter", function() {
2 var actual;
3 var odd_callback;
4 var even_callback;
5 
6 beforeEach(function() {
7 odd_callback = jasmine.createSpy('odd');
8 odd_callback.and.returnValues(true, false, true, false, true);
9 
10 even_callback = jasmine.createSpy('even');
11 even_callback.and.returnValues(false, true, false, true, false);
12 });
13 
14 describe("when filtering odd numbers", function() {
15 beforeEach(function() {
16 actual = array_filter([1, 2, 3, 4, 5], odd_callback);
17 });
18 
19 it("should return only odd numbers", function() {
20 expect(actual).toEqual([1, 3, 5]);
21 });
22 });
23 
24 describe("when filtering even numbers", function() {
25 beforeEach(function() {
26 actual = array_filter([1, 2, 3, 4, 5], even_callback);
27 });
28 
29 it("should return only even numbers", function() {
30 expect(actual).toEqual([2, 4]);
31 });
32 });
33});

We again see code grouped by statement. If we focus on the context when filtering even numbers, we might ask ourselves, "What is even_callback?"

If we apply the Proximity Rule, we can improve the readability and eliminate this question.

1describe("array_filter", function() {
2 var actual;
3 
4 describe("when filtering odd numbers", function() {
5 beforeEach(function() {
6 var callback = jasmine.createSpy('odd');
7 callback.and.returnValues(true, false, true, false, true);
8 actual = array_filter([1, 2, 3, 4, 5], callback);
9 });
10 
11 it("should return only odd numbers", function() {
12 expect(actual).toEqual([1, 3, 5]);
13 });
14 });
15 
16 describe("when filtering even numbers", function() {
17 beforeEach(function() {
18 var callback = jasmine.createSpy('even');
19 callback.and.returnValues(false, true, false, true, false);
20 actual = array_filter([1, 2, 3, 4, 5], callback);
21 });
22 
23 it("should return only even numbers", function() {
24 expect(actual).toEqual([2, 4]);
25 });
26 });
27});

This example also demonstrates how the Proximity Rule can help condense code. Especially when Code Smells, such as Lazy Class, are in the air.

For me, the Proximity Rule is simply a coding style. One I doubt is new. I am sure Knuth or Beck or one of the other Programming Godfathers have written about this in some capacity. If so, please let me know. One of my recent goals is to call things by their proper name. To be fair, I did attempt to ask on Twitter.

Find this interesting? Let's continue the conversation on Twitter.