Skip to content

Commit 813beab

Browse files
committed
Blog edits
1 parent 8adacec commit 813beab

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

_posts/2020-09-09-self-documenting-code-comments.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ contained some good info, as well as some things I don't agree with. Top of the
1212

1313
> Self-documenting code is bullshit.
1414
15+
I mean... it's not, is it?
16+
1517
## Self-Documenting Code
1618

1719
Self-documenting code is code written in such a way that it's easy to read and understand its intention.
@@ -21,7 +23,7 @@ but it removes the need for _a lot_ of documentation and code comments.
2123

2224
## For Example
2325

24-
The blog gives the following example of documenting code with comments:
26+
The blog gives the following example of code documented with comments:
2527

2628
```js
2729
/**
@@ -59,8 +61,8 @@ var toggleVisibility = function (selector, toggle) {
5961
};
6062
```
6163

62-
This is pretty neatly-written code - let's take it line-by-line, consider which comments add value, and
63-
if we can make the code more self-documenting.
64+
The code itself is pretty neatly-written - let's take it line-by-line, consider which comments add
65+
value, and if we can make it more self-documenting.
6466

6567
### JSDoc Documentation
6668

@@ -77,7 +79,7 @@ var toggleVisibility = function (selector, toggle) {
7779
7880
According to the documentation, this function toggles the visibility of a tab - but its name and the
7981
names of its arguments make no mention of tabs. Maybe the file containing this function is tab-specific,
80-
so within [its context](/naming-things-is-hard-namespace-interface-class-method-context) this makes
82+
so [within its context](/naming-things-is-hard-namespace-interface-class-method-context) this makes
8183
sense, but maybe not. In any case, this can be more self-documenting:
8284
8385
```js
@@ -117,8 +119,8 @@ it can be improved. `elem` is a completely generic variable name - it gives us a
117119
variable is, but tells us nothing about its purpose.
118120
119121
Secondly, is the comment lying? It says we're getting the tab to 'show', but doesn't this function
120-
_toggle_ visibility? The JSDoc said 'Toggle visibility of a content tab' - I'd expect from that that it
121-
would show hidden tabs, and hide visible ones. Sure enough though, it only _shows_ tabs, it doesn't
122+
_toggle_ visibility? The JSDoc said 'Toggle visibility of a content tab' - I'd expect from that it
123+
would _show_ hidden tabs, and _hide_ visible ones. Sure enough though, it only _shows_ tabs, it doesn't
122124
hide them. This is one of the problems with documentation and comments - unlike code, they can _lie_.
123125
124126
So with a quick detour to rename the function:
@@ -153,7 +155,7 @@ a more self-documenting CSS class name:
153155
...the comment is redundant.
154156
155157
The comments for the next two statements again pretty much repeat what
156-
their code is doing, especially with our new naming:
158+
their code is doing, especially with our updated naming:
157159
158160
```js
159161
// If a toggle element was provided, add an .active class
@@ -166,7 +168,7 @@ their code is doing, especially with our new naming:
166168
tabToShow.focus()
167169
```
168170
169-
...so we can remove them. They're noise.
171+
They're noise, and we can remove them.
170172
171173
### Worthwhile Comments
172174
@@ -184,7 +186,7 @@ The comment for the _next_ statements actually adds value:
184186
It explains how we ended up at the final two lines, and why we're adding a `tabindex` - that information
185187
would be tricky to convey in the code itself, so the comment has earned its place.
186188
187-
We can however, self-document a little more by adding a helper function:
189+
We can however, self-document a little more with a helper function:
188190
189191
```js
190192
function hasFocus(elementSelector) {

0 commit comments

Comments
 (0)