Skip to content

A sketch implementing a simple password checker without an if statement

Notifications You must be signed in to change notification settings

StorminGorman/ZeroIfExperiment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Zero If Statement experiment

Woke up this morning with a desire to see if I can do away with if statements altogether. I decided to implement a quick and dirty login system in swift that just checks to see whether the supplied password matches the stored one and returns a response accordingly.

The implementation boiled down to these two differences..

Using a map/dictionary instead of an If

let potentialActions: [Bool:Action] = [
    true : LoggedInAction(cred: cred),
    false : InvalidPasswordAction()
]
let auth = UserAuthentication(username: username, password: password, credentials: creds)
let action = potentialActions[auth.passwordMatches()]!
print(action.response())

and the normal way

let auth = UserAuthentication(username: username, password: password, credentials: creds)

var action: Action
if(auth.passwordMatches()) {
    action = LoggedInAction(cred: cred)
} else {
    action = InvalidPasswordAction()
}

print(action.response())

About

A sketch implementing a simple password checker without an if statement

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages