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

Software Design & Architecture on my mind

WeRockStar
December 11, 2020

Software Design & Architecture on my mind

This is my slide for sharing knowledge in our company

WeRockStar

December 11, 2020
Tweet

More Decks by WeRockStar

Other Decks in Programming

Transcript

  1. @werockstar Everything in software architecture is a trade-off Fundamentals of

    Software Architecture, Neal Ford and Mark Richards
  2. What I want to share to you all? - What

    is design/architecture? - Classical/Essential OOP? - Coupling and Cohesion - Many Principles - Testing - Design Pattern - Legacy Code - Code Smell - What are missing? @werockstar
  3. @werockstar Architecture • Module, connections, dependencies and interface • The

    big picture • Design with bigger picture in mind • Interface rather than implementation • A conceptual model • A plan • A blueprint • System, Subsystem, interactions and interfaces • Mindset • The system as a whole • Everything, Everything, and Everything Source: Software Architecture for Developers Vol.1
  4. @werockstar The structure of a software product. This includes elements,

    the externally visibility properties of the elements, and the relationships between the elements Bass et al. 2012
  5. @werockstar The software architecture of a system is the set

    of structures needed to reason about the system, which comprise software elements, relations among them, and properties of both. Software Architecture in Practice, 3rd Edition
  6. @werockstar Uncle Bob, Author of Clean Architecture, Clean Code, etc.

    The low-level details and the high-level structure are all part of the same whole. You can’t have one without the other
  7. @werockstar Uncle Bob, Author of Clean Architecture, Clean Code, etc.

    The ultimate goal is to minimize the lifetime cost of the system and to maximize programmer productivity.
  8. @werockstar I think, I don’t know about an OOP at

    all https://unsplash.com/photos/vYyTAjJuuL4
  9. @werockstar Alan Kay OOP to me means only messaging, local

    retention and protection and hiding of state- process, and extreme late-binding of all things
  10. @werockstar Alan Kay I’m sorry that I long ago coined

    the term ‘object’ for this topic because it gets many people to focus on the lesser idea. The big idea is messaging
  11. @werockstar Message Passing • Communication mechanism between objects by passing

    message • Decoupling objects from each other • Message Passing involves name of object, name of method(message) and information to be send • Care about contract, don’t care about implementation https://unsplash.com/photos/fN603qcEA7g
  12. @werockstar Local retention, … and hiding of state-process • Locally

    within objects • Encapsulating state • State changes are controlled at a local • Not command • No direct referencing of internal state https://unsplash.com/photos/DRzYMtae-vA
  13. @werockstar Extreme late-binding • Runtime adaptability • Looked up implementation

    at runtime • Dynamic binding • Polymorphism? • Flexibility? https://unsplash.com/photos/P9zwFXDtDfs
  14. @werockstar Adaptive Code 2nd Edition, Gary McLean Hall Coupling is

    the strength of interdependence between software elements
  15. @werockstar Software Elements • Property/State/Attribute • Method/Function • Class •

    Package/Group • Namespace/Module • Solution https://unsplash.com/photos/9cCeS9Sg6nU
  16. Coupling forced by framework (Implicit coupling) UI Interact with UI

    Source: Thinking in Compose Different Language
  17. @werockstar Adaptive Code 2nd Edition, Gary McLean Hall Cohesion is

    the strength of contextual relationship between software elements
  18. @werockstar In another word, Cohesion talks to how we related

    code The code that changes together, stays together
  19. Cohesion in Module (Bigger picture) A valid relationship between classes

    occurs when: • Same layer of application architecture • Same bounded context of the application Adaptive Code 2nd Edition, Gary McLean Hall
  20. Same layer of application architecture Classes that relate to the

    same layer can live together in the same module, whereas classes that relate to different layers must be separated by at least a module boundary Adaptive Code 2nd Edition, Gary McLean Hall User Interface Data Access Business Logic
  21. Same bounded context of the application Classes that relate to

    the same bounded context can live together in the same module, whereas classes that relate to different bounded contexts must be separated by at least a module boundary. Adaptive Code 2nd Edition, Gary McLean Hall
  22. @werockstar Cohesion in Microservices • To guarantee independent deployability, we

    need to ensure our services are loosely coupled. • We need to be able to change one service without having to change anythings else. https://unsplash.com/photos/9cCeS9Sg6nU Source: Monolith to Microservices, Sam Newman
  23. @werockstar Larry Constantine Attempting to divide a cohesion module would

    only result increased coupling and decreased readability
  24. @werockstar SOLID • SRP - Single Responsibility • OCP -

    Open-closed • LSP - Liskov substitution • ISP - Interface segregation • DIP - Dependency Inversion https://unsplash.com/photos/9cCeS9Sg6nU
  25. @werockstar Clean Architecture book, Uncle Bob Historically, A module should

    have one, and only one, reason to change Single Responsibility Principle
  26. @werockstar A module should be responsible to one, and only

    one, actor Again, Single Responsibility Principle
  27. @werockstar Software system changed to satisfy users and stakeholders; those

    users and stakeholders are the “reason to change” that the principle is talking about Responsible to one actor (User point of view)
  28. @werockstar Developers have a tendency to attempt to solve specific

    problems with general solutions This leads to coupling and complexity at first - Greg Young
  29. @werockstar Example: ODDS worklogs Employee - Role - You -

    Calculate salary - P’Roof - 50 ทวิ & Report & สำเนาบัตร - Accounting
  30. @werockstar Agile Principles, Patterns, and Patterns in C#, Uncle Bob

    Software entities (class, module, function, etc.) should be open for extension but closed for modification Open-closed Principle
  31. @werockstar If OCP is applied well, further changes of that

    kind are achieved by adding new, not by changing old that already works
  32. @werockstar Agile Principles, Patterns, and Patterns in C#, Uncle Bob

    Subtypes must be substitutable for their base types Liskov Substitution Principle
  33. @werockstar Real World Software Development, Raoul-Gabriel Urma and Richard Warburton

    In other words, extend a class or implement an interface should maintain the behavior they inherit from their parents Liskov Substitution Principle
  34. @werockstar Rules? • Preconditions can’t be strengthened in subtype •

    Postconditions can’t be weakened in a subtype • Invariant of supertype must be preserved in a subtype • Shouldn’t allow state change that your super type disallowed https://unsplash.com/photos/pwpVGQ-A5qI
  35. @werockstar Agile Principles, Patterns, and Patterns in C#, Uncle Bob

    Clients should not be forced to depend on methods they do not use Interface Segregation Principle
  36. @werockstar - Write interfaces with the client code’s requirements as

    a primary concern. - Create smaller interfaces with more directed purposes. - Identify scenarios where interface segregation can be used.
  37. @werockstar We well know about many kind of sort algorithms

    At high level of design, we can treat all of these equally because of what they have in common: They all sort
  38. @werockstar Variability of sort algorithms At level of design, we

    can specific all of these to be different kind of sorting
  39. @werockstar Summary 1. Identify commonalities first 2. Create abstraction from

    these 3. Identify derivations with the variations of the commonalities 4. See how the commonalities related to each other Source: Design Patterns Explained, 2nd Edition
  40. @werockstar Mark Seemann Having only one implementation of a given

    interface is a code smell Reused Abstraction Principle
  41. @werockstar Agile Principles, Patterns, and Patterns in C#, Uncle Bob

    - High-level modules should not depend on low-level modules. Bot should depend on abstractions. - Abstractions should not depend upon details. Details should depend upon abstractions Dependency Inversion Principle
  42. @werockstar Agile Principles, Patterns, and Patterns in C#, Uncle Bob

    - High-level modules should not depend on low-level modules. Bot should depend on abstractions. - Abstractions should not depend upon details. Details should depend upon abstractions Dependency Inversion Principle class or object
  43. @werockstar Clean Architecture • Independent of Frameworks • Testable •

    Independent of UI • Independent of Database • Independent of any external agency https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
  44. @werockstar Dependency Rule Nothing in an inner circle can know

    anything at all about something in an outer circle The name of something declared in an outer circle must not be mentioned by the code in an inner circle
  45. @werockstar Essential vs Detail • Space is essential • Usability

    is essential • Building material is a detail • Brick is a detail
  46. @werockstar What about details? Web Database Frameworks React Vue Angular

    Android iOS PlayStation 5 ยืนเฉย ๆ เขาก็ไม่รัก
  47. @werockstar Generically, a dependency is a relationship between two distinct

    entities whereby one cannot perform some function—or perhaps exist—without the other
  48. @werockstar Testing • Definition Unit of Work • Good Unit

    Test • Test Smell, Good design and Testability • Test Coupling between production code? • Don’t Test me, please. Why? • New Line Pattern • More Specific • Strict with behaviors, not structure of elements • Test Double • London school and Classical https://unsplash.com/photos/ilSnKT1IMxE
  49. Definition of Unit Test A unit test is a piece

    of code that invokes a unit of work and checks one specific end result of that unit of work The Art of Unit Testing, 2nd Edition
  50. Overview - AAA - Arrange, Act, Assert - SUT -

    System Under Test, Subject Under Test - Given-When-Then
  51. Good Unit Test - They run fast - They help

    us localize problem - Deterministic
  52. Design Smell to Test Smell - Difficult Setup - State

    leaks across tests - Leak implementation detail - Coupling with Test only - Difficult to replace with Test Double? - Non-deterministic - God Class with God Test
  53. God Class with God Test Account AccountTest - Where is

    withdraw? - Failure with many reason
  54. As the tests get more specific, the production code get

    more generic - DRY - Don’t Repeat Yourself, Good? - Naming
  55. @werockstar Everything in software architecture is a trade-off Fundamentals of

    Software Architecture, Neal Ford and Mark Richards
  56. @werockstar It must remain concrete enough to be understood while

    simultaneously begin abstract enough to allow for change Sandi Metz