very long if statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tomaz
    New Member
    • Oct 2008
    • 29

    very long if statement

    hi everybody

    i've been searching a clean and correct way to use very long if statements.

    multiple options:

    Code:
    if (cond1 && cond2 && (cond3 && cond4) && (cond5 || cond6) {
    // do this
    }
    Code:
    if (cond1) {
      if (cond2) {
         if (cond3 && cond4) {
             if (cond5 || cond6) {
             // do this
             }
         }
    }
    Code:
    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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:
    Code:
    function CheckCondition () {
      return (cond1 && cond2 && (cond3 && cond4) && (cond5 || cond6));
     }

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      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

      Comment

      • tomaz
        New Member
        • Oct 2008
        • 29

        #4
        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...

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          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

          Comment

          • tomaz
            New Member
            • Oct 2008
            • 29

            #6
            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...

            Comment

            Working...