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

DRY, WET, SPOT…What?

Sam Jarman
April 13, 2024
38

DRY, WET, SPOT…What?

As an engineer, there seems to be a slew of contradicting advice on what to do about duplicate code. Never do it? Do it once then refactor? None of it really makes sense. This talk introduces a few new mindsets for reasoning about duplicate code and what to do about it.

Sam Jarman

April 13, 2024
Tweet

Transcript

  1. @samjarman • Code Camp Welly 2024 About me • Programming

    for over 15 years • Worked in Christchurch, Wellington, Auckland and now Sydney • Worked in businesses small, medium and large, start ups, agencies and enterprise • Worked in iOS (ObjC, Swift), Web (React, Angular), Backend (Ruby/Rails, Javascript/node) • Engineering Manager at Cochlear - a company that builds implants for people who are born without hearing or lose their hearing which helps them get their hearing back • Work on the backend of software that surgeons, audiologist and patients/carers use
  2. @samjarman • Code Camp Welly 2024 Agenda • Code Duplication

    – What is it? • Ways of Handling Duplication – Code Demo • Introducing SPOT • Introducing Connascence • Code Demo of Connascence • Mindsets to take away with you
  3. @samjarman • Code Camp Welly 2024 Today’s Talk • The

    code examples are in beginner TypeScript - but the language doesn’t make a difference to the broader point of this talk • The code examples in this talk is designed for conference talk glanceability/readability not perfection • “Code Unit” - refers to a method, class, module, package, microservice • You are welcome to live tweet/x/masto-post/threet/bluesk/whatever - I’m @samjarman on everything • Thoughts are my own and do not represent my employer
  4. @samjarman • Code Camp Welly 2024 Code Duplication – What

    is it? Code duplication is when code is repeated in the same method, class, module, application, system, or company. It is generally regarded as a code smell
  5. @samjarman • Code Camp Welly 2024 Code Smell • Code

    Smells are when something ‘smells’ about a codebase • “Where there’s smoke, there’s fire”
  6. @samjarman • Code Camp Welly 2024 Why is duplicated code

    bad? We are told… • When we have to change something, we have multiple places to change • We might not be able to find all the places we to change • This adds maintenance burdens to our codebase
  7. @samjarman • Code Camp Welly 2024 “DRY” • Don't Repeat

    Yourself is a popular phrase and mindset for avoiding duplication. • Easy to understand • Easy (ish) to do • Easier to code afterwards
  8. @samjarman • Code Camp Welly 2024 “YAGNI” • On the

    other end of the spectrum of DRY… • "YAGNI" - You Ain't Gonna Need It • Discourages over-engineering • You ain't gonna need it – you're not going to need that nice little method/package anywhere else. Don't build it.
  9. @samjarman • Code Camp Welly 2024 WET • Write Everything

    Twice • Write everything twice and on the third time – refactor • Between DRY and YAGNI
  10. @samjarman • Code Camp Welly 2024 Summary • DRY •

    WET • YAGNI → 3 Popular ways of thinking about avoiding coding duplication
  11. @samjarman • Code Camp Welly 2024 Types of Duplication •

    Accidental vs Deliberate • Deliberate = These two/n things should have be the same in order to maintain system correctness ◦ When person had two implementations of nickname • Accidental = These two/n things happen to be the same for now ◦ When Doctor happened to have the same nickname as Person
  12. @samjarman • Code Camp Welly 2024 Code exists through time

    • These two things are the same – but will they always be? • This is YAGNI but clearer
  13. @samjarman • Code Camp Welly 2024 SPOT – Single Point

    Of Truth • Person= two points of truth for a nickname • Doctor vs Person = Two, single points of truth, one for each person …Forget DRY/WET
  14. @samjarman • Code Camp Welly 2024 Coupling If I must

    change two places in order to keep system correctness… Then the things are “coupled”
  15. @samjarman • Code Camp Welly 2024 Connascence 1. The common

    birth of two or more at the same time. 2. That which is born or produced with another. 3. The act of growing together.
  16. @samjarman • Code Camp Welly 2024 Connascence – Melir Page-Jones

    (1992) “Connascence is a software quality metric [...] to allow reasoning about the complexity caused by coupling”
  17. @samjarman • Code Camp Welly 2024 Connascence Two software components

    are connascent if a change in one would require the other to be modified in order to maintain the overall correctness of the system.
  18. @samjarman • Code Camp Welly 2024 Types of Connascence •

    Name • Type • Meaning • Position • Algorithm • Execution • Timing • Value • Identity
  19. @samjarman • Code Camp Welly 2024 Types of Connascence •

    Name • Type • Meaning • Position • Algorithm • Execution • Timing • Value • Identity Weaker Stronger Degree Fewer More
  20. @samjarman • Code Camp Welly 2024 Types of Connascence •

    Name • Type • Meaning • Position • Algorithm • Execution • Timing • Value • Identity Weaker Stronger Degree Fewer More Locality
  21. @samjarman • Code Camp Welly 2024 Types of Connascence •

    Static ◦ Name ◦ Type ◦ Meaning ◦ Position ◦ Algorithm • Dynamic ◦ Execution ◦ Timing ◦ Value ◦ Identity
  22. @samjarman • Code Camp Welly 2024 Ease of Change •

    Name of a property/method - Easy • Type of a property – Easy • Parameters of a method – Harder • Contract of a method - Hard
  23. @samjarman • Code Camp Welly 2024 Connascence of Execution •

    If order matters • If you need to call moduleA before moduleB, there is coupling there • Consider combining into a single module, or state machines or other techniques to go back to Connascence of Algorithm
  24. @samjarman • Code Camp Welly 2024 Connascence of Timing •

    When the timing of the execution of multiple components is important • Similar to execution but time based • moduleA can start before or after moduleB starts but must finish before • I.e the dreaded ‘race conditions’
  25. @samjarman • Code Camp Welly 2024 Connascence of Value •

    When two sources of data have to agree on the value of something • Multiple databases (2-way sync, integrated systems)
  26. @samjarman • Code Camp Welly 2024 Connascence of Identity •

    When two/n resources have to be known as one in the system • Merging of resources • Keeping track of multiple identifiers for a given resource
  27. @samjarman • Code Camp Welly 2024 Locality • Refers to

    where the code units are • Might be: ◦ Same module ◦ Same file ◦ Different technology types (database vs backend vs front end) ◦ Different code bases ◦ Different departments in the company ◦ Different companies • The further away they are the harder it is to maintain ◦ The worse the connascence
  28. @samjarman • Code Camp Welly 2024 What else do I

    need to change if this changes?
  29. @samjarman • Code Camp Welly 2024 Does this happen to

    be the same or does it have be the same?
  30. @samjarman • Code Camp Welly 2024 Learn More & Thanks

    • Codemania 2015: Josh Robb - Connascence & Coupling https://www.youtube.com/watch?v=Ip2o4vbAK3s ◦ Some of these slides reproduced here with permission • https://en.wikipedia.org/wiki/Connascence • https://codesai.com/posts/2017/01/about-connascence