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?
JavaScript
x
34
34
1
let someThink
2
let someWhere
3
4
if(someThink === true){
5
// get someData in a database and after
6
if( someData > 5 ){
7
8
if(someWhere === true){
9
// get someData2 in a database and after
10
if( someData2 > 3 ){
11
console.log("Go to plans page")
12
} else {
13
console.log("Update data")
14
}
15
} else {
16
console.log("Go to plans page")
17
}
18
19
} else {
20
console.log("Update data")
21
}
22
23
} else if (someWhere === true){
24
// get someData2 in a database and after
25
if( someData2 > 3 ){
26
console.log("Go to plans page")
27
} else {
28
console.log("Update data")
29
}
30
31
} else {
32
console.log("Go to plans page")
33
}
34
Advertisement
Answer
You can use some early returns after finding a case for updating the data:
JavaScript
1
21
21
1
let someThink
2
let someWhere
3
4
if (someThink) {
5
// get someData in a database and after
6
if (someData <= 5) {
7
console.log("Update data")
8
return;
9
}
10
}
11
12
if (someWhere) {
13
// get someData2 in a database and after
14
if (someData2 <= 3) {
15
console.log("Update data")
16
return;
17
}
18
}
19
20
console.log("Go to plans page");
21
You can avoid the early returns by putting the data fetching stuff in some helper function instead of inside the decision logic:
JavaScript
1
11
11
1
let someThink
2
let someWhere
3
4
if (someThink && getSomeData() <= 5
5
|| someWhere && getSomeData2() <= 3
6
) {
7
console.log("Update data")
8
} else {
9
console.log("Go to plans page");
10
}
11