1,105,320 Community Members

One of those stupid questions: What is "+=" and "-="?

Member Avatar
riahc3
 
1,397 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
0
 

Hello

This is one of those stupid questions but you gotta ask to be sure

What is

var h=0;
h += 5;

var l=0;
l -= 5;

Is it the same as:

var h=0;
h = h+ 5;

var l=0;
l = l- 5;

Thanks and sorry for the stupidity

Member Avatar
JorgeM
IT Addict
6,381 posts since Dec 2011
Reputation Points: 567 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
1
 

Yes you are correct... About the examples not about the stupidity. :-)

Member Avatar
riahc3
 
1,397 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
0
 

OK, thank you.

I hate code written like that. It takes a second to copy and paste instead of writing something like h+=l

Thank you. Solved.

Question Answered as of 10 Months Ago by JorgeM
Member Avatar
Nichito
Posting Virtuoso
1,724 posts since Mar 2007
Reputation Points: 393 [?]
Q&As Helped to Solve: 81 [?]
Skill Endorsements: 4 [?]
Featured
 
0
 

You shouldn't hate it. Instead, get used to it since lots of languages allow it and the majority of programmers prefer it that way. In a way it optimizes your code (albeit for a few bytes), since variable names are not always one character long, and it makes it easier to keep track of what is going on when the operation is longer than a simple n + 1.

While I'm at that, I'd recommend to get used to seeing the n++and n-- syntax, which are used very often as well.

Member Avatar
riahc3
 
1,397 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
0
 

While I'm at that, I'd recommend to get used to seeing the n++and n-- syntax, which are used very often as well.

Always change that as well.

n=n+1;

it optimizes your code (albeit for a few bytes)

Programmer should understand the code, not the computer. It should be optimized for the programmer, not the computer.

Sorry, its a heated debate I feel passioned about.

Member Avatar
deceptikon
Eternally Awesome
4,668 posts since Jan 2012
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 679 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
3
 

Always change that as well.
n=n+1;

I hope you're not changing code that someone else wrote just to suit your own aesthetic preferences. That's an excellent way to introduce bugs.

I'd also argue that your preference is unconventional and will cause programmers to stumble over your code not because they don't understand it, but because they don't trust that you understand it. When a language supports x += y or x++ for example, any use of x = x + y will be viewed with suspicion; this is justified, in my opinion, because bad programmers are the ones that use such constructs without good reason.

Member Avatar
riahc3
 
1,397 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
-4
 

I hope you're not changing code that someone else wrote just to suit your own aesthetic preferences. That's an excellent way to introduce bugs.

Yes, I do. As of today any time I see x+=y or x++, as long as I am 100% sure of the change, I change it to x=x+y and x=x+1 the way it is suppose to be expressed. And as of today, I have never introduced a bug because of it.

I'd also argue that your preference is unconventional and will cause programmers to stumble over your code not because they don't understand it, but because they don't trust that you understand it. When a language supports x += y or x++ for example, any use of x = x + y will be viewed with suspicion; this is justified, in my opinion, because bad programmers are the ones that use such constructs without good reason.

Unconventional? I dont understand it? How can I understand code that is clearly written?

My reason is good, if not the best: Programmers should code the easiest way possible for them to understand their code.

If you think for writing x++ instead of x+1 make a programmer understand his code a lot easier, then sadly we are still stuck in that 50s-60s (I think that was the time Fortran was introduced) programming mentality where code should be as simple as possible and for the machine to understand it, not us.

Member Avatar
Nichito
Posting Virtuoso
1,724 posts since Mar 2007
Reputation Points: 393 [?]
Q&As Helped to Solve: 81 [?]
Skill Endorsements: 4 [?]
Featured
 
0
 

I change it to x=x+y and x=x+1 the way it is suppose to be expressed.

If it is "the way it is supposed to be expressed", are you implying that the guys who invented the programming language were wrong by adding these expressions?

And as of today, I have never introduced a bug because of it.

Then, luckily for you, you haven't found a complex enough program.

My reason is good, if not the best: Programmers should code the easiest way possible for them to understand their code.

If you think for writing x++ instead of x+1 make a programmer understand his code a lot easier, then sadly we are still stuck in that 50s-60s (I think that was the time Fortran was introduced) programming mentality where code should be as simple as possible and for the machine to understand it, not us.

It is not about making code simple FOR the machine... As long as your syntax is correct, compilers don't give a rat's behind about how your code is laid out. You might as well be making your arithmetic expressions longer and more ridiculous and the machine will give you the same result you're expecting, since it doesn't care about rules, standards or optimal code; as long as you have your syntax right.

for(i=0; i<employeeArray.length; i++){
    employeeArray[i].privilegeString += companyPrivileges.employeesByLevel(employeeArray.level++).getPrivileges();
}

This is just some random code I though about now, maybe not as "complex" as I intended it to be. It doesn't even make much sense... But which do you think is more optimal? The x++ x+= way, or the way you like it? If your answer is b then there is no doubt that you live up to your signature, and should read a little about code optimization.

Doing things your way makes unnecessarry memory allocations for the same variable and may confuse other programmers. Mantaining code laid out that way only lowers the idea other programmers may have of you and is not healthy for the programmer community. I know what we tell you may not change the way you do it, but I recommend you change your ways and start adopting the optimal way to do it.

Member Avatar
deceptikon
Eternally Awesome
4,668 posts since Jan 2012
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 679 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

I change it to x=x+y and x=x+1 the way it is suppose to be expressed.

It seems you have an extremely low opinion of your peers. Your arrogance and condescension of others is palpable; I'm truly grateful that you're not one of my coworkers.

Member Avatar
stbuchok
Posting Shark
957 posts since May 2011
Reputation Points: 123 [?]
Q&As Helped to Solve: 142 [?]
Skill Endorsements: 2 [?]
 
1
 

riahc3, I've seen you ask a lot of questions on here. If I were you, I'd look at what others have just said and take it to heart.

x++ and x += ... are considerably easier to read once you understand what they do.

How do you write your for loops?

for(int i = 0; i < length; i++){...}

or

for(int i = 0; i < length; i += 1){...}

Please don't be so narrow minded and look for the reasons why something is written a certain way before you condemn it.

Member Avatar
Troy III
Practically a Posting Shark
891 posts since Jun 2008
Reputation Points: 174 [?]
Q&As Helped to Solve: 119 [?]
Skill Endorsements: 10 [?]
 
2
 

{ i = i + 1 } = "I just started coding and I simply Rock!"

{ i += 1 } = "I know, I used to suck so hard..."

{ i++ } = "Am I finally - getting the hang of it?"

Member Avatar
riahc3
 
1,397 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
0
 

If it is "the way it is supposed to be expressed", are you implying that the guys who invented the programming language were wrong by adding these expressions?

Never planted this but yes; I agree adding these expressions were wrong.

Then, luckily for you, you haven't found a complex enough program.

Problably you are correct.

It is not about making code simple FOR the machine... As long as your syntax is correct, compilers don't give a rat's behind about how your code is laid out. You might as well be making your arithmetic expressions longer and more ridiculous and the machine will give you the same result you're expecting, since it doesn't care about rules, standards or optimal code; as long as you have your syntax right.

for(i=0; i<employeeArray.length; i++){
    employeeArray[i].privilegeString += companyPrivileges.employeesByLevel(employeeArray.level++).getPrivileges();
}

This is just some random code I though about now, maybe not as "complex" as I intended it to be. It doesn't even make much sense... But which do you think is more optimal? The x++ x+= way, or the way you like it? If your answer is b then there is no doubt that you live up to your signature, and should read a little about code optimization.

Why stop there?

for(i=0;i<employeeArray.length;i++){employeeArray[i].privilegeString+=companyPrivileges.employeesByLevel(employeeArray.level++).getPrivileges();}

Lets strip all the spaces to optimize even more!

....my answer would be C........um....

x++ A
x+= B
x=x+1 C

Doing things your way makes unnecessarry memory allocations for the same variable and may confuse other programmers. Mantaining code laid out that way only lowers the idea other programmers may have of you and is not healthy for the programmer community. I know what we tell you may not change the way you do it, but I recommend you change your ways and start adopting the optimal way to do it.

That bold parts MAKES NO SENSE.

x++ is understandable but x=x+1 is even clearer. I have yet to see a kindergarden teacher tell her students that to add you have to but x++. I mean, my jaw drops when I read that x++ is more clearer than x+1.

I dont give a shit about the "programmer community". I program for me and/or who I work for/with.

It seems you have an extremely low opinion of your peers. Your arrogance and condescension of others is palpable; I'm truly grateful that you're not one of my coworkers.

Im also grateful you are not one of my coworkers either. You should let your coworkers work how they wish as long as they get the job done.

riahc3, I've seen you ask a lot of questions on here. If I were you, I'd look at what others have just said and take it to heart.

x++ and x += ... are considerably easier to read once you understand what they do.

How do you write your for loops?

> for(int i = 0; i < length; i++){...}

or

> for(int i = 0; i < length; i += 1){...}

Please don't be so narrow minded and look for the reasons why something is written a certain way before you condemn it.

Actually if it is a very complex inner loop, I do

for (int i=0;i<5;i=i+1)

Helps me keep track...

Member Avatar
riahc3
 
1,397 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
0
 

Anyways the original question was solved: Lets not go on a pointless debate please :)

Member Avatar
deceptikon
Eternally Awesome
4,668 posts since Jan 2012
Reputation Points: 1,339 [?]
Q&As Helped to Solve: 679 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

You should let your coworkers work how they wish as long as they get the job done.

It doesn't work that way. Your statement tells me that either you've never worked on a team before or the only teams you've worked on have been exceptionally bad. But whatever floats your boat. I'm confident you'll never be in a position to join any of my teams (because you wouldn't be hired), so your poor choices really don't affect me at all.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: