codebar / tutorials

🗒 codebar's tutorials
http://tutorials.codebar.io/
258 stars 241 forks source link

JS Lesson 4: getGithubInfo should not return the xmlhttp object #451

Open codebru opened 4 years ago

codebru commented 4 years ago

Where

The tutorial https://github.com/codebar/tutorials/blob/gh-pages/js/lesson4/tutorial.md

What

The code block

function getGithubInfo(username) {
  var url = 'https://api.github.com/users/' + username;

  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open('GET', url, false);
  xmlhttp.send();

  return xmlhttp;
}

returns the xmlhttp object. I would argue that this is bad practice and the function should return only the data or a signifier of failure, for example throwing an error. The reason I think this is bad practice is that it as a function should only give data, and should not let its functionality "leak out" (sorry I am struggling to describe this well).

Am I willing to fix

Yes, I just wanted to know if people agreed with me first :)