0
I’m creating a function with a . fetch to get data from openweathermap.org, which by definition returns in Fahrenheit. Is it acceptable from the point of view of Clean Code, which says that the function should do only one thing and have few lines, make an If-Else block to control it OR is it more appropriate to make another function with a flag and return the url in the desired form and use it in requestCurrentWeather()? Below is an example of how the extensive function looks with If-Else.
let flagUnitsFormat= true;
const requestCurrentWeather = cityName => {
let URL_CURRENT_WEATHER = `http://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${keyApiCurrent}`
if(flagUnitsFormat) {
fetch(`${URL_CURRENT_WEATHER}&units=metric`, {method:'get'})
.then(response => {
response.json()
.then(result => { console.log(result)});
})
.catch((error) => { console.error(error); });
} else {
fetch(URL_CURRENT_WEATHER, {method:'get'})
.then(response => {
response.json()
.then(result => { console.log(result)});
})
.catch((error) => { console.error(error); });
}
}
I followed your advice from the first example and found the result quite satisfactory. The function became small and fulfills the purpose well.
– F-Sampaio