Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Comma operator considered harmful

Comma operator considered harmful

Scheduled Pinned Locked Moved The Weird and The Wonderful
graphicsgame-devquestion
5 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    o m n i
    wrote on last edited by
    #1

    From a console tic-tac-toe game: (drawing function)

    for (int x = 0, y = 0; y < 3; y+=((x==2)?1:0),x+=((x==2)?-2:1) ) {
    cout << boardStrings[board[x][y]] << ((x == 2) ? "\n" : "");
    }

    :((

    A O B B 4 Replies Last reply
    0
    • O o m n i

      From a console tic-tac-toe game: (drawing function)

      for (int x = 0, y = 0; y < 3; y+=((x==2)?1:0),x+=((x==2)?-2:1) ) {
      cout << boardStrings[board[x][y]] << ((x == 2) ? "\n" : "");
      }

      :((

      A Offline
      A Offline
      Al_Brown
      wrote on last edited by
      #2

      I hate to say it, but I quite like that code. With a couple of named constants instead of the use of literals 2 and 3 and more consistant spacing it would be nice and succinct.

      1 Reply Last reply
      0
      • O o m n i

        From a console tic-tac-toe game: (drawing function)

        for (int x = 0, y = 0; y < 3; y+=((x==2)?1:0),x+=((x==2)?-2:1) ) {
        cout << boardStrings[board[x][y]] << ((x == 2) ? "\n" : "");
        }

        :((

        O Offline
        O Offline
        oggenok64
        wrote on last edited by
        #3

        I'm glad to say that i like this code :-) No horror there i my opinion.

        - oggenok

        1 Reply Last reply
        0
        • O o m n i

          From a console tic-tac-toe game: (drawing function)

          for (int x = 0, y = 0; y < 3; y+=((x==2)?1:0),x+=((x==2)?-2:1) ) {
          cout << boardStrings[board[x][y]] << ((x == 2) ? "\n" : "");
          }

          :((

          B Offline
          B Offline
          BillW33
          wrote on last edited by
          #4

          While I do use the comma operator, I do so sparingly. I also like code to be easily readable, which this example is not. :sigh:

          Just because the code works, it doesn't mean that it is good code.

          1 Reply Last reply
          0
          • O o m n i

            From a console tic-tac-toe game: (drawing function)

            for (int x = 0, y = 0; y < 3; y+=((x==2)?1:0),x+=((x==2)?-2:1) ) {
            cout << boardStrings[board[x][y]] << ((x == 2) ? "\n" : "");
            }

            :((

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            That's not really bad, to me. Initialising and updating two variables in a loop is one of the well understood allowed places to use a comma. The thing that's wrong with this code is that the 3x3 nature of the board should be a variable (or at least a constant), not in there as a magic number 5 times.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups