comparison or assignment

Discuss any general programming issues here

comparison or assignment

Postby 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.
User avatar
r-tea
 
Posts: 59
Joined: Wed Feb 17, 2016 12:48 am
Location: Zdzieszowice, Poland

Re: comparison or assignment

Postby sashapont » Fri Aug 11, 2017 2:12 pm

Good remark! I absolutely agree!
sashapont
 
Posts: 25
Joined: Thu Aug 03, 2017 2:49 pm

Re: comparison or assignment

Postby 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?
SamuraiCrow
 
Posts: 89
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: comparison or assignment

Postby 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.
User avatar
r-tea
 
Posts: 59
Joined: Wed Feb 17, 2016 12:48 am
Location: Zdzieszowice, Poland


Return to General programming

Who is online

Users browsing this forum: No registered users and 2 guests