A Reactjs coding style guide
This project is maintained by LinuxDevil
The dry principle, also known as the “don’t repeat yourself” principle, is a concept in software development that suggests that developers should avoid writing the same code multiple times in a program. The idea behind this principle is that writing the same code multiple times can lead to a number of problems, such as increased complexity, difficulty maintaining the code, and increased potential for errors. Instead, developers should strive to write code that is modular and reusable, so that they can avoid repeating themselves and make their code more maintainable and reliable.
// This function checks if a given number is even
function isEven(number: number): boolean {
return number % 2 === 0;
}
// This function checks if a given number is odd
function isOdd(number: number): boolean {
return number % 2 === 1;
}
// Without using the dry principle, we would have to write the same calculation
// multiple times in different functions, like this:
function isEvenOrOdd(number: number): 'even' | 'odd' {
if (number % 2 === 0) {
return 'even';
} else {
return 'odd';
}
}
// However, if we follow the dry principle, we can reuse the calculation
// from the previous functions and avoid repeating ourselves, like this:
function isEvenOrOdd(calculationNumber: number): 'even' | 'odd' {
return isEven(calculationNumber) ? 'even' : 'odd';
}