Don’t write code that is too DRY

@featureA/src/common
|-util.js
|-settings.js
|-constants.js
@featureB/src/common
|-util.js
|-settings.js
|-constants.js
// !!! multiple constants have the same value !!!
// This is not DRY
const MY_CONST_1 = "some value";
const MY_CONST_2 = "some value";
const MY_CONST_3 = "some value";
// !!! constants used only once !!!
// This extra layer of abstraction is not needed
const MY_CONST_COMPONENT_A = "a value only used in A";
const MY_CONST_COMPONENT_B = "a value only used in B";
const MY_CONST_COMPONENT_C = "a value only used in C";
  • A unit of code (class, variable, function, static method) used only once that is not in the same unit of code as its sole caller.
  • A unit of code that is obviously business related in a generic unit of code: e.g. a isShoppingCartEmpty function in a util.js file. Why isn’t this function in the ShoppingCart class?
  • A function that is just a big if/else where the conditional tests the presence of an argument:
function doSomething(arg1) {
if(!arg1) { // <-- this can indicate that the code was copy/pasted
// do something
} else {
// do almost the same thing, but with arg1
}
}
  • A one line function with a generic name that handles business logic
// misleading name
const isArrayEmpty = shoppingCart => shoppingCart.items.some(!tax);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store