Skip to content

Instantly share code, notes, and snippets.

@rikukissa
Last active September 19, 2019 07:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikukissa/0dd8c7df3681c139c0f92cee3a0d5466 to your computer and use it in GitHub Desktop.
Save rikukissa/0dd8c7df3681c139c0f92cee3a0d5466 to your computer and use it in GitHub Desktop.
Collection of code simplification and refactoring tips #best practices #refactoring #code review

logo


Collection of code simplification and refactoring tips

Want to write clean code? Well here's your chance. The following set of examples are practices that I've come across during my times as a programmer and that I believe in. At least at the moment. Please feel free to add comments, criticise my thinking and contribute to everyone's knowledge. I'm trying to find a better platform for these, but for now a gist should do. I'm doing this solely to document, analyse and share my own thinking, which oftentimes is quite unstructured. These days I'm not too fussed about the syntactical structures of the code, but would rather concentrate on painting the largest guidelines and deciding the direction I want my programming style to move towards.

Contents:

Further reading:

@rikukissa
Copy link
Author

rikukissa commented Oct 10, 2018

Stop repeating yourself!

You repeating yourself

👎

interface User {
 userCreatedAt: number;
 userPoints: number;
}

👍

interface User {
 createdAt: number;
 points: number;
}

Repeating the word user creates extra fuzz around the words that are actually relevant, without adding any value. The reader should be able to infer the context even when it's few lines above.


👎

it('should add two numbers together', ...)
it('should fail if divider is zero', ...)

👍

it('adds two numbers together', ...)
it('fails if divider is zero', ...)

By rephrasing the test case description, we can keep it as descriptive while making it more concise. Someone could even argue, that a test not only verifies that your code should do something, but that it actually does the thing it was written for.

Don't add unneeded context

@rikukissa
Copy link
Author

rikukissa commented Oct 10, 2018

Always name your constants

Got this idea while writing the bit about cyclic dependencies. You might have come across time calculation like this before:

// Returns true if token is older than 3 days
export function hasTokenExpired(token) {
  return (Date.now - token.createdAt) > 86400000 * 3;
}

The comment is pretty spot on, but we can do better:

export function hasTokenExpired(token) {
  const day = 24 * 3600 * 1000;
  return (Date.now - token.createdAt) > day * 3;
}

Not sure if you disagree, but to me it's much easier to read that something's greater than day * 3 (probably means 3 days) than that something's greater than 86400000 (possibly the mass of the Earth)

Great quote from clean-code-javascript:

We will read more code than we will ever write. It's important that the code we do write is readable and searchable. By not naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment