Thread: Validation efficiency

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367

    Validation efficiency

    Hi, I have the four following variables, and the values of these variables need to be within a set a threshold of the optimal value. The only way I can think of at the moment to validate them is with the following code. If anyone could suggest an alternative way that would be great:

    Code:
    const int OPTIMAL_VAL = 5;
    const int THRESHOLD_VAL = 1;
    
    int iA = 4, iB = 5, iC = 5, iD = 6;
    
    if((OPTIMAL_VAL - THRESHOLD_VAL >= iA && iA <= OPTIMAL_VAL + THRESHOLD_VAL) &&
       (OPTIMAL_VAL - THRESHOLD_VAL >= iB && iB <= OPTIMAL_VAL + THRESHOLD_VAL) &&
       (OPTIMAL_VAL - THRESHOLD_VAL >= iC && iC <= OPTIMAL_VAL + THRESHOLD_VAL) &&
       (OPTIMAL_VAL - THRESHOLD_VAL >= iD && iD <= OPTIMAL_VAL + THRESHOLD_VAL))
    {
    
      //WHATEVER
    
    }
    I could put the values in an array, and go through each one. However, this is more time consuming. Additionally, the number of variables required is always going to be 4, so there is not really a maintainability advantage in using an array.

    Thanks.
    Be a leader and not a follower.

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    a function for each threshold check might be prettier, but w.e works
    .sect signature

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    It should be
    Code:
    OPTIMAL_VAL - THRESHOLD_VAL <= iA && iA <= OPTIMAL_VAL + THRESHOLD_VAL
    not
    Code:
    OPTIMAL_VAL - THRESHOLD_VAL >= iA && iA <= OPTIMAL_VAL + THRESHOLD_VAL
    And since OPTIMAL_VAL - THRESHOLD_VAL and OPTIMAL_VAL + THRESHOLD_VAL don't change, calculate them once.

  4. #4
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    the compiler will perform such trivial optimizations itself, no need to worry about that
    .sect signature

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    Quote Originally Posted by ggs
    the compiler will perform such trivial optimizations itself, no need to worry about that
    Sure, but it looks less cluttered

  6. #6
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    i'm all for that. recommend using a two letter macro, such as OT to replace them as well.

    (in all seriousness )
    .sect signature

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Hi all, Thanks for your replies. I noticed the problem about coditions being the wrong way. Thanks for the suggestions, I will implement them.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-10-2009, 08:51 AM
  2. Efficiency with c++
    By pastitprogram in forum C++ Programming
    Replies: 17
    Last Post: 08-08-2008, 11:18 AM
  3. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 08:49 AM
  4. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 01:10 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM