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

PyConZA 2022 Best practices for good(ish) and clean(ish) code

PyConZA 2022 Best practices for good(ish) and clean(ish) code

Have you ever thought what is the cost vs revenue of a well written code and of an ugly code? Nowadays, in most companies, code bases are huge (in terms of lines of code and complexity) and the number of contributors is also high; this means that a well shaped code could benefit the company finance and the mental health of the employees (AKA software engineers).

Writing clean (and/or good) code requires discipline. This discipline can be acquired and increased during your job and doing experience with messy code, but more important is that using discipline, respecting and following some rules you can achieve elegant and efficient code.

In this talk I will cover some principles of clean code (they are too much for 30 minutes):

* Meaningful Names
* Formatting
* Tests and their importance
* Continuous Integration
* Code Refactoring
* Code Review
* Boundaries

All the work contained in this talk is mostly inspired by the book Clean Code - Robert C. Martin (AKA Uncle Bob) and the experience I have made during my current job and the previous one.

ernestoarbitrio

October 14, 2022
Tweet

More Decks by ernestoarbitrio

Other Decks in Programming

Transcript

  1. Best practices for good(ish) and clean(ish) code PyConZa 2022

  2. Who am I

  3. Why this talk

  4. Why this talk

  5. What’s Clean Code •Readable •Maintainable •Elegant •Testable

  6. None
  7. None
  8. Can be inherited and enhanced by other developers other than

    the original author Ask yourself if the code is “clean” enough
  9. Clean Code Measurement

  10. Owning a mess, is it worth?

  11. What prevents clean code? I’ll do the refactor later Rule

    of the thumb Spoiler alert: “Later” never comes
  12. Owning a mess, is it worth?

  13. Owning a mess, is it worth?

  14. Owning a mess, is it worth?

  15. Descriptive names The name should always represent the developer’s idea

  16. Meaningful names

  17. Searchable names

  18. Class names Classes should have noun or noun phrase names

    like Customer, WikiPage, Account, AddressParser. Avoid words like Manager, Processor, Data … in the name of a class. A class name should not be a verb.
  19. Method names Methods should have verb of verb phrase names

    like post_payment, delete_page, save. Method names should say what they do!
  20. Comments sometimes lie

  21. Code never lies

  22. Code never lies

  23. If they don’t lie, they can be lazy Crime: using

    a comment to avoid programming effort.
  24. If they don’t lie, they can be lazy Crime: using

    a comment to avoid programming effort.
  25. None
  26. Only one thing

  27. Only one thing

  28. Extract, extract, extract

  29. Extract, extract, extract

  30. Small cognitive/cyclomatic complexity 🙁

  31. Small cognitive/cyclomatic complexity 😊

  32. Advantages of measuring software complexity 1. Better Test Coverage

  33. Advantages of measuring software complexity 3. Reduced Risk

  34. Advantages of measuring software complexity 3. Lower Costs

  35. Law of Demeter (“only 1 dot”) Lieberherr, K.J.; Holland, I.M.

    (September 1989). "Assuring good style for object-oriented programs". IEEE Software. 6 (5): 38–48. doi:10.1109/52.35588. Ask a dog to walk 🙅 book.pages().last().text()
  36. Law of Demeter: drawbacks it won't apply to cats

  37. “Premature optimisation is the root of all evil” — Donald

    Knuth
  38. Continuous Refactoring “The most common “presenting” pathology in the hoarded

    codebases I’ve seen — by far — is that developers don’t feel they have time and/or permission to refactor code.” — Sarah Mei [Refactoring is] a very core technique to the whole agile way of thinking because it fi ts in with the whole way in which we can build software in a way that it can change easily … Refactoring is central to this because refactoring is a disciplined way of making changes. ” — Martin Fowler
  39. Tests matter “How can I make sure that if I

    change this, nothing else breaks?”
  40. … also experience matter Most popular code smells Duplication Improper

    use of inheritance Unnecessary complexity Convoluted Code Useless/misleading comments Tight coupling No Code Reviews Over abstraction Huge methods Design Pattern overuse Poor naming No test Commented code / code that’s not used Improper usage of private and public objects
  41. Thanks❤ github.com/ernestoarbitrio twitter.com/__pamaron__ Questions? 🤔