Page 1 of 1

comparison or assignment

Posted: Fri Aug 11, 2017 1:53 pm
by r-tea
It's about the "=" operator.
Is there an enough important reason the Hollywood uses the same operator "=" for comparison and assignments as well?
My code grows up, and it makes me confused sometimes looking through and seeing e.g. val=0. I had to stop for a while and hard figure out is it an assignment or not.

Re: comparison or assignment

Posted: Fri Aug 11, 2017 2:12 pm
by sashapont
Good remark! I absolutely agree!

Re: comparison or assignment

Posted: Fri Aug 11, 2017 4:59 pm
by SamuraiCrow
There is a C/C++ coding convention in GCC about having assignments in the middle of an expression that states that if you put two sets of parentheses around it, you meant it as an assignment. Otherwise it warns you that you may have meant to use the == comparison operator.

Putting a set of parentheses around any comparison would accomplish a similar goal in Hollywood. Thus if you want to assign the result of a boolean comparison between a and b to variable c, it would look like this:

Code: Select all

c=(a=b)
Does that help?

Re: comparison or assignment

Posted: Sun Aug 13, 2017 12:22 am
by r-tea
Not really. Looking at your exanple I would as well think you want to assign the value of b to the variable a, and simultaneously to the variable c.