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

Fight Complexity with Functional Programming in Kotlin

Fight Complexity with Functional Programming in Kotlin

A Metric-driven approach to reduce Cognitive Complexity in a code base, using Functional Programming, demoed hands-on, by solving a complex real-world ubiquitous design challenge - REST API Bulk Request Validation, with an extensible Framework that separates what-to-do (Validations) from how-to-do (Validation Orchestration). Let’s do a case study of a successful implementation done by our team in the world’s largest SaaS org, Salesforce, through our in-house baked FOSS library Vader.

Gopal S Akshintala

April 16, 2020
Tweet

More Decks by Gopal S Akshintala

Other Decks in Programming

Transcript

  1. overfullstack.github.io

    View Slide

  2. FP is cool , coz
    it’s complex enough to
    make devs feel like
    they are doing
    something smart!
    @GopalAkshintala
    - Some Manager (Not Mine!)

    View Slide

  3. @GopalAkshintala

    View Slide

  4. Dev
    Holy
    Wars
    @GopalAkshintala

    View Slide

  5. IDE Theme
    Light
    Vs
    Dark
    @GopalAkshintala

    View Slide

  6. Eclipse
    Vs
    Intellij
    @GopalAkshintala

    View Slide

  7. OOP Vs FP ?
    @GopalAkshintala

    View Slide

  8. @GopalAkshintala
    Immutability
    Referential Transparency
    Side-effects @ Boundaries
    FP Principles
    …………………………………………………
    …………………………………………………
    OOP Principles

    View Slide

  9. Imperative
    Vs
    Declarative
    The Jotlin
    Dev
    @GopalAkshintala

    View Slide

  10. Imperative
    Code
    -
    -
    -
    Imperative
    @GopalAkshintala

    View Slide

  11. Let’s Concatenate
    last names in a
    team with a
    delimiter
    lastName1 lastName2 lastName2
    @GopalAkshintala
    Team Building!

    View Slide

  12. @GopalAkshintala
    What about
    Corner cases?

    View Slide

  13. @GopalAkshintala
    lastName1 lastName2 lastName2

    View Slide

  14. @GopalAkshintala

    View Slide

  15. @GopalAkshintala
    Imperative Declarative

    View Slide

  16. @GopalAkshintala
    Imperative Declarative

    View Slide

  17. @GopalAkshintala
    Imperative Declarative

    View Slide

  18. @GopalAkshintala
    Imperative Declarative

    View Slide

  19. @GopalAkshintala
    Imperative Declarative

    View Slide

  20. Declarative
    Imperative
    Code
    -
    -
    -
    Imperative
    Vs
    Declarative
    @GopalAkshintala

    View Slide

  21. The Java
    @GopalAkshintala

    View Slide

  22. But is FP just about
    Style?

    View Slide

  23. How to Concatenate
    Last names in
    Parallel?
    @GopalAkshintala

    View Slide

  24. @GopalAkshintala
    How to make this Parallel?

    View Slide

  25. How-to-dos What-to-dos
    Looping through
    the list
    Aggregating the
    results
    Validating the
    names
    Extracting last
    names
    @GopalAkshintala

    View Slide

  26. @GopalAkshintala
    Serial Parallel

    View Slide

  27. The
    Context:Core
    Philosophy
    Do
    Things differently
    without
    doing
    Different things!
    @GopalAkshintala

    View Slide

  28. Sequence
    Busted
    @GopalAkshintala

    View Slide

  29. @GopalAkshintala
    Reading about
    Functors, Monads, Applicatives!

    View Slide

  30. Either Monad
    Egg
    Validation Failure
    Context
    @GopalAkshintala
    Left Right

    View Slide

  31. Source @GopalAkshintala

    View Slide

  32. Source @GopalAkshintala

    View Slide

  33. Source @GopalAkshintala
    Left State
    Stays Left

    View Slide

  34. Source @GopalAkshintala
    f(g(h(x)))

    View Slide

  35. Are Monads used in
    Enterprise
    Software?
    @GopalAkshintala

    View Slide

  36. The
    Vader
    @GopalAkshintala

    View Slide

  37. REST
    APIs
    @GopalAkshintala

    View Slide

  38. REST
    Payload
    @GopalAkshintala

    View Slide

  39. @GopalAkshintala

    View Slide

  40. The
    Batch
    Validation
    Framework
    @GopalAkshintala

    View Slide

  41. @GopalAkshintala

    View Slide

  42. Framework
    Requirements
    @GopalAkshintala

    View Slide

  43. @GopalAkshintala
    Validation layer
    Service layer
    DB layer
    Aggregated Response

    View Slide

  44. Framework
    Meta
    Requirements
    @GopalAkshintala

    View Slide

  45. @GopalAkshintala

    View Slide

  46. One Egg
    One Validation
    Many Eggs
    One Validation
    @GopalAkshintala

    View Slide

  47. Many Eggs
    Many Validations
    !
    @GopalAkshintala

    View Slide

  48. Chaos
    @GopalAkshintala

    View Slide

  49. Chain of Responsibility
    @GopalAkshintala
    Is FP the Best Fit?

    View Slide

  50. @GopalAkshintala
    2D Problem
    Eggs
    Validations

    View Slide

  51. Making of a
    Extensible Machine
    @GopalAkshintala
    Validations Configuration Orchestration
    (What-to-do) (How-to-do) (How-to-do)

    View Slide

  52. Immutable
    Eggs
    @GopalAkshintala

    View Slide

  53. Heterogeneous
    DataTypes in pipeline
    @GopalAkshintala

    View Slide

  54. Uniform DataTypes
    with Monad Boxes
    @GopalAkshintala

    View Slide

  55. Standardize
    Validations
    @GopalAkshintala

    View Slide

  56. @GopalAkshintala
    val validate1Simple: Validator = {
    if (simpleRule(it)) true.right() else NO_EGG_TO_VALIDATE_1.left()
    }
    validate1Simple
    fun validate1(
    badEggFailureBucketMap: MutableMap,
    eggIndex: Int,
    iterator: MutableIterator,
    eggToBeValidated: Egg?
    ): Boolean {
    if (!simpleRule(eggToBeValidated)) {
    iterator.remove()
    badEggFailureBucketMap[eggIndex] = ValidationFailures.NO_EGG_TO_VALIDATE_1
    return false
    }
    return true
    }

    View Slide

  57. @GopalAkshintala
    Validator
    typealias Validator
    = suspend (ValidatableT) -> Either

    View Slide

  58. Framework
    How-to-dos
    @GopalAkshintala

    View Slide

  59. @GopalAkshintala
    Lift

    View Slide

  60. @GopalAkshintala
    Lift

    View Slide

  61. Framework
    How-to-dos
    @GopalAkshintala

    View Slide

  62. Transformation
    map
    flatMap
    filter
    take…
    Aggregation
    reduce
    groupBy
    zip,unzip
    fold,foldRight
    scan,runningReduce
    @GopalAkshintala
    Dozen FP Operations for
    Daily Programming

    View Slide

  63. Framework
    How-to-dos
    @GopalAkshintala

    View Slide

  64. What about
    Complexity?
    @GopalAkshintala

    View Slide

  65. FP is NOT Complex
    It fights
    Cognitive-Complexity
    @GopalAkshintala

    View Slide

  66. Min.
    Cognitive-Complexity
    Same
    Time-Complexity
    @GopalAkshintala

    View Slide

  67. Railway-Oriented
    Programming
    @GopalAkshintala

    View Slide

  68. @GopalAkshintala
    bit.ly/imp-vs-dec
    bit.ly/ro-validation-kt
    bit.ly/fcwfp-kt-slides
    { }

    View Slide

  69. @GopalAkshintala

    View Slide

  70. @GopalAkshintala
    [email protected]

    View Slide

  71. @GopalAkshintala

    View Slide