Debian Codegraph @

www/src/content/essays/locality-of-behaviour.md

raw symbols SCIP
1 ---
2 title: "Locality of Behaviour (LoB)"
3 description: "Carson Gross explores the Locality of Behaviour (LoB) principle, which emphasizes making the behavior of code units obvious on inspection to enhance maintainability. He discusses the tradeoffs between LoB and other software 01-frontend principles like DRY and SoC, offering insights on balancing clarity, abstraction, and maintainability in modern development."
4 created: 2020-05-29
5 modified: 2023-01-20
6 authors: [ "Carson Gross" ]
7 tags: ["simplicity"]
8 ---
9
10 > "The primary feature for easy maintenance is locality: Locality is that characteristic of source code that enables a
11 > programmer to understand that source by looking at only a small portion of
12 > it." -- [Richard Gabriel](https://www.dreamsongs.com/Files/PatternsOfSoftware.pdf)
13
14 ## The LoB Principle
15
16 Locality of Behaviour is the principle that:
17
18 > The behaviour of a unit of code should be as obvious as possible by looking only at that unit of code
19
20 ## Discussion
21
22 The LoB principle is a simple prescriptive formulation of the quoted statement
23 from [Richard Gabriel](https://www.dreamsongs.com).
24 In as much as it is possible, and in balance with other concerns, developers should strive to make the behaviour of
25 a code element obvious on inspection.
26
27 Consider two different implementations of an AJAX request in HTML, the first in [htmx](/):
28
29 ```html
30 <button hx-get="/clicked">Click Me</button>
31 ```
32
33 and the second in [jQuery](https://jquery.com/):
34
35 ```javascript
36 $("#d1").on("click", function(){
37 $.ajax({
38 /* AJAX options... */
39 });
40 });
41 ```
42
43 ```html
44 <button id="d1">Click Me</button>
45 ```
46
47 In the former, the behaviour of the `button` element is obvious on inspection, satisfying the LoB principle.
48
49 In the latter, the behaviour of the `button` element is spread out amongst multiple files. It is difficult to know
50 exactly what the button does without a total knowledge of the code base. This "spooky action at a distance" is a source
51 of maintenance issues and stands in the way of developers understanding of the code base.
52
53 The htmx example demonstrates good Locality of Behaviour, while the jQuery example has poor Locality of Behaviour.
54
55 ### Surfacing Behaviour vs. Inlining Implementation
56
57 A common objection to Locality of Behaviour is that it is inlining implementation details within a code unit, making the
58 code unit less abstract and more brittle. However, it is important to make the distinction between inlining the
59 *implementation* of some behaviour and inlining the invocation (or declaration) of some behaviour.
60
61 Consider functions in most programming languages: there is a distinction between the declaration of function and its
62 use at call sites. A good function abstracts away its implementation details, but is also invoked in an obvious manner,
63 without any spooky action at a distance.
64
65 Increasing the obviousness of the behaviour of an element is, ceteris paribus, a good thing, but it falls to both
66 end-developers
67 and especially framework developers to make LoB both as easy and as conceptually clean as possible.
68
69 ### Conflict With Other Development Principles
70
71 The LoB will often conflict with other software development principles. Two important ones
72 are:
73
74 * [DRY - Don't Repeat Yourself](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)
75
76 Software developers typically strive to avoid redundancy in their code or data. This has come to be called "Staying
77 DRY",
78 i.e. Don't Repeat Yourself. Like other software design principles this, on its own, is a good thing. htmx, for
79 example,
80 allows you to place many attributes on parent elements in a DOM and avoid repeating these attributes on children. This
81 is a
82 violation of LoB, in favor of DRY, and such tradeoffs need to be made judiciously by developers.
83
84 Note that the further behaviour gets from the code unit it effects, the more severe the violation of LoB. If it is
85 within a few lines of the code unit, this is less serious than if it is a page away, which is less serious than if
86 it is in a separate file entirely.
87
88 There is no hard and fast rule, but rather subjective tradeoffs that must be made as software developers.
89
90 * [SoC - Separation Of Concerns](https://en.wikipedia.org/wiki/Separation_of_concerns)
91
92 Separation of concerns a design principle for separating a computer program into distinct sections such that each
93 section addresses a separate concern. A canonical example of this is splitting HTML, CSS, and Javascript. Again, on
94 its own and
95 in isolation this may, indeed, be a good thing. Inlining
96 styles [has become more prevalent lately](https://tailwindcss.com/),
97 but there are still strong arguments in favor of SoC in this regard.
98
99 Note that SoC is, however, in conflict with LoB. By tweaking a CSS file the look and, to an extent, behaviour of an
100 element can change dramatically, and it is not obvious where this dramatic change came from. Tools can help to an
101 extent
102 here, but there is still "spooky action at a distance" going on.
103
104 Again, this isn't to condemn SoC wholesale, just to say that there are subjective tradeoffs that must be made when
105 considering how to structure your code. The fact that inline styles have become more prevalent as of late is an
106 indication that SoC is losing some support amongst developers.
107
108 ## Conclusion
109
110 LoB is a subjective software design principle that can help make a code base more humane and maintainable. It must be
111 traded
112 off against other design principles and be considered in terms of the limitations of the system a code unit is
113 written in, but, as much as is it is practical, adherence to this principle will increase your software maintainability,
114 quality and sustainability.