Member-only story

DRY — Don’t repeat yourself

Oliver Jumpertz
4 min readJan 23, 2022

--

The DRY principle is an integral part of clean code, but what does it actually mean and what is it really good for?

Should you really care about it?

What does it actually mean?

Don’t write code that does the same thing twice.

It doesn’t mean that you must only ever write one loop within all of your code. But you shouldn’t rewrite business or core logic multiple times by hand.

An Example

Let’s take a look at a very basic example to give you an idea of what it actually looks like to unnecessarily repeat code.

async function getArticlesForUser(userId) {
const userResponse = await fetch(`/api/users/${userId}`, {
credentials: ‘include’,
redirect: ‘follow’
});
const user = userResponse.json();
const articleResponse = await fetch(`/api/articles/${user.authorId}`);
return articleResponse.json();
}
async function getCommentsForUser(userId) {
const userResponse = await fetch(`/api/users/${userId}`, {
credentials: ‘include’,
redirect: ‘follow’
});
const user = userResponse.json();
const commentResponse = await fetch(`/api/comments/${user.commentId}`);
return commentResponse.json();
}

--

--

Oliver Jumpertz
Oliver Jumpertz

Written by Oliver Jumpertz

Software Engineer - Content Creator

No responses yet

Write a response