For example, suppose if I had a Character
class and an inventory, before taking out a weapon to use it, it's probably a good idea to check if that weapon exists first, so I might have a method called has(GameObject weapon);
that returns a boolean. I can then use it in an if
statement like this:
if character.has(sword):
# rest of code here
I like this because it almost sounds like English and flows better. However, I could also have a method called check_inventory(GameObject weapon);
that does the same thing, but doesn't flow as well, for example:
if character.check_inventory(GameObject Weapon)
# rest of code
Which approach is better? In the second example, does it break encapsulation to suggest we are checking a collection of some type?
has()
is pretty broad. Can you compromise and use something likehasInventoryItem()
?hasItem()
,hasAbility()
, etc...