Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


increaseStat with negative value or decreaseStat function


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 jakson15   Members   -  Reputation: 423

Like
0Likes
Like

Posted 27 August 2014 - 04:51 PM

Not really a big deal but I was just wondering what people tend to prefer.

 

So you have a function that can increase a stat, do you pass a negative value or have two separate functions for incr/decr?

 

Programming on my own I feel passing a negative value works just as well.

But would it be questioned if I wrote that code for a company? 



Sponsor:

#2 NathanRunge   Members   -  Reputation: 658

Like
1Likes
Like

Posted 27 August 2014 - 06:24 PM

A single function should work perfectly well, but it might be wise to change the name to something like 'modifyStat' or 'changeStat' rather than 'increaseStat' if you intend to use it for both purposes or, in fact, if it is possible to use the method for both purposes.

 

EDIT: That said, I have been known to create both methods when it comes to things like 'damage' as opposed to 'heal.' Even when it's not necessary, and probably introduces a small degree of complexity, I like to map the public interface to logical actions that could be taken with the object. Still, I think one method is probably preferable in most circumstances, unless significantly different steps need to be taken for increases and decreases.


Edited by NathanRunge, 27 August 2014 - 06:27 PM.

Personal Page: http://www.nathanrunge.com/     Company Page: http://www.ozymandias.com.au/


#3 SeanMiddleditch   Crossbones+   -  Reputation: 10184

Like
1Likes
Like

Posted 27 August 2014 - 06:27 PM

It might be questioned, sure, but there are a bazillion terrible programmers out there with fancy titles in charge of projects who will want to impose their ways onto everyone. Don't worry about whether something will be questioned in a corporate environment. For learning, worry about whether it's good not, and then realize that once you work at a company you may have to adjust a little to "fit in" with whatever practices are in place, good or bad.

That said... I'd prefer the negative value. This moves any special logic about increase vs decrease into that one single function rather than making every single call of the function have to make the differentiation. It also makes it easier to have various game systems that don't actually know if they're going to raise or lower a stat but rather just evaluate some math and pass that along. e.g., then you only need a single ModifyStatEffect that has an integer and stat ID rather than needing both an IncreaseStatEffect and a DecreaseStatEffect (or a more complicated ModifyStatEffect).

Localize your logic as much as possible, don't force people to repeat themselves. With the separate functions, many callers are just going to have to cut-n-paste this code:

int mod = calculate_modification();
if (mod < 0)
  object->DecreaseStat(-mod);
else if (mod > 0)
  object->IncreaseStat(mod);
and that's not good.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS