Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SwiftLille Meetup #4 - @kamidude - Bool Tricks

SwiftLille Meetup #4 - @kamidude - Bool Tricks

Booleans - Logic tips and tricks for a better code : Comment utiliser les booleens pour simplifier votre code, le rendre plus lisible et plus robuste ? Comment les reconnaitre, les recombiner et aussi ne pas s’en servir quand il ne faut pas.
🎤Présenté par Jérémie Girault, Founder & CTO @ Hubvisor

Swift Lille

November 19, 2019
Tweet

More Decks by Swift Lille

Other Decks in Programming

Transcript

  1. Basics 〉Priorities let E = !A || B && C

    || D let E = ((!A) || (B && C)) || D @kamidude
  2. Basics 〉ShortCircuits func doThis () -> Bool { print("doThis") return

    false } func doThat () -> Bool { print("doThat") return false } let x = doThis() && doThat() print(x) @kamidude
  3. public static func && (lhs: Bool, rhs: @autoclosure () throws

    -> Bool) rethrows -> Bool
 public static func || (lhs: Bool, rhs: @autoclosure () throws -> Bool) rethrows -> Bool Basics 〉ShortCircuits func doThis () -> Bool { print("doThis") return false } func doThat () -> Bool { print("doThat") return false } let x = doThis() && doThat() print(x) @kamidude
  4. Basics 〉Extract names func canEnterRollercoaster(age: UInt, size: UInt) -> Bool

    { age >= 15 && size > 150 } func canEnterRollercoaster(age: UInt, size: UInt) -> Bool { let isTallEnough = size > 150 let isOldEnough = age >= 15 return isTallEnough && isOldEnough } @kamidude
  5. Basics 〉Logic Semantic Only @kamidude struct AppleCard { let isMan:

    Bool func getCreditScore () -> Double { isMan ? 100 : 50 } }
  6. Basics 〉Logic Semantic Only @kamidude struct AppleCard { let isMan:

    Bool func getCreditScore (useNewAlgorithm: Bool) -> Double { let initialValue: Double = isMan ? 100 : 50 return useNewAlgorithm ? random * initialValue : initialValue } }
  7. Basics 〉Logic Semantic Only @kamidude struct AppleCard { let isMan:

    Bool func getCreditScore (algorithm: Algorithm) -> Double { let initialValue: Double = isMan ? 100 : 50 switch (algorithm) { case .old: return random * initialValue case .new: return initialValue } } }
  8. Basics 〉Linearize code @kamidude func performActionIfAllowed (action: () -> Void)

    { if (isLoggedIn) { if (hasAccessRights) { action() } else { showError("User has not access rights !") } } else { showError("User is not logged in !") } }
  9. Basics 〉Linearize code @kamidude func performActionIfAllowed (action: () -> Void)

    { if (isLoggedIn) { if (hasAccessRights) { action() } else { showError("User has not access rights !") } } else { showError("User is not logged in !") } } func performActionIfAllowed (action: () -> Void) { guard isLoggedIn else { return showError("User is not logged in !") } guard hasAccessRights else { return showError("User has not access rights !") } action() }
  10. Advanced 〉Operators Symbol Name Swift 㱻 Equivalence ¬ Négation !

    ∧ Conjonction && ∨ Disjonction || @kamidude
  11. Advanced 〉Operators Symbol Name Swift 㱻 Equivalence == ¬ Négation

    ! ∧ Conjonction && ∨ Disjonction || @kamidude
  12. Advanced 〉Operators Symbol Name Swift 㱻 Equivalence == ¬ Négation

    ! ∧ Conjonction && ∨ Disjonction || ⊻ Disjonction exclusive @kamidude
  13. Advanced 〉Operators Symbol Name Swift 㱻 Equivalence == ¬ Négation

    ! ∧ Conjonction && ∨ Disjonction || ⊻ Disjonction exclusive != @kamidude
  14. Advanced 〉Operators Symbol Name Swift 㱺 Implication 㱻 Equivalence ==

    ¬ Négation ! ∧ Conjonction && ∨ Disjonction || ⊻ Disjonction exclusive != @kamidude
  15. Advanced 〉Implication A B A㱺B 0 0 1 0 1

    1 1 0 0 1 1 1 A 㱺 B 㱻 ¬B 㱺 ¬A @kamidude
  16. Advanced 〉Implication A B A㱺B 0 0 1 0 1

    1 1 0 0 1 1 1 A 㱺 B 㱻 ¬A ∨ B 㱻 ¬B 㱺 ¬A @kamidude
  17. Advanced 〉Implication extension Bool { func implies (_ other: @autoclosure

    () -> Bool) -> Bool { return !self || other() } } @kamidude
  18. struct User { let age: UInt let hasParentsAuthorization: Bool let

    hasActiveSubscription: Bool } Advanced 〉Implication @kamidude
  19. struct User { let age: UInt let hasParentsAuthorization: Bool let

    hasActiveSubscription: Bool } func isAdmissible (user: User) -> Bool { if (user.hasActiveSubscription) { if (user.age < 18) { return user.hasParentsAuthorization } else { return true } } else { return false } } Advanced 〉Implication @kamidude
  20. struct User { let age: UInt let hasParentsAuthorization: Bool let

    hasActiveSubscription: Bool } func isAdmissible (user: User) -> Bool { if (user.hasActiveSubscription) { if (user.age < 18) { return user.hasParentsAuthorization } else { return true } } else { return false } } extension User { var isJuvenile: Bool { age < 18 } } Advanced 〉Implication @kamidude
  21. struct User { let age: UInt let hasParentsAuthorization: Bool let

    hasActiveSubscription: Bool } func isAdmissible (user: User) -> Bool { if (user.hasActiveSubscription) { if (user.age < 18) { return user.hasParentsAuthorization } else { return true } } else { return false } } extension User { var isJuvenile: Bool { age < 18 } } func isAdmissible (user: User) -> Bool { return user.hasActiveSubscription && user.isJuvenile.implies(user.hasParentsAuthorization) } Advanced 〉Implication @kamidude
  22. Advanced 〉Transformations Usage if (shouldDisplayData && (hasLocalData || isNetworkAvailable)) {

    // show results screen } let shouldDisplayLocalData = shouldDisplayData && hasLocalData let shouldDisplayRemoteData = shouldDisplayData && isNetworkAvailable if (shouldDisplayLocalData || shouldDisplayRemoteData) { // show results screen } @kamidude
  23. Advanced 〉De Morgan A B ¬(A∨B) ¬A∧¬B 0 0 1

    1 0 1 0 0 1 0 0 0 1 1 0 0 A B ¬(A∧B) ¬A∨¬B 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0 0 Proof @kamidude
  24. Advanced 〉Transformations (!(X1 || X2) || (X3 && X4)) &&

    (!X1 || X2) @kamidude 㱻 (!X1 || X3) && (!X1 || X4) && (!X2 || X3) && (!X2 || X4) && (!X1 || X2) FNC && FND
  25. Advanced 〉FNC && FND @kamidude func refundCustomerIfNeeded () { if

    ((!isEnterprise && !isCivil && hasFreePass) && (now < billSentTime + refundExpirationInterval) && (billHasBeenPosted || billHasBeenEmailed || billHasBeenGivenInHand) && hasBillBeenPaid && customerHasSentHandwrittenLetter) { refundNow() } else { showError("An Unknown error happened, please try again later") } } Use Case
  26. Advanced 〉FNC && FND @kamidude func refundCustomerIfNeeded () { let

    isCustomerBillable = isEnterprise || isCivil || !hasFreePass let hasCustomerBeenSentBill = billHasBeenPosted || billHasBeenEmailed || billHasBeenGivenInHand let refundPeriodExpired = now < billSentTime + refundExpirationInterval guard isCustomerBillable else { return showError("The customer did not need to pay anything !") } guard hasCustomerBeenSentBill else { return showError("The customer has not been sent a bill yet !") } guard !refundPeriodExpired else { return showError("The refund period has expired !") } guard hasBillBeenPaid else { return showError("The bill has not been paid, no refund can be done yet") } guard customerHasSentHandwrittenLetter else { return showError("The customer must send a handwritten letter") } refundNow() } func refundCustomerIfNeeded () { if ((!isEnterprise && !isCivil && hasFreePass) && (now < billSentTime + refundExpirationInterval) && (billHasBeenPosted || billHasBeenEmailed || billHasBeenGivenInHand) && hasBillBeenPaid && customerHasSentHandwrittenLetter) { refundNow() } else { showError("An Unknown error happened, please try again later") } } Use Case
  27. Recap @kamidude Linearize code Use Bools for logic Extract and

    name terms Transform and optimize expressions
  28. Recap @kamidude Distributivity Linearize code Use Bools for logic Extract

    and name terms Transform and optimize expressions
  29. Recap @kamidude Distributivity Linearize code Use Bools for logic Extract

    and name terms Transform and optimize expressions De Morgan
  30. Recap @kamidude Distributivity Linearize code Use Bools for logic Extract

    and name terms Transform and optimize expressions De Morgan FNC && FND
  31. Recap @kamidude Distributivity Linearize code Use Bools for logic Extract

    and name terms Transform and optimize expressions De Morgan FNC && FND Theory is fun, go read wikipedia