Ad
Repeated If Else Blocks
I have an if else construction that will sound absurd to you. Actually, I wrote a basic version of my real code so as not to prolong it. Is there anyone who can rationalize it?
let someThink
let someWhere
if(someThink === true){
// get someData in a database and after
if( someData > 5 ){
if(someWhere === true){
// get someData2 in a database and after
if( someData2 > 3 ){
console.log("Go to plans page")
} else {
console.log("Update data")
}
} else {
console.log("Go to plans page")
}
} else {
console.log("Update data")
}
} else if (someWhere === true){
// get someData2 in a database and after
if( someData2 > 3 ){
console.log("Go to plans page")
} else {
console.log("Update data")
}
} else {
console.log("Go to plans page")
}
Ad
Answer
You can use some early returns after finding a case for updating the data:
let someThink
let someWhere
if (someThink) {
// get someData in a database and after
if (someData <= 5) {
console.log("Update data")
return;
}
}
if (someWhere) {
// get someData2 in a database and after
if (someData2 <= 3) {
console.log("Update data")
return;
}
}
console.log("Go to plans page");
You can avoid the early returns by putting the data fetching stuff in some helper function instead of inside the decision logic:
let someThink
let someWhere
if (someThink && getSomeData() <= 5
|| someWhere && getSomeData2() <= 3
) {
console.log("Update data")
} else {
console.log("Go to plans page");
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad