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

More Decks by Gopal S Akshintala

Other Decks in Programming


  1. overfullstack.github.io

  2. FP is cool , coz it’s complex enough to make

    devs feel like they are doing something smart! @GopalAkshintala - Some Manager (Not Mine!)
  3. @GopalAkshintala

  4. Dev Holy Wars @GopalAkshintala

  5. IDE Theme Light Vs Dark @GopalAkshintala

  6. Eclipse Vs Intellij @GopalAkshintala

  7. OOP Vs FP ? @GopalAkshintala

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

    ………………………………………………… OOP Principles
  9. Imperative Vs Declarative The Jotlin Dev @GopalAkshintala

  10. Imperative Code - - - Imperative @GopalAkshintala

  11. Let’s Concatenate last names in a team with a delimiter

    lastName1 lastName2 lastName2 @GopalAkshintala Team Building!
  12. @GopalAkshintala What about Corner cases?

  13. @GopalAkshintala lastName1 lastName2 lastName2

  14. @GopalAkshintala

  15. @GopalAkshintala Imperative Declarative

  16. @GopalAkshintala Imperative Declarative

  17. @GopalAkshintala Imperative Declarative

  18. @GopalAkshintala Imperative Declarative

  19. @GopalAkshintala Imperative Declarative

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

  21. The Java @GopalAkshintala

  22. But is FP just about Style?

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

  24. @GopalAkshintala How to make this Parallel?

  25. How-to-dos What-to-dos Looping through the list Aggregating the results Validating

    the names Extracting last names @GopalAkshintala
  26. @GopalAkshintala Serial Parallel

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

  28. Sequence Busted @GopalAkshintala

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

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

  31. Source @GopalAkshintala

  32. Source @GopalAkshintala

  33. Source @GopalAkshintala Left State Stays Left

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

  35. Are Monads used in Enterprise Software? @GopalAkshintala

  36. The Vader @GopalAkshintala

  37. REST APIs @GopalAkshintala

  38. REST Payload @GopalAkshintala

  39. @GopalAkshintala

  40. The Batch Validation Framework @GopalAkshintala

  41. @GopalAkshintala

  42. Framework Requirements @GopalAkshintala

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

  44. Framework Meta Requirements @GopalAkshintala

  45. @GopalAkshintala

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

  47. Many Eggs Many Validations ! @GopalAkshintala

  48. Chaos @GopalAkshintala

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

  50. @GopalAkshintala 2D Problem Eggs Validations

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

    (How-to-do) (How-to-do)
  52. Immutable Eggs @GopalAkshintala

  53. Heterogeneous DataTypes in pipeline @GopalAkshintala

  54. Uniform DataTypes with Monad Boxes @GopalAkshintala

  55. Standardize Validations @GopalAkshintala

  56. @GopalAkshintala val validate1Simple: Validator<Egg, ValidationFailure> = { if (simpleRule(it)) true.right()

    else NO_EGG_TO_VALIDATE_1.left() } validate1Simple fun validate1( badEggFailureBucketMap: MutableMap<Int, ValidationFailure>, eggIndex: Int, iterator: MutableIterator<Egg?>, eggToBeValidated: Egg? ): Boolean { if (!simpleRule(eggToBeValidated)) { iterator.remove() badEggFailureBucketMap[eggIndex] = ValidationFailures.NO_EGG_TO_VALIDATE_1 return false } return true }
  57. @GopalAkshintala Validator typealias Validator<ValidatableT, FailureT> = suspend (ValidatableT) -> Either<FailureT,

  58. Framework How-to-dos @GopalAkshintala

  59. @GopalAkshintala Lift

  60. @GopalAkshintala Lift

  61. Framework How-to-dos @GopalAkshintala

  62. Transformation map flatMap filter take… Aggregation reduce groupBy zip,unzip fold,foldRight

    scan,runningReduce @GopalAkshintala Dozen FP Operations for Daily Programming
  63. Framework How-to-dos @GopalAkshintala

  64. What about Complexity? @GopalAkshintala

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

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

  67. Railway-Oriented Programming @GopalAkshintala

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

  69. @GopalAkshintala

  70. @GopalAkshintala [email protected]

  71. @GopalAkshintala