single responsibility functions
image aythor: https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSR5p42RUa9veMuZNWG-OvFyrx6No0qB3NZVg&usqp=CAU

single responsibility functions

How to make your function only have one responsibility?

As the name say it this principle talks about make functions with only one responsibility.

pros

  • more readable code
  • more robust
  • more maintainable
  • smaller functions

I'm going to explain with a practical example, imagine that you have the following function:

function getData(baseUrl, route) {
  fetch(`${baseUrl}${route}`)
    .then(response => response.json())
    .then(data => console.log(data));  
}

nothing weird with the above function, just a normal function, is a function that works with two arguments (baseUrl, route) and a simple fetch with the arguments, handle the response and that's it, looks like a single responsibility function but the true is not, the function above do 3 things, give the route, give the base url and fetch the data.

Let's make single responsibility for each responsibility

  1. first step is return an arrow function to handle the route appart the baseUrl, that allow us have apart the task to give the baseUrl and the task to give route
function getData(baseUrl) {

  return (route) => {

   fetch(`${baseUrl}${route}`)
    .then(response => response.json())
    .then(data => console.log(data));
 } 
 
}

you can use the above function like this:

const getExpecificData = getData("https://example.com")

const getUsersFromExpecificData = getExpecificData("/users")

You can see it? yes, it is completely beautiful

let's keep going

2. apply the above steps

function getData(baseUrl) {

  return (route) => {


  return (callback) => {

    fetch(`${baseUrl}${route}`)
    .then(response => response.json())
    .then(data => callback(data));

  }   

 } 
 
}


you can use the function like this:

const getExpecificData = getData("https://example.com")


const getUsersFromExpecificData = getExpecificData("/users")

const getUserNamesFromExpecificData = getUsersFromExpecificData(
     user => user.name  
  )
  


now we can handle the data of any fetch with any baseURL with any route.

I hope that you enjoy this post, all the best for you

要查看或添加评论,请登录

Bernardo Aguayo Ortega的更多文章

  • Get Subsets of arrays(4)

    Get Subsets of arrays(4)

    What if we only want a subset of data, no to transform the array we only want the elements that we are interesting on…

  • Perform Actions on all elements arrays(3)

    Perform Actions on all elements arrays(3)

    There are a method incredible powerful call map that allow us to make transform in each element of the array. //we have…

  • Check Element Existence in Arrays (2)

    Check Element Existence in Arrays (2)

    If you work with a hard code values is easy to work with them because you know where are the values inside the array…

  • Build Flexible Collections with Arrays (1)

    Build Flexible Collections with Arrays (1)

    There are a data structure to save collection of data call it array use [] brackets instead {}, Arrays are a kind of…

  • Merge Objects with Object Spread

    Merge Objects with Object Spread

    If you want to merge various objects you need to spread object's properties in new object and literary since ECMAScript…

  • Object Destructuring (JavaScript)

    Object Destructuring (JavaScript)

    Object destructuring allows us to pull properties from an object and make them into variables, in other words allow us…

  • Use Objects for Managing key-value pairs (js)

    Use Objects for Managing key-value pairs (js)

    We already know the different types of variables in JavaScript so you can see a variable as a box and an object like a…

    2 条评论
  • Shorter functions with arrow functions

    Shorter functions with arrow functions

    With ES6 we have a new form to write functions, that allow us to write shorter functions, we don't need any more the…

    4 条评论
  • How Functions Should Be Named

    How Functions Should Be Named

    A few things to remember when naming functions:Name functions after a verb which clearly conveys what they do Omit…

  • Better functions with default parameters (javascript)

    Better functions with default parameters (javascript)

    Imagine the you have a function function convertTemperature(celsius , decimalPlaces) { ? const fahrenheit = celsius *…

    4 条评论

社区洞察