patterncsharpMinor
Login functionality with enum or bool
Viewed 0 times
withenumboolloginfunctionality
Problem
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,
I'm thinking it would be better to just use a
In my opinion, it is slower and more confusing to read:
It would seem a lot better to just write:
or
I already tested it in my branch and changed everything accordingly. Everything still works normally, so the new Login class looks like this:
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?
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?
Solution
I find enums are ofte easier to use then booleans. At some point you probably will want to search for users so consider this:
and its usage
vs
and its usage
but even with the enum you could do
by adding an implicit cast to the
User FindUserByStatus(LoginStatus status)and its usage
userSearch.FindUserByStatus(LoginStatus.LoggedIn)vs
User FindUserByStatus(bool loginStatus)and its usage
userSearch.FindUserByStatus(true);but even with the enum you could do
if (login) { .. }by adding an implicit cast to the
Loginpublic static implicit operator bool(Login login) => login.Status == LoginStatus.LoggedIn;Code Snippets
User FindUserByStatus(LoginStatus status)userSearch.FindUserByStatus(LoginStatus.LoggedIn)User FindUserByStatus(bool loginStatus)userSearch.FindUserByStatus(true);if (login) { .. }Context
StackExchange Code Review Q#160688, answer score: 2
Revisions (0)
No revisions yet.