Thread: breaking up an if statment for readability

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    breaking up an if statment for readability

    i have an if statement that i need to spread over several lines so its not one long line that spreads from lands-end to john o'groats

    Code:
    if ((temp_take_piece[coordinate_x - 1][coordinate_y + (1 * multiplyer)] == 'b') || (temp_take_piece[coordinate_x - 1][coordinate_y + 1] == 'B')) || ((temp_take_piece[coordinate_x - 1][coordinate_y + (1 * multiplyer)] == 'w') || (temp_take_piece[coordinate_x - 1][coordinate_y + 1] == 'W'))
    how can i do it please
    coop

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The compiler doesn't care about white space.

    So what's wrong with
    Code:
    if ( (temp_take_piece[coordinate_x - 1][coordinate_y + (1 * multiplyer)] == 'b') || 
         (temp_take_piece[coordinate_x - 1][coordinate_y + 1] == 'B') || 
         (temp_take_piece[coordinate_x - 1][coordinate_y + (1 * multiplyer)] == 'w') || 
         (temp_take_piece[coordinate_x - 1][coordinate_y + 1] == 'W')
       )
    Or just make a function out of it.
    Code:
    int checkTakePiece(temp_take_piece,coordinate_x,coordinate_y,multiplyer) {
        char lc = temp_take_piece[coordinate_x - 1][coordinate_y + (1 * multiplyer)];
        char uc = temp_take_piece[coordinate_x - 1][coordinate_y + 1];
        return lc == 'b' || lc == 'w' || uc == 'B' || uc == 'W';
    }
    
    ///
    
    if ( checkTakePiece(temp_take_piece,coordinate_x,coordinate_y,multiplyer) ) {
        // do stuff
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-30-2015, 07:26 AM
  2. Replies: 49
    Last Post: 06-09-2013, 10:09 AM
  3. need help with go to statment
    By begginer in forum C Programming
    Replies: 15
    Last Post: 04-06-2011, 10:54 AM
  4. complex if statement readability
    By mlupo in forum C Programming
    Replies: 3
    Last Post: 11-19-2001, 08:54 PM
  5. if statment
    By rkjd2 in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2001, 11:48 AM

Tags for this Thread