459,351 Members | 1,316 Online
Need help? Post your question and get tips & solutions from a community of 459,351 IT Pros & Developers. It's quick & easy.

# very long if statement

 P: 29 hi everybody i've been searching a clean and correct way to use very long if statements. multiple options: Expand|Select|Wrap|Line Numbers if (cond1 && cond2 && (cond3 && cond4) && (cond5 || cond6) { // do this }   Expand|Select|Wrap|Line Numbers if (cond1) {   if (cond2) {      if (cond3 && cond4) {          if (cond5 || cond6) {          // do this          }      } }   Expand|Select|Wrap|Line Numbers if ( CheckCondition() ) { // do this }   public bool CheckCondition () {   return bool a = cond1;   bool b = cond2;   bool c = (cond3 && cond4);   bool d = (cond5 || cond6);   }   a switch case statement is in my matter not very useable. What would be the best way to use, what are the points I should pay attention to? Are there any other options to obtain a fast and clean statement? Thank you very much for the tips Dec 9 '08 #1
5 Replies

 Expert Mod 15k+ P: 16,027 The first method would be OK in a one-off scenario, but the second one is more flexible if you want to later add else part conditions. The third method wouldn't work because it's not JavaScript and the code is incorrect. If you do find yourself using this if statement multiple times, a function does make sense, but code it something like this: Expand|Select|Wrap|Line Numbers function CheckCondition () {   return (cond1 && cond2 && (cond3 && cond4) && (cond5 || cond6));  } Dec 9 '08 #2

 Expert Mod 5K+ P: 5,390 thats true ... :) and just consider to use a function only when you really need the check more then once ... a function-call is much slower then a direct call ... even when the code would look much cleaner with the function it increases runtime especially in large loops ... so when this might be an issue ... then use a direct call ... we often avoid a 'script is running slow' message just by removing function calls in large loops - just a note because sometimes this might make problems. kind regards Dec 9 '08 #3

 P: 29 thanks for the tips. my case looks as follows: i create an array with all input elements. a for loop runs untill all input elements are checked. in that loop I have conditions that looks like this: - is the type 'text' or 'password': ok - i don't have the attribute 'no_vkb' - i don't have the 'fs_enabled' and 'df_enabled' attribute - the element is not disabled or read-only - the style is not null. so this if statement is done about 10 times on each page. some pages have 30 input fields... So, I suppose in this case, a function seems the right thing to do... Dec 9 '08 #4

 Expert Mod 5K+ P: 5,390 yep ... in such cases a function is not 'evil' ... i was talking about loops of hundreds of records ... sometimes here we have 3000 or even more ;) kind regards Dec 9 '08 #5

 P: 29 in fact, i have 2 functions with such a loop: one for the text input elements and one for the textarea fields. So If I could create a function with common conditions for both functions, that would be clean. Or at least it seems clean ;) in the loop itself I can add more specific conditions for each function, next to the CheckCondition function. I'll try it this way... Dec 9 '08 #6