I am currently available for freelance/contract work. Book a meeting so we can talk about your project.

Hiding repetition with helper functions

Posted on

Have to use a function over and over, but one or more of its parameters are always the same? Improve readability by creating a function that hides that repetition.

// Repeating "en_US" every time is tedious and prone to typos.
const title = translate("app.title", "en_US")
const slogan = translate("app.slogan", "en_US")
const action = translate("app.action", "en_US")
 
// While "translate" takes two parameters, this function only takes one. It
// returns the result we would get if we called `translate` with "en_US".
const translateEN = key => translate(key, "en_US")
 
// When we call our new function, we no longer need to write "en_US".
const title = translateEN("app.title")
const slogan = translateEN("app.slogan")
const action = translateEN("app.action")
// Repeating "en_US" every time is tedious and prone to typos.
const title = translate("app.title", "en_US")
const slogan = translate("app.slogan", "en_US")
const action = translate("app.action", "en_US")
 
// While "translate" takes two parameters, this function only takes one. It
// returns the result we would get if we called `translate` with "en_US".
const translateEN = key => translate(key, "en_US")
 
// When we call our new function, we no longer need to write "en_US".
const title = translateEN("app.title")
const slogan = translateEN("app.slogan")
const action = translateEN("app.action")
Debug
none
Grid overlay