-3

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-)?

  • 2
    That's simply a poor naming decision. Having already introduced the term "user", a better name for the model class might have been "Account" or "Player". – Ralf Kleberhoff Feb 18 '18 at 18:25
  • @RalfKleberhoff Thanks for the suggestion, but I can not change the model name, only the prefixes. – Ehtesham Hasan Feb 19 '18 at 3:39
  • 3
    @EhteshamHasan I know of nothing as destructive to the maintenance of software as a bad name. Do not give into the idea of living with one easily. – candied_orange Feb 19 '18 at 4:10
  • 3
    Typically I like to differentiate between "user" and "player"/"account"; I use "user" when referring to the person using the application, and "player" or "account" when referring to the in-application person. – mgthomas99 Feb 19 '18 at 9:52
2

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()).

1

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?

  • In my case, own as prefix seems more suitable (ownPlayer, ownUser, getOwnCard()). – Ehtesham Hasan Feb 19 '18 at 10:13
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.