comparison or assignment

Discuss any general programming issues here
Post Reply
User avatar
r-tea
Posts: 62
Joined: Wed Feb 17, 2016 12:48 am
Location: Zdzieszowice, Poland
Contact:

comparison or assignment

Post by r-tea » Fri Aug 11, 2017 1:53 pm

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.

sashapont
Posts: 78
Joined: Thu Aug 03, 2017 2:49 pm

Re: comparison or assignment

Post by sashapont » Fri Aug 11, 2017 2:12 pm

Good remark! I absolutely agree!

SamuraiCrow
Posts: 130
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: comparison or assignment

Post by SamuraiCrow » Fri Aug 11, 2017 4:59 pm

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?

User avatar
r-tea
Posts: 62
Joined: Wed Feb 17, 2016 12:48 am
Location: Zdzieszowice, Poland
Contact:

Re: comparison or assignment

Post by r-tea » Sun Aug 13, 2017 12:22 am

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.

Post Reply