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

Andela - Clean Code

Avatar for Jeremy Appel Jeremy Appel
August 20, 2020
130

Andela - Clean Code

Avatar for Jeremy Appel

Jeremy Appel

August 20, 2020
Tweet

Transcript

  1. Who am I? • 20+ years of software engineering experience

    with B.S. in Computer Science and MBA from Ohio State University
  2. Who am I? • 20+ years of software engineering experience

    with B.S. in Computer Science and MBA from Ohio State University • Worked in the Telecommunications, E- Commerce, and Financial industries
  3. Who am I? • 20+ years of software engineering experience

    with B.S. in Computer Science and MBA from Ohio State University • Worked in the Telecommunications, E- Commerce, and Financial industries • Experience with distributed systems, cloud native, and mobile technologies
  4. What is Clean Code & Architecture • In all seriousness,

    I like to think about Clean code being concerned with the micro e.g. readability, naming conventions, comments
  5. What is Clean Code & Architecture • In all seriousness,

    I like to think about Clean code being concerned with the micro e.g. readability, naming conventions, comments • While Clean Architecture is concerned with the macro, the relative composition of components and classes within a software system
  6. What is Clean Code & Architecture • In all seriousness,

    I like to think about Clean code being concerned with the micro e.g. readability, naming conventions, comments • While Clean Architecture is concerned with the macro, the relative composition of components and classes within a software system • If only there was someone we could look to for an answer…
  7. Good old “Uncle Bob” • Robert C. Martin, author of

    Clean Code and Clean Architecture • Founder of the Agile Manifesto • Advocate for Test-Driven Development • What does he think?
  8. What does Uncle Bob say? • Well he does the

    same thing as me. He asks others.. • “Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent, but is full of crisp abstractions and straightforward lines of control” - Grady Booch
  9. What does Uncle Bob say? Cont’d • “You know you

    are working on clean code when each routine pretty much turns out to be what you expected. You can call it beautiful code when the code also make it look like the language was made for the problem.” - Ward Cunningham
  10. What do I say? • I agree with both of

    these statements because they emphasize intent, clarity, and simplicity.
  11. What do I say? • I agree with both of

    these statements because they emphasize intent, clarity, and simplicity. • We don’t program by ourselves: we are part of a team and others will work on your code as much as you do
  12. What do I say? • I agree with both of

    these statements because they emphasize intent, clarity, and simplicity. • We don’t program by ourselves: we are part of a team and others will work on your code as much as you do • “Uncle Bob” mentions the “Boy Scout Rule” - leave things better than you found them
  13. Let’s Dive into the Details • Let’s look “through the

    lens of the microscope” to analyze discrete practices for producing and maintaining clean code
  14. Meaningful Names • Conveys intent • Can be easily searched

    from within an IDE • Can be easily pronounced
  15. Meaningful Names • Conveys intent • Can be easily searched

    from within an IDE • Can be easily pronounced • Domain and Context aware
  16. Comments • Intent, clarity, and simplicity rule the day here

    too • Comments don’t absolve bad code
  17. Comments • Intent, clarity, and simplicity rule the day here

    too • Comments don’t absolve bad code • Comments explaining non-intuitive code for performance reasons are a reasonable example
  18. Code Style/Formatting • Very subjective, but pick a code style

    and stick with it • Reading inconsistently indented and formatted code is a mental drain
  19. Code Style/Formatting • Very subjective, but pick a code style

    and stick with it • Reading inconsistently indented and formatted code is a mental drain • Invest the time in setting up a code style template for your IDE of choice and then distribute to the team via source control and use tools like Checkstyle to enforce it
  20. Classes • Small • Do only one thing and only

    one reason to change i.e. Single Responsibility Principle
  21. Classes • Small • Do only one thing and only

    one reason to change i.e. Single Responsibility Principle • A useful heuristic that I use is to describe what a class does in a sentence. If I use And/Or, then the class does too much
  22. Classes cont’d • Low Coupling and High Cohesion are critical

    qualities for classes when it comes to organizing a system as a whole
  23. Classes cont’d • Low Coupling and High Cohesion are critical

    qualities for classes when it comes to organizing a system as a whole • Depend on things that change the least i.e. abstractions
  24. Testing • So far we haven’t distinguished between new and

    legacy code • How do we transform bad code into clean code? Think “Boy Scout Rule”
  25. Testing • So far we haven’t distinguished between new and

    legacy code • How do we transform bad code into clean code? Think “Boy Scout Rule” • The only surefire way to transform bad code is to have tests that verify the behavior and invariants
  26. Testing • So far we haven’t distinguished between new and

    legacy code • How do we transform bad code into clean code? Think “Boy Scout Rule” • The only surefire way to transform bad code is to have tests that verify the behavior and invariants • By having tests in place, we can employ stepwise refinement to improve the code
  27. Clean Architecture • Now that we have focused on the

    micro and what practices we should follow to ensure that the low-level constructs are well- made, let’s step back and take a look at the bigger picture to ensure that they are assembled in a consistent, reinforcing fashion
  28. Clean Architecture • Now that we have focused on the

    micro and what practices we should follow to ensure that the low-level constructs are well- made, let’s step back and take a look at the bigger picture to ensure that they are assembled in a consistent, reinforcing fashion • Fundamentally an architecture must support:
  29. Clean Architecture • Now that we have focused on the

    micro and what practices we should follow to ensure that the low-level constructs are well- made, let’s step back and take a look at the bigger picture to ensure that they are assembled in a consistent, reinforcing fashion • Fundamentally an architecture must support: • The use cases and operation of the system
  30. Clean Architecture • Now that we have focused on the

    micro and what practices we should follow to ensure that the low-level constructs are well- made, let’s step back and take a look at the bigger picture to ensure that they are assembled in a consistent, reinforcing fashion • Fundamentally an architecture must support: • The use cases and operation of the system • The maintenance of the system
  31. Clean Architecture • Now that we have focused on the

    micro and what practices we should follow to ensure that the low-level constructs are well- made, let’s step back and take a look at the bigger picture to ensure that they are assembled in a consistent, reinforcing fashion • Fundamentally an architecture must support: • The use cases and operation of the system • The maintenance of the system • The development of the system
  32. Clean Architecture • Now that we have focused on the

    micro and what practices we should follow to ensure that the low-level constructs are well- made, let’s step back and take a look at the bigger picture to ensure that they are assembled in a consistent, reinforcing fashion • Fundamentally an architecture must support: • The use cases and operation of the system • The maintenance of the system • The development of the system • The deployment of the system
  33. Another quote • “Architecture represents the significant design decisions that

    shape a system, where significant is measured by the costs of change.” - Grady Booch
  34. The Best of Both Worlds • What I like about

    Grady’s quote is how he alludes to the goal of minimizing the cost of change
  35. The Best of Both Worlds • What I like about

    Grady’s quote is how he alludes to the goal of minimizing the cost of change • Our job isn’t only to build software that meets the stated requirements, but to build it in such a way that it minimizes the effort and resources to maintain/change it in the future i.e. Behavior vs. Structure
  36. The Best of Both Worlds • What I like about

    Grady’s quote is how he alludes to the goal of minimizing the cost of change • Our job isn’t only to build software that meets the stated requirements, but to build it in such a way that it minimizes the effort and resources to maintain/change it in the future i.e. Behavior vs. Structure • How do we achieve this?
  37. Best Practices and Patterns • There is a plethora of

    resources out there to help you become better software architects, too many to possibly mention here, and so we will touch on those that I feel have provided the most value to me
  38. SOLID • An acronym for five separate OO design principles

    that are widely established and practiced
  39. SOLID • An acronym for five separate OO design principles

    that are widely established and practiced • “…SOLID principles tell us how to arrange the bricks into walls and rooms…” - Uncle Bob
  40. SOLID • An acronym for five separate OO design principles

    that are widely established and practiced • “…SOLID principles tell us how to arrange the bricks into walls and rooms…” - Uncle Bob • Reminds us that all good software is layered and relies on composition, something we will revisit shortly.
  41. SOLID cont’d • Single responsibility principle - change for only

    one reason • Open/Closed principle - classes open for extension, closed for modification
  42. SOLID cont’d • Single responsibility principle - change for only

    one reason • Open/Closed principle - classes open for extension, closed for modification • Liskov Substitution principle - subtypes should be substitutable without altering correctness
  43. SOLID cont’d • Single responsibility principle - change for only

    one reason • Open/Closed principle - classes open for extension, closed for modification • Liskov Substitution principle - subtypes should be substitutable without altering correctness • Interface Segregation Principle - prefer small abstractions over larger ones
  44. SOLID cont’d • Single responsibility principle - change for only

    one reason • Open/Closed principle - classes open for extension, closed for modification • Liskov Substitution principle - subtypes should be substitutable without altering correctness • Interface Segregation Principle - prefer small abstractions over larger ones • Dependency Inversion Principle - depend only on abstractions
  45. Components • Next higher level of composition • A unit

    of deployment like jar, dll, or an assembly in .NET
  46. Components • Next higher level of composition • A unit

    of deployment like jar, dll, or an assembly in .NET • “..component principles tell us how to arrange rooms into buildings..” - Uncle Bob
  47. Components cont’d • Common Closure Principle - combine those classes

    that change for the same reason and at the same interval
  48. Components cont’d • Common Closure Principle - combine those classes

    that change for the same reason and at the same interval • Common Reuse Principle - don’t depend on things that you don’t use
  49. Components cont’d • Common Closure Principle - combine those classes

    that change for the same reason and at the same interval • Common Reuse Principle - don’t depend on things that you don’t use • Component Dependency Principles
  50. Components cont’d • Common Closure Principle - combine those classes

    that change for the same reason and at the same interval • Common Reuse Principle - don’t depend on things that you don’t use • Component Dependency Principles • Avoid dependency cycles at all costs
  51. Components cont’d • Common Closure Principle - combine those classes

    that change for the same reason and at the same interval • Common Reuse Principle - don’t depend on things that you don’t use • Component Dependency Principles • Avoid dependency cycles at all costs • Depend on abstractions
  52. Architectural Decisions • Assess and provide means by which to

    decouple from the various dimensions that are involved: development, deployment, and operation
  53. Architectural Decisions • Assess and provide means by which to

    decouple from the various dimensions that are involved: development, deployment, and operation • Like most software development/architecture, it is more art than science
  54. Architectural Decisions • Assess and provide means by which to

    decouple from the various dimensions that are involved: development, deployment, and operation • Like most software development/architecture, it is more art than science • It boils down to how to balance/delay the decision making process as much as possible for those items which aren’t essential
  55. Architectural Decisions • Assess and provide means by which to

    decouple from the various dimensions that are involved: development, deployment, and operation • Like most software development/architecture, it is more art than science • It boils down to how to balance/delay the decision making process as much as possible for those items which aren’t essential • At the end of the day, a proposed architecture should reveal the use cases of the system it supports
  56. Conclusion • Like most things, the concepts and practices we

    have discussed take time to learn • At first you will probably struggle how to balance the options that you have against your requirements.
  57. Conclusion • Like most things, the concepts and practices we

    have discussed take time to learn • At first you will probably struggle how to balance the options that you have against your requirements. • Over time you will gain a feel for what decisions are the most important vs. those that can be deferred.
  58. Conclusion • Like most things, the concepts and practices we

    have discussed take time to learn • At first you will probably struggle how to balance the options that you have against your requirements. • Over time you will gain a feel for what decisions are the most important vs. those that can be deferred. • Most importantly remember that it is incumbent on a software architect to be vocal with respect to the Structure of a system vs. the Behavior.