comparison or assignment

Discuss any general programming issues here
Post Reply
User avatar
r-tea
Posts: 133
Joined: Tue Feb 16, 2016 11:48 pm
Location: Zdzieszowice, Poland
Contact:

comparison or assignment

Post 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.
sashapont
Posts: 152
Joined: Thu Aug 03, 2017 2:49 pm

Re: comparison or assignment

Post by sashapont »

Good remark! I absolutely agree!
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: comparison or assignment

Post 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?
I'm on registered MorphOS using FlowStudio.
User avatar
r-tea
Posts: 133
Joined: Tue Feb 16, 2016 11:48 pm
Location: Zdzieszowice, Poland
Contact:

Re: comparison or assignment

Post 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.
Post Reply