Skip to content

Instantly share code, notes, and snippets.

@cjkoepke
Created August 10, 2017 17:17
Show Gist options
  • Save cjkoepke/a8afcd3dad90fa85e1f754d9a2602622 to your computer and use it in GitHub Desktop.
Save cjkoepke/a8afcd3dad90fa85e1f754d9a2602622 to your computer and use it in GitHub Desktop.
Terse is Better Than Verbose

Terse is Better Than Verbose

Below are some examples on how the best practice of writing "readable" code is not always synonymous with writing it verbosely. For example, in scenarios where your code is complicated, being verbose might actually be harder to scan and quickly see the purpose without a bunch of decorative wrappers.

See below for more explanation.

// This is usually preferred and labelled as more "readable".
function(param) {
if (param === true) {
doSomething();
} else {
return;
}
}
// It's much more readable and terse to define this logic like this.
function(param) {
if (!param) {
return;
}
doSomething();
}
// You can even take that a step further.
function(param) {
!param && return;
doSomething();
}
// Or even this route, which is arguably more readable (not to mention, pretty).
function(param) {
param
? doSomthing()
: return;
}
// Another great example is arrow functions. Besides their effect on the `this` keyword, they are also
// very effective at being more readable (once you get used to them).
// Traditional.
function add(int1) {
return function(int2) {
return int1 + int2;
}
}
// While a little more difficult to get used to, can actually make your code look less messy, and therefore more scannable.
const add = int1 => int2 => int1 + int2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment