Thread: Fair use vs. abuse of the comma operator

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    18

    Fair use vs. abuse of the comma operator

    Hi everyone,

    I recently discovered the comma operator in C and it has certainly been useful in my projects. However, I worry that evaluating many instructions in one line can quickly uglify the code and potentially make it harder to read. Are there any generally accepted guidelines about what constitutes abuse of the comma operator and when it is okay/not okay to employ it?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    452
    Quote Originally Posted by C-UL8R View Post
    Hi everyone,

    I recently discovered the comma operator in C and it has certainly been useful in my projects. However, I worry that evaluating many instructions in one line can quickly uglify the code and potentially make it harder to read. Are there any generally accepted guidelines about what constitutes abuse of the comma operator and when it is okay/not okay to employ it?
    The comma operator is sometimes seen in for loops where you have two index variables

    Code:
    for (i=0; j = N-1; i < N; i++,j--)
    However even here many people frown on its use. Comma operators make code hard to read and seldom have a logical purpose, and most people avoid them. I do know of one programmer who disagrees with this and uses them heavily, but he is very much the exception.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    Quote Originally Posted by Malcolm McLean View Post
    The comma operator is sometimes seen in for loops where you have two index variables

    Code:
    for (i=0; j = N-1; i < N; i++,j--)
    Was that a way to subtly discourage copying the example by introducing a syntax error?

    I rarely use the comma operator because as you observed I too find that "evaluating many instructions in one line can quickly uglify the code and potentially make it harder to read". Then again, I also usually declare variables of the same type in the same scope separately, and use braces for if statements and for loops whose body consists of a single statement, though I am not necessarily opposed to the comma operator in the "two index variables" scenario... but then index variables tend to have a very small scope and terse names.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comma operator in c
    By Abhishek Kumar in forum C Programming
    Replies: 2
    Last Post: 03-01-2014, 09:02 AM
  2. Comma operator
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 10-20-2010, 05:35 AM
  3. k&r page 63: comma operator
    By alecodd in forum C Programming
    Replies: 3
    Last Post: 09-16-2010, 11:10 PM
  4. comma operator
    By raghuveerd in forum C Programming
    Replies: 5
    Last Post: 07-09-2007, 04:52 AM
  5. Comma Operator
    By doraiashok in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 08:38 AM

Tags for this Thread