d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Stylistic Question
Add Reply New Topic New Poll
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Sep 2 2013 02:14pm
Which one do you prefer?

1) a -= b - c
2) a = a - b + c

I would argue that while being a little shorter, 1) has a high potential to troll another person reading your code as it is easy to forget mentally inverting the second minus when skimming over a program.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 2 2013 02:19pm
Make code readable / thread
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Sep 2 2013 02:21pm
Quote (carteblanche @ Sep 2 2013 09:19pm)
Make code readable / thread

You did not say which version is more readable to you, though. Opinions on this might actually differ.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 2 2013 02:39pm
Quote (tt_toby @ Sep 2 2013 04:21pm)
You did not say which version is more readable to you, though. Opinions on this might actually differ.


cannot give an answer because it depends on the meaning of the variables. you're forgetting a lot more options

1) a -= b - c
2) a = a - b + c
3) a -= (b - c)
4) a = a - (b - c)
5) a = a - b - (-c)
6) a -= b; a += c;

or how about remove the compound operator entirely and use another variable

total = b - c
total = -(b + c)

suppose you are calculating the sum vector formed by two vectors, one pointing up and one pointing down.
vector = up - down

now suppose you have a parameter that will reduce down by some amount delta. then this makes perfect sense
vector = up - (down - delta)

whereas this may be a little confusing:
vector = up - down + delta

now suppose you have a parameter that will increase up by some amount delta. this this makes perfect sense
vector = (up + delta) - down

whereas this will be confusing
vector = up - (down - delta)

tldr if the business logic says (b - c) should be removed from a, then a -= (b-c) is fine. whereas if the business logic says c should be added, then don't put a negative sign.

This post was edited by carteblanche on Sep 2 2013 02:43pm
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Sep 3 2013 01:48am
Thanks, nice answer.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll