Thread: complex if statement readability

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Question complex if statement readability

    what is the best way to make something like this more readable?
    is there a standard for complex if's like this one??

    Code:
    if((!ki.fname || ki.fname[0]=='0')&& ki.id==0)
      if(((!ki.sender ||  ki.sender[0]=='0') || 
      strcmp(ki.sender,curr->sender)==0)  &&  
      ((!ki.addressee || ki.addressee[0]=='0') || 
      strcmp(ki.addressee,curr->addressee)==0) &&
      ((!ki.regarding || ki.regarding[0]=='0') || 
      strcmp(ki.regarding,curr->regarding)==0) &&
      (ki.date.day == 0 || ki.date.day == curr->date.day) &&
      (ki.date.month == 0 || ki.date.month == curr->date.month) &&
      (ki.date.year == 0 || ki.date.year == curr->date.year)) 
      {
        hits->next=copynode(curr);
        hits=hits->next;		//bump pointer
      }
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    When writitng very complicated nested paranthesis, I just try to indent in the same manner as if I were nesting functions. I'm also not a big fan of using short lines, so it makes things easier to write, personally (althouh it turns out ugly when printed). Also, you could cut down on the number of paranthesis if you're comfortable with precedence and associativity.
    Code:
    if
    (
     (!ki.fname || ki.fname[0]=='0') &&
     (ki.id ==0) &&
     (!ki.sender ||  ki.sender[0]=='0' || strcmp(ki.sender,curr->sender)==0)  &&
     (!ki.addressee || ki.addressee[0]=='0' || strcmp (ki.addressee,curr->addressee)==0) &&
     (!ki.regarding || ki.regarding[0]=='0' || strcmp(ki.regarding,curr->regarding)==0) &&
     (ki.date.day == 0 || ki.date.day == curr->date.day) &&
     (ki.date.month == 0 || ki.date.month == curr->date.month) &&
     (ki.date.year == 0 || ki.date.year == curr->date.year)
    )
    {
     hits->next=copynode(curr);
     hits=hits->next;		//bump pointer
    }
    Although something I have to ask about this code, you're using things like !ki.sender, which makes sense if the pointer may be initialized to NULL, however NULL is not neccisarily 0. If you're intending to do something equivalent to ki.sender != NULL, I suggest you do so that a way. Also, I eliminated the nested if because... well, it could be done with one if, and that matched better.
    Last edited by QuestionC; 11-18-2001 at 07:14 PM.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is there a standard for complex if's like this one??
    Tricky - there's good formatting as suggested by QuestionC. Good formatting never goes amiss.

    You could try several local variables...
    t1 = (!ki.fname || ki.fname[0]=='0');
    t2 = (ki.id ==0);
    t3 = (!ki.sender || ki.sender[0]=='0' || strcmp(ki.sender,curr->sender)==0);
    ...
    if ( t1 && t2 && t3 )

    It's not strictly the same code, since && and || have short circuit evaluation (if t1 is false, t2 is not evaluated). But with several discrete expressions, they are all evaluated.

    This might not be a problem here, but in some cases it could be.

    The other advantage is that you can use the debugger to look at t1, t2 etc, to see why your test starts failing mysteriously.


    > however NULL is not neccisarily 0.
    http://www.eskimo.com/~scs/C-faq/q5.5.html

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I stand corrected... although if I rewrote C, I think I'd take that rule out.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Why am I getting 'undelcared identifier' ???
    By Bill83 in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 01:00 PM
  3. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  4. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM