Skip to content

Latest commit

 

History

History
28 lines (20 loc) · 1.72 KB

27-DRY-principle.md

File metadata and controls

28 lines (20 loc) · 1.72 KB

Don't Repeat Yourself

The "Don't Repeat Yourself" (DRY) principle is a software development principle that states that code should be written in such a way that there is no duplication of functionality. This principle is widely used in many programming languages, including JavaScript.

In JavaScript, following the DRY principle can help you write more maintainable and efficient code. For example, instead of copying and pasting the same code block in multiple places throughout your program, you can create a function that encapsulates that functionality and call it whenever you need it. This way, if you need to make changes to that functionality later, you only need to make the change in one place.

Another way to follow the DRY principle in JavaScript is to avoid hardcoding values in your code. Instead, you should use variables or constants to store values that are likely to change, so that you only have to update the value in one place if it needs to be changed.

Here's an example of how to follow the DRY principle in JavaScript:

// Bad Code
console.log("Hello, World!");
console.log("Hello, World!");
console.log("Hello, World!");

// Good Code
function greet() {
    console.log("Hello, World!");
}
greet();
greet();
greet();

In this example, the bad code violates the DRY principle by duplicating the same string multiple times in the code. The good code, on the other hand, follows the DRY principle by encapsulating the string in a function that can be called multiple times instead of duplicating it.

In summary, the DRY principle states that code should be written in such a way that there is no duplication of functionality, following this principle in JavaScript can help you write more maintainable and efficient code.