My understanding for IoC(Inversion of Control)

20230322 free chat sharing notes

Framework vs Library

  • our code calls library code
  • our code called by framework code

My understanding for IoC

IoC is we as programmers write codes that are called by Frameworks

  • control structures: Sequence, Selection & Iteration
  • normally we write business logic as step1, step2, step3… using above control structures
  • but we will notice bolier templates as we getting more and more familar with them, for examples:
    • we always need to close an open file
    • we always need to declare an empty array if we’re only interested in part of the elements in a list
    • some code only differs in the middle part

Examples

  • map/filter/reduce(pipelined list handling)
    • list.filter {}.map {}
    • list.filter {}.map {}.reduce {}
  • ruby: partition/all?/any?/none?
    • (1..6).partition { |v| v.even? } #=> [[2, 4, 6], [1, 3, 5]]
  • dsl(describe what to do, not how to do it)
  • framework
    • rails
    • event_sourcing
  • lambda(ruby block / First-class Function)

Take aways

  • find & use good abstraction(e.g.: use map/filter/reduce more)
  • learn more about the framework we’re using

References