Sign in to follow this  
jakson15

increaseStat with negative value or decreaseStat function

Recommended Posts

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? 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this