This is my if-statement at the moment:
if (excel_getValue("A" + i) == "" &&
excel_getValue("A" + (i + 1)) == "" &&
excel_getValue("A" + (i + 2)) == "" &&
excel_getValue("A" + (i + 3)) == "" &&
excel_getValue("A" + (i + 4)) == "" &&
excel_getValue("A" + (i + 5)) == "" &&
excel_getValue("A" + (i + 6)) == "" &&
excel_getValue("A" + (i + 7)) == "" &&
excel_getValue("A" + (i + 8)) == "")
Is there anyway I can reduce this statement? Should I build this if-statement in a for-loop?
BTW, this if-statement if already in a forloop and it uses the i of the forloop
It is only bad if you need to do something when the condition is false. No, absolutely not! In fact, it is very common to have an if without an else when there is no specific activity that needs to be performed when the condition is false.
Use If/Else Statements in One Line To simplify the code and reduce the number of lines in the conditional statements, you can use an inline if conditional statement. Consider the following concise code that performs the same with one line for each if/else conditional statement.
There is nothing wrong with using if-statements, but avoiding them can sometimes make the code a bit more readable to humans. This is definitely not a general rule as sometimes avoiding if-statements will make the code a lot less readable. You be the judge. Avoiding if-statements is not just about readability.
You can use All
:
if(Enumerable.Range(0, 9).All(c => excel_getValue("A" + (i + c)) == "")) {
}
There's an easy way to "reduce" (at least "beautify") with a for
loop... this wouldn't be more performant though, so you choose:
bool do = true;
for(var j = 0; j < 9; j++) {
if (excel_getValue("A" + (i+j)) != "") {
do = false;
break;
}
}
if(do) { /* whatever */ }
Or using Linq:
if(Enumerable.Range(0, 9).All(x => excel_getValue("A" + (i + x)) == "")) { /* whatever */ }
This could be probably reduced easily, but I guess the point is that it should be "prettier", and not "better" or "complicated". Nothing would beat your original one in terms of performance :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With