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

Hands on Clean Code

Yoan
February 01, 2017

Hands on Clean Code

Workshop designed using 4C model to discover Clean Code principles and practice them.

Yoan

February 01, 2017
Tweet

More Decks by Yoan

Other Decks in How-to & DIY

Transcript

  1. @yot88
    By yoan thirion
    Clean code

    View Slide

  2. @yot88
    Your definition of clean code
    Imagine you open the most beautiful code on earth. It is perfect !!!
    What do you see in this code ? Why is it so perfect ?
    https://miro.com/welcomeonboard/EhZaQPO9XwTHCd7RGyP17SmoTCrN60QrAlngV6tGmoNo6BsWmCNTF4joUEID3rOI

    View Slide

  3. @yot88
    Robert C. Martin a.k.a
    Uncle bob
    Prentice Hall, 2009
    A word on the book

    View Slide

  4. View Slide

  5. @yot88
    Reading
    Writing
    Programmers = authors
    Time spent by programmers at work
    Our Audience = other programmers

    View Slide

  6. @yot88
    What is bad code ?
    • Not easy to understand
    • Classes are tightly coupled
    • Not easy to change
    • Does not communicate the intent
    • Shows too much of the internals (bad encapsulation)

    View Slide

  7. @yot88
    Code smells are warning signs in the code

    View Slide

  8. @yot88
    “The only valid measurement of code quality is the number of WTFs per minute” – Robert C. Martin

    View Slide

  9. @yot88
    developer
    good identifier names
    I can read code and
    understand code
    4 big topics
    Naming
    Comments
    Formatting
    functions

    View Slide

  10. Names must reveal your intentions

    View Slide

  11. @yot88
    Avoid Disinformation

    View Slide

  12. Use meaningful names in their self context

    View Slide

  13. Good names length

    View Slide

  14. Avoid Encodings

    View Slide

  15. Java class names from Spring Framework :
    • SimpleBeanFactoryAwareAspectInstanceFactory
    • AbstractInterceptorDrivenBeanDefinitionDecorator
    • AbstractSingletonProxyFactoryBean
    • Classes and objects should have noun or noun-phrase names
    • A class name should not be a verb.
    Class Names

    View Slide

  16. @yot88

    View Slide

  17. @yot88
    VenetianBlind
    Television
    Picture
    Glass
    Sofa
    TelevisionRemoteControl
    Door
    Peephole

    View Slide

  18. @yot88
    Use an ubiquitous language
    Language used :
    By all team members
    By all business experts
    In the source code as well

    View Slide

  19. @yot88
    What about comments ?
    “A common fallacy is to assume authors of incomprehensible code will somehow be able to express
    themselves lucidly and clearly in comments.”
    —Kevlin Henney

    View Slide

  20. @yot88
    Redundant comments

    View Slide

  21. @yot88
    Misleading comments
    can contain lies

    View Slide

  22. @yot88
    Don't leave commented out code in your codebase

    View Slide

  23. @yot88
    Don't have journal comments
    Use your source control instead

    View Slide

  24. @yot88
    Comments are noise

    View Slide

  25. @yot88
    Valid use of comments

    View Slide

  26. @yot88
    Public API comments

    View Slide

  27. @yot88
    Legal comments

    View Slide

  28. @yot88
    TODO comments

    View Slide

  29. @yot88
    Explanation of intent

    View Slide

  30. @yot88
    One-sentence documentation comments
    – peter hilton
    1. Write the best code you can.
    2. Write a one-sentence documentation comment for every public class and method/function.
    3. Refactor the code.
    4. Delete unnecessary comments.
    5. Rewrite bad comments (because all good writing requires rewriting).
    6. Only add detail where necessary.
    Comments are Answering the
    whyquestions that you can’t answer in code

    View Slide

  31. @yot88
    What about formatting ?

    View Slide

  32. Use consistent capitalization

    View Slide

  33. Use consistent capitalization

    View Slide

  34. @yot88
    We are uncovering better ways of developing software by
    doing it and helping others do it.
    Through this work we have come to value:
    Individuals and interactions over
    Working software over comprehensiv
    Customer collaboration over contra
    Responding to change over following a plan
    Unsustainable Spacing

    View Slide

  35. @yot88
    Column 80
    How many programmers lay out their code

    View Slide

  36. How people read

    View Slide

  37. @yot88

    View Slide

  38. @yot88

    View Slide

  39. @yot88
    Cyclomatic complexity :
    Metric to indicate the complexity of a piece of code
    Measure the number of independent paths through our code
    Indentation can reveal things

    View Slide

  40. @yot88
    Functions

    View Slide

  41. Functions Should be Small
    (no longer than about 6 or so lines long)

    View Slide

  42. Functions Should Do Only One Thing

    View Slide

  43. @yot88
    Use Descriptive Names

    View Slide

  44. @yot88
    Function Arguments
    A function should have preferably no arguments (niladic).
    Otherwise, preferably one (monadic),
    two is okay (dyadic),
    three (triadic) should be avoided where possible,
    and you should never have four or more (polyadic).

    View Slide

  45. @yot88
    Monadic Functions
    3valid forms :
    boolean FileExists("MyFile")
    InputStream FileOpen("MyFile")
    void onPasswordAttemptsFailed(int attempts)
    Those taking “flag” arguments, such as
    void Render(true/false)
    should be split in 2 functions that take no argument

    View Slide

  46. @yot88
    Diadic Functions
    There are times when two arguments are conceptually important, such as
    AddNewPoint(int x, int y)
    > 2 arguments : your function is trying to do too much ?

    View Slide

  47. @yot88
    Command/Query Separation

    View Slide

  48. @yot88
    not do both
    Command/Query Separation
    A function should either :
    do something : change the state of an object — a command
    return something : return some information about that object — a query

    View Slide

  49. @yot88
    conditional that performs various actions depending on object type or
    properties.

    View Slide

  50. Tell-Don't-Ask principle
    Replace Conditional with Polymorphism

    View Slide

  51. @yot88
    Prefer Exceptions to Returning Error Codes

    View Slide

  52. @yot88
    Rule number 1
    “Leave the campground cleaner than you found it.” - Boy scout rule
    “Leave your code better than you found it.” - Clean coder

    View Slide

  53. @yot88
    CODE SMELLS

    View Slide

  54. @yot88
    exercises
    Practice clean code principles on code examples
    Clone the repository at : https://github.com/ythirion/clean-code

    View Slide

  55. @yot88
    Exercise 1

    View Slide

  56. @yot88
    https://refactoring.guru/

    View Slide

  57. @yot88
    Exercise 2

    View Slide

  58. Exercise 3
    Identify the code smells
    Define your refactoring strategy (steps)
    https://yoan-thirion.gitbook.io/knowledge-base/software-craftsmanship/code-katas/mikado-method

    View Slide

  59. Exercise 3
    Comments are useless
    Naming
    Remove magic numbers (const+enum)
    Switch case
    What else ?

    View Slide

  60. Exercise 3
    SRP (Loyalty and Account Status Calculator)
    Polymorphism

    View Slide

  61. Exercise 4

    View Slide

  62. @yot88
    Acronyms
    please

    View Slide

  63. Clean code acronyms
    cheat sheet
    kiss Keep it simple AND stupid
    Simplicity should be a key goal in design, and that unnecessary complexity should be avoided
    You ain't gonna need it
    Programmers should not add functionality until it is necessary
    Always implement things when you need them, never when you just foresee that you need them
    yagni
    DRY
    Don't repeat yourself
    Reducing repetition of information of all kinds
    dry
    die Duplication
    is
    evil
    Every piece of knowledge must have a single, unambiguous, representation within a system

    View Slide

  64. @yot88
    “Always code as if the guy who ends up maintaining your code
    will be a violent psychopath who knows where you live”
    - Martin Golding

    View Slide

  65. @yot88

    View Slide

  66. View Slide

  67. @yot88
    4 square feedback
    Connections : my feeling about what I have learned are… learned : the most important concepts I learned are…
    Concrete practice : what I plan to do with what I learned… Conclusions : a final comment, suggestion, or question I still have is

    View Slide