By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,612 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,612 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
  1. if (cond1 && cond2 && (cond3 && cond4) && (cond5 || cond6) {
  2. // do this
  3. }
  4.  
Expand|Select|Wrap|Line Numbers
  1. if (cond1) {
  2.   if (cond2) {
  3.      if (cond3 && cond4) {
  4.          if (cond5 || cond6) {
  5.          // do this
  6.          }
  7.      }
  8. }
  9.  
Expand|Select|Wrap|Line Numbers
  1. if ( CheckCondition() ) {
  2. // do this
  3. }
  4.  
  5. public bool CheckCondition () {
  6.   return bool a = cond1;
  7.   bool b = cond2;
  8.   bool c = (cond3 && cond4);
  9.   bool d = (cond5 || cond6);
  10.  
  11. }
  12.  
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
Share this Question
Share on Google+
5 Replies


acoder
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
  1. function CheckCondition () {
  2.   return (cond1 && cond2 && (cond3 && cond4) && (cond5 || cond6));
  3.  }
Dec 9 '08 #2

gits
Expert Mod 5K+
P: 5,282
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

gits
Expert Mod 5K+
P: 5,282
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

Post your reply

Sign in to post your reply or Sign up for a free account.