How do you refer to the currently logged in user in your code?

In other words - what would be a good name for a variable that refers to the logged in user?

I came up with a few:

  • logged_user
  • visitor
  • I

I'm not sure if any of them are good enough though.

+2


a source to share


7 replies


The lead point is worth some development.

It is very important to choose good variable names where the "good name" has properties

  • accurate (not named sloppily)
  • short (as short as possible, without loss of meaning)
  • unambiguous (not easy to confuse with something else)

If you get stuck, try to describe the thing in plain English.

Do you want to save the name of the user who is currently logged in? How about currentLoggedInUser ?

In your context, do you care that users are not logged in?



If not, then currentUser will do everything and it will be more concise.

Can anything else be confused?

Nope. We have a winner!

Now you can shorten it further, for example currUser , but you will lose some of the clarity. Remember, the IDE will be there to help you type, so you need to think about what you are losing (clarity) as a trade off of what you get (less keystrokes). This moment can be decided according to personal taste, when you develop for yourself and for yourself, but if you are in a team, it is not easy; choose the version that is easiest to understand in the future.

Think of this ax-wielding maniac who has to maintain your code in five years.

+2


a source


current_user seems like the obvious choice.



+5


a source


Either theUserWhoLoggedInAFewMinutesBeforeAndWhoHasNotYetLoggedOutAgain

or justich

+2


a source


authenticatedUser, validUser, activeUser, authUser

+2


a source


I would be more inclined to go with the simplest form user

. If you need to identify a previously logged in user, then it previousUser

is a logical choice. Also, if you need to distinguish between a user who is logged in or not, a simple trick isUserLoggedIn

will do the trick.

+2


a source


loggedOnUser

seems appropriate.

+1


a source


I think currentUser

OR loggedInUser

seems to be better.

+1


a source







All Articles