I'm re-designing the login functionality for our application, and the first thing I came across is something a previous developer wrote a while back. There's a simple Login class that just contains 2 strings, username
and password
, and an enum LoginStatus
:
public enum LoginStatus
{
LoggedIn,
LoggedOut
}
I'm thinking it would be better to just use a bool
here, but maybe there's something more here that I'm not understanding?
In my opinion, it is slower and more confusing to read:
if (login.LoginStatus == LoginStatus.LoggedIn)
{
...
}
It would seem a lot better to just write:
if (login.loggedIn) { ... }
or
if (!login.loggedIn) { ... }
I already tested it in my branch and changed everything accordingly. Everything still works normally, so the new Login class looks like this:
public class Login
{
public string username { get; set; }
public string password { get; set; }
public bool loggedIn { get; set; }
}
Is there any specific reason one would want to use enum over bool in this instance? Performance, readability, ease of future expansions, or any other overhead my young mind may not be seeing?
enum
if there can ever be more than two states, or they are not a yes/no-style of state, otherwise use abool
. (I.e.,Up
/Down
would be a candidate for anenum
,LoggedIn
andLoggedOut
probably not.) \$\endgroup\$