How Classic Writing Rules Can Sharpen Your JavaScript Code Style
This article adapts William Strunk Jr.'s timeless seven writing principles to JavaScript development, showing how functions that do one thing, minimal code, active voice, logical grouping, avoiding spaghetti, feature‑based file organization, and true‑value naming can produce cleaner, more maintainable front‑end code.
In 1920 William Strunk Jr. published *The Elements of Style*, a list of seven writing rules that have endured for a century. The article demonstrates how developers can apply the same principles to JavaScript code to improve readability and maintainability.
Use paragraphs as the basic unit – one idea per paragraph.
Eliminate unnecessary statements.
Prefer the active voice.
Avoid a series of loosely connected sentences.
Keep related content together.
Make positive statements.
Use distinct structures for different concepts.
1. A function should do one thing
Software development is fundamentally about composition – combining modules, functions, and data structures.
In JavaScript functions can be classified as I/O, procedural, or mapping. Most practical functions are mapping functions that return a value for a given input. Mixing I/O with mapping violates the “one‑thing” rule.
An ideal pure function returns the same output for the same input, has no side effects, and is easy to test.
2. Remove unnecessary code
Less code means fewer bugs and a higher signal‑to‑noise ratio. The following example shows a verbose function and its concise ES6 arrow‑function equivalent.
function secret(message) {
return function () {
return message;
};
};Can be simplified to: const secret = msg => () => msg; Removing superfluous variables also improves clarity. For instance, the full‑name function can be written without an intermediate fullName variable.
const getFullName = ({firstName, lastName}) => `${firstName} ${lastName}`;3. Use active voice
Active verbs make intent clear. Prefer myFunction.called() over myFunction.wasCalled(), createUser over User.create(), and notify() over Notifier.doNotification().
4. Keep related code together
Separate concerns such as data loading, state calculation, and rendering into distinct functions. This enables unit testing of each part and avoids “spaghetti” code.
5. Group files by feature, not by type
.
├── todos
│ ├── component
│ ├── reducer
│ └── test
└── user
├── component
├── reducer
└── testFeature‑based organization reduces navigation overhead in large projects.
6. Write code that reflects true values
Prefer predicates that read positively, e.g., isFlying instead of isNotFlying, and write conditionals that test for the presence of a value rather than its absence.
if (err) return reject(err);7. Choose appropriate composition techniques
Function composition (e.g., compose2, pipe) replaces intermediate variables and clarifies data flow. Example using Lodash flow:
import pipe from 'lodash/fp/flow';
const incThenDouble = pipe(g, f);
incThenDouble(20); // 42By following these adapted style rules, JavaScript code becomes simpler, more robust, easier to debug, and maintainable without becoming overly simplistic.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
