Question

In one of my projects, I had to name some variables and functions that belong to the person using my app. I named them userCard, getUserInventory() and so on. I named them like that cause there were variables and functions that belonged to other persons, like opponentCard, getAllyInventory() etc.

Now, I have a model class named User, which contains account information for a specific user. Like other variables and functions, this model can belong to the person using my app, as well as his/her opponent and ally. If I follow previous conventions, I have to name them userUser, getUserUser(), opponentUser, getAllyUser() etc, which I don't like, specially the userUser and getUserUser() ones.

What strategy should I follow, assuming I can't change the names of the models, but only the prefixes (user-, opponent-, ally-)?

Was it helpful?

Solution

I ended up using own as prefix.

So variables and functions belonging to the user are named like ownCard, ownUser, getOwnInventory(). Variables and functions belonging to others are unchanged (opponentUser, opponentCard, getAllyInventory()).

OTHER TIPS

It seems your app is a competitive multi-player game and you want to discriminate between "just any user" on the one hand and "the user on this end of the app" on the other hand.

All users are players or contestants or participants. I would avoid the generic term user altogether, it has more of a professional feel to it. What about ThisPlayer or PlayerZero for the player holding the device the app runs on?

If you are using an object-oriented language, the state-of-the-art approach is to define an interface:

interface IUserRepository
{
    User GetCurrentUser();
    User GetSpecificUser(Expression<User,bool> expression);
}

Then you'd create an object that implements the interface and inject it into the game controller:

class EquipmentController
{
    private readonly IUserRepository _users; //Injected

    public EquipmentController(IUserRepository userRepo)
    {
        _users = userRepo;
    }

    public void EquipBestSword()
    {
        _users.GetCurrentUser().GetInventory()
           .Where
           (
               i => i.Name.Contains("Sword")
           )
           .OrderByDescending
           (
               i => i.Damage
           )
           .Equip();
    }
}

That way you can mock the user and inventory in order to unit test your logic.

//Arrange
var mockWeapon = new Weapon { Name = "Sword", Damage = 100 };
var mockUsers = new MockUserRepository();
mockUsers.CurrentPlayer = new User
{
    Inventory = new List<GameItem>
    {
        mockWeapon
    }
}
var equipment = new EquipmentController(mockUsers);

//Act
equipment.EquipBestSword();

//Assert
Assert.IsTrue(mockWeapon.IsEquipped);
Licensed under: CC-BY-SA with attribution
scroll top