Thread: Relying on order of execution

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92

    Relying on order of execution

    Is it bad practice to assume the proper (left to right) execution of boolean expressions for simplicity?

    Reliant code:
    Code:
    if(
      !Is_Next_Match(Parser, TOKEN_WEIGHT)                                           ||
      !Next_Number(Parser, j, INT)                                                   ||
      j != i                                                                         ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Joint          ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Bias           ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Position[_X_]) ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Position[_Y_]) ||
      !Next_Number(Parser, Character->Meshes[Mesh_Index]->Weights[i]->Position[_Z_])
    )
      goto Error_Currupt_File;
    Non-reliant code:
    Code:
    if(!Is_Next_Match(Parser, TOKEN_WEIGHT))
      goto Error_Currupt_File;
    if(!Next_Number(Parser, j, INT))
      goto Error_Currupt_File;
    ....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, it is perfectly fine, but be careful not to switch || for | otherwise you lose the sequence point introduced.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  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
    Well if you can't rely on that, your compiler is broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Excellent, thanks for the clarification

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    && and || are fast fail, so you can rely on them not only to execute the their operands in order, but to not execute the second argument when the result is determined by the first. ?: is similar too.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by King Mir View Post
    && and || are fast fail, so you can rely on them not only to execute the their operands in order, but to not execute the second argument when the result is determined by the first.
    A more usual description is "short-circuit evaluation".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. order of execution
    By manzoor in forum C++ Programming
    Replies: 15
    Last Post: 08-13-2009, 01:14 PM
  2. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  3. expression execution order
    By _Elixia_ in forum C Programming
    Replies: 3
    Last Post: 10-02-2003, 04:01 PM
  4. uncertainty in order of execution - any explanation?
    By ylzhang in forum C Programming
    Replies: 6
    Last Post: 05-31-2003, 08:04 AM
  5. randomizing order of execution of function
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-03-2002, 07:50 PM