SlideShare a Scribd company logo
1 of 37
Nick Murphy
Center for Astrophysics | Harvard & Smithsonian
With thanks to: the PlasmaPy, SunPy, and Astropy communities; the
Python in Heliophysics Community; Sterling Smith; Sumana
Harihareswara; Leonard Richardson; and many others.
Writing Clean Scientific Software
About me
● Grew up in Michigan
○ Side effect: I really like suspension bridges
● Undergrad at the University of Michigan
○ Side effect: I know about rabbit dietary preferences
● Grad school in astronomy at the University of Wisconsin
○ Thesis: simulating magnetic reconnection
○ Side effect: I started reading sci-fi poetry
● Have been at Center for Astrophysics for a decade
○ Side effect: getting to drive ~1800 km with a very grumpy cat
● Helped begin the PlasmaPy project
What is PlasmaPy?
Mission
To grow an open source software ecosystem
for plasma research & education
Anatomy of a software ecosystem
● PlasmaPy core package
○ Most frequently needed functionality
○ Under active development (clean coding is important)
● Affiliated packages
○ Will contain specialized functionality
● Educational resources
○ Introduce plasma concepts using PlasmaPy
● Community
○ You’re welcome to say hi on our Riot chat room!
○ Or join in our weekly community meetings!
Where I’m coming from...
● This talk does not come from:
○ Years of experience writing clean code
● Rather, this talk comes from:
○ Years of experience writing messy code
○ And then living with the consequences... 😿
○ Lessons from PlasmaPy
● Many of these suggestions come from:
○ Clean Code and Clean Architecture by R. Martin
○ Best Practices for Scientific Computing by G. Wilson et al.
○ Pseudo-random blog posts on the World Wide Web™
○ Pseudo-random friends
Common pain points with scientific software
● Often not openly available
● Difficult installation
● Inadequate documentation
● Lack of user-friendliness
● Cryptic error messages
● Missing tests
● Unreadable code
Why do these pain points exist?
● Programming not covered in physics courses
● We tend to be self-taught programmers
● Worth often measured by number of publications
● Code is often written in a rush
● Time pressure prevents us from taking time to learn
● Software not valued as a research product
Consequences of these pain points
● Beginning research is hard
● Collaboration is difficult
● Duplication of functionality
● Research is less reproducible
● Research can be frustrating
How do we address these pain points?
● Make our software open source
● Use a high-level language
● Prioritize documentation
● Create an automated test suite
● Develop code as a community
● Write readable, reusable, & maintainable code
My definition of clean code
● Readable and modifiable
● Communicates intent
● Well-tested
● Good documentation
● Succinct
● Can understand the big picture
● Makes research fun!
Code is communication!
Which is more readable?
>>> omega_ce = 1.76e7*(B/u.G)*u.rad/u.s
>>> electron_gyrofrequency = e * B / m_e
How do we choose good variable names?
● Reveal intention and meaning
● Choose clarity over brevity
○ Longer names are better than unclear abbreviations
● Avoid ambiguity
○ Is electron_gyrofrequency an angular frequency?
○ Is volume in m3 or in barn-megaparsecs?
● Be consistent
○ Use one word for each concept
● Use searchable names
Change numerical values to named constants
● In this expression:
velocity = -9.81 * time
○ Where does -9.81 come from?
○ Are we sure it’s correct?
○ What if we go to a different planet?
● Clarify intent by using named constants instead:
velocity = gravitational_acceleration * time
Decompose large programs into functions
● Huge chunks of code are hard to:
○ Read
○ Test
○ Keep track of in our mind
● Breaking code into functions helps us:
○ Re-use code
○ Improve readability
○ Isolate bugs
Don’t repeat yourself
● Copying and pasting code is fraught with peril
○ Bugs would need to be fixed for every copy
● Create functions instead of copying code
○ Simplifies fixing bugs
○ Can re-use code
● If we want to change one thing in the code, we
should only need to change it in one place
How do we write clean functions?
● Functions should:
○ Be short
○ Do one thing
○ Have no side effects
● Write explanatory note at top of function
● Avoid having too many required arguments
○ Use keywords or optional arguments
○ Define classes or data structures
Comments are not inherently good!
● As code evolves, comments often:
○ Become out-of-date
○ Contain misleading information
● “A comment is a lie waiting to happen” 🙀
Not so helpful comments
● Commented out code
○ Quickly becomes irrelevant
○ Use version control instead
● Definitions of variables
○ Encode definitions in variables names instead
● Redundant comments
i = i + 1 # increment i
● Description of the implementation
○ Becomes obsolete quickly
○ Communicate the implementation in the code itself
Helpful commenting practices
● Explain the intent but not the implementation
○ Refactor code instead of explaining how it works
● Amplify important points
● Explain why an approach was not used
● Provide context and references
● Update comments when updating code
Well-written tests make code more flexible
● Without tests:
○ Changes might introduce hidden bugs
○ Less likely to change code for fear of breaking
something
● With clean tests:
○ We know if a change broke something
○ We can track down bugs more quickly
● “Legacy code is code without tests.”
— from Working Effectively with Legacy Code by M. Feathers
A minimal software test
def test_douglas_adams_number():
“““Test answer to life, the universe, & everything.”””
assert 6 * 9 == 42,“Universe is broken.”
● Descriptive name
● Descriptive docstring
● A check that a condition is met
● Descriptive error message if condition
Testing best practices
● Write assertions into code
○ Raise error if positive_number is negative
● Turn every bug into a new test
○ Tells us when that bug is fixed
○ Prevents bug from happening in future
● Error messages should
○ Describe what went wrong
○ Provide information needed to fix problem
● Run tests often!!!!
○ To find bugs as soon as we introduce them
Test-driven development
● Most common practice:
○ Write a function
○ Write tests for that function
○ Fix bugs in the function
● Test-driven development
○ Write tests for a function
○ Write and edit the function until tests pass
● Advantages of writing tests first
○ Makes us think about what each function will do
○ Saves us time!
How do we know what tests to write?
● We write tests to:
○ Provide confidence that code gives correct results
○ Help us find and track down bugs
● Test some typical cases
● Test special cases
○ If a function acts weird near 0, test at 0
● Test near and at the boundaries
○ If a function requires a value ≥ 1, test at 1 and 1.00001
● Test that code fails correctly
○ If a function requires a value ≥ 1, test at 0.999
High-level vs. low-level code
● High-level code:
○ Describes the big picture
○ “Abstracts away” implementation details
● Low-level code:
○ Describes implementation details
○ Contains concrete instructions for a computer
High-level vs. low-level cooking instructions
● High-level: describe goal of recipe
○ Bake a cake
● Low-level: a line in a recipe
○ Add 1 barn-Mpc of baking powder to flour
Avoid mixing low-level & high-level code
● Mixing low-level & high-level code makes it
harder to:
○ Understand what the program is doing
○ Change how code is implemented
● Separate high-level, big picture code from
low-level implementation details
Write code as a top-down narrative1
To perform a numerical simulation, we:
1. Read in the inputs
2. Construct the grid
3. Perform the time advances
4. Output the results
1 This is called the “Stepdown Rule” in Clean Code by R. Martin.
Write code as a top-down narrative
To perform a numerical simulation, we:
1. To read in the inputs, we:
1. Open the input file
2. Read in each individual parameter
3. Close the input file
2. Construct the grid
3. Perform the time advances
4. Output the results
● Each of these lines can be a function
Write code as a top-down narrative
To perform a numerical simulation, we:
1. To read in the inputs, we:
1. Open the input file
2. To read in each individual parameter, we:
1. Read in a line of text
2. Parse the text
3. Store the variable
3. Close the input file
2. Construct the grid
3. Perform the time advances
4. Output the results
When is it worth taking time to write clean code?
● Writing clean code requires time and effort 😿
○ Sometimes it’s worth it 😸
○ Sometimes it’s not 🙀
● Code to be used once needn’t be (very) clean 😹
● Taking time to write clean code is worth it when:
○ You’ll re-use the code 😺
○ The code will be shared with others 🐈 🐈 🐈 🐈 🐈 🐈
● Avoid perfectionism 😼
○ Best to mostly (but not completely) follow this advice
Announcing APS DPP open source mini-conference
● Mini-conference on: Growing an open source
software ecosystem for plasma science
○ To be held at virtual APS DPP meeting in November
○ Abstracts due by June 29 at 5 pm EDT
Final thoughts
● Code is communication!
● Helpful to practice reading code
● Important to take time to learn
● Break up complicated code into manageable chunks
● Writing clean code is an iterative process
● No single way to write clean code
● I’m happy to talk more later about PlasmaPy!
Extra slides
“Program to an interface, not an implementation”
● Suppose our program uses atomic data
● We’re using the Chianti database, but want to use
AtomDB
● If our high-level code repeatedly calls Chianti, then…
○ Switching to AtomDB will be a pain!
● If our high-level code calls functions that call Chianti
○ We need only make these interface functions call
AtomDB instead
○ The high-level code can remain unchanged!
Separate stable & unstable code with boundaries
● These interface functions
represent a boundary
● The clean, stable code depends
directly on the boundary, not the
messy unstable code
● The boundary should be stable

More Related Content

What's hot

Advantages of Python Learning | Why Python
Advantages of Python Learning | Why PythonAdvantages of Python Learning | Why Python
Advantages of Python Learning | Why PythonEvoletTechnologiesCo
 
The Ring programming language version 1.2 book - Part 77 of 84
The Ring programming language version 1.2 book - Part 77 of 84The Ring programming language version 1.2 book - Part 77 of 84
The Ring programming language version 1.2 book - Part 77 of 84Mahmoud Samir Fayed
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golangBasil N G
 
Proglangauage1.10.18
Proglangauage1.10.18Proglangauage1.10.18
Proglangauage1.10.18Thinkful
 
Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality:  A Panacéia para seu código ElixirElixir Brasil 2019 - Quality:  A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality: A Panacéia para seu código ElixirWeverton Timoteo
 
Evolution or stagnation programming languages
Evolution or stagnation programming languagesEvolution or stagnation programming languages
Evolution or stagnation programming languagesDaniele Esposti
 
The Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventureThe Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventuremylittleadventure
 
The TKP Intentional Method of Teaching Kids to Program
The TKP Intentional Method of Teaching Kids to ProgramThe TKP Intentional Method of Teaching Kids to Program
The TKP Intentional Method of Teaching Kids to ProgramLynn Langit
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go langAmal Mohan N
 
Teaching Kids Programming Using the Intentional Method
Teaching Kids Programming Using the Intentional MethodTeaching Kids Programming Using the Intentional Method
Teaching Kids Programming Using the Intentional MethodJessica Ellis
 
Is your code ready for testing?
Is your code ready for testing?Is your code ready for testing?
Is your code ready for testing?Ralph Ligtenberg
 
Teaching Kids Programming Using the Intentional Method
Teaching Kids Programming Using the Intentional MethodTeaching Kids Programming Using the Intentional Method
Teaching Kids Programming Using the Intentional MethodLynn Langit
 
Natural language identification
Natural language identificationNatural language identification
Natural language identificationShaktiTaneja
 
Microservices, the lean way
Microservices, the lean wayMicroservices, the lean way
Microservices, the lean wayBruno Bossola
 
Teaching Kids Programming using Agile Practices
Teaching Kids Programming using Agile PracticesTeaching Kids Programming using Agile Practices
Teaching Kids Programming using Agile PracticesLynn Langit
 
Why you should care about Go (Golang)
Why you should care about Go (Golang)Why you should care about Go (Golang)
Why you should care about Go (Golang)Aaron Schlesinger
 
Python debuggers slides
Python debuggers slidesPython debuggers slides
Python debuggers slidesmattboehm
 
LCE12: Intro Training: Upstreaming 101
LCE12: Intro Training: Upstreaming 101LCE12: Intro Training: Upstreaming 101
LCE12: Intro Training: Upstreaming 101Linaro
 

What's hot (20)

Advantages of Python Learning | Why Python
Advantages of Python Learning | Why PythonAdvantages of Python Learning | Why Python
Advantages of Python Learning | Why Python
 
The Ring programming language version 1.2 book - Part 77 of 84
The Ring programming language version 1.2 book - Part 77 of 84The Ring programming language version 1.2 book - Part 77 of 84
The Ring programming language version 1.2 book - Part 77 of 84
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Kshitij
KshitijKshitij
Kshitij
 
Proglangauage1.10.18
Proglangauage1.10.18Proglangauage1.10.18
Proglangauage1.10.18
 
Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality:  A Panacéia para seu código ElixirElixir Brasil 2019 - Quality:  A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir
 
Evolution or stagnation programming languages
Evolution or stagnation programming languagesEvolution or stagnation programming languages
Evolution or stagnation programming languages
 
The Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventureThe Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventure
 
Nlp project
Nlp projectNlp project
Nlp project
 
The TKP Intentional Method of Teaching Kids to Program
The TKP Intentional Method of Teaching Kids to ProgramThe TKP Intentional Method of Teaching Kids to Program
The TKP Intentional Method of Teaching Kids to Program
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go lang
 
Teaching Kids Programming Using the Intentional Method
Teaching Kids Programming Using the Intentional MethodTeaching Kids Programming Using the Intentional Method
Teaching Kids Programming Using the Intentional Method
 
Is your code ready for testing?
Is your code ready for testing?Is your code ready for testing?
Is your code ready for testing?
 
Teaching Kids Programming Using the Intentional Method
Teaching Kids Programming Using the Intentional MethodTeaching Kids Programming Using the Intentional Method
Teaching Kids Programming Using the Intentional Method
 
Natural language identification
Natural language identificationNatural language identification
Natural language identification
 
Microservices, the lean way
Microservices, the lean wayMicroservices, the lean way
Microservices, the lean way
 
Teaching Kids Programming using Agile Practices
Teaching Kids Programming using Agile PracticesTeaching Kids Programming using Agile Practices
Teaching Kids Programming using Agile Practices
 
Why you should care about Go (Golang)
Why you should care about Go (Golang)Why you should care about Go (Golang)
Why you should care about Go (Golang)
 
Python debuggers slides
Python debuggers slidesPython debuggers slides
Python debuggers slides
 
LCE12: Intro Training: Upstreaming 101
LCE12: Intro Training: Upstreaming 101LCE12: Intro Training: Upstreaming 101
LCE12: Intro Training: Upstreaming 101
 

Similar to Writing clean scientific software Murphy cleancoding

AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEGavin Pickin
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017Ortus Solutions, Corp
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With PytestEddy Reyes
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
Pentester++
Pentester++Pentester++
Pentester++CTruncer
 
Porting 100k Lines of Code to TypeScript
Porting 100k Lines of Code to TypeScriptPorting 100k Lines of Code to TypeScript
Porting 100k Lines of Code to TypeScriptTiny
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleikram_ahamed
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Clean application development (talk)
Clean application development (talk)Clean application development (talk)
Clean application development (talk)Adam Culp
 
How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)Asier Barrenetxea
 
Writing Tests Effectively
Writing Tests EffectivelyWriting Tests Effectively
Writing Tests EffectivelyPaul Boocock
 
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi Krzysztof (Chris) Ozog
 
Day 2 (Lecture 5): A Practitioner's Perspective on Building Machine Product i...
Day 2 (Lecture 5): A Practitioner's Perspective on Building Machine Product i...Day 2 (Lecture 5): A Practitioner's Perspective on Building Machine Product i...
Day 2 (Lecture 5): A Practitioner's Perspective on Building Machine Product i...Aseda Owusua Addai-Deseh
 
OSDC 2015: Kris Buytaert | From ConfigManagementSucks to ConfigManagementLove
OSDC 2015: Kris Buytaert | From ConfigManagementSucks to ConfigManagementLoveOSDC 2015: Kris Buytaert | From ConfigManagementSucks to ConfigManagementLove
OSDC 2015: Kris Buytaert | From ConfigManagementSucks to ConfigManagementLoveNETWAYS
 
Exploring the Real Power of Functional Programming
Exploring the Real Power of Functional ProgrammingExploring the Real Power of Functional Programming
Exploring the Real Power of Functional ProgrammingKnoldus Inc.
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 

Similar to Writing clean scientific software Murphy cleancoding (20)

Pair programming
Pair programmingPair programming
Pair programming
 
What is xp
What is xpWhat is xp
What is xp
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLEAN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
 
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Pentester++
Pentester++Pentester++
Pentester++
 
Porting 100k Lines of Code to TypeScript
Porting 100k Lines of Code to TypeScriptPorting 100k Lines of Code to TypeScript
Porting 100k Lines of Code to TypeScript
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Clean application development (talk)
Clean application development (talk)Clean application development (talk)
Clean application development (talk)
 
How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)How to deliver the right software (Specification by example)
How to deliver the right software (Specification by example)
 
Writing Tests Effectively
Writing Tests EffectivelyWriting Tests Effectively
Writing Tests Effectively
 
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi
Code Camp NYC 2017 - How to deal with everything... | Chris Ozog - Codesushi
 
Day 2 (Lecture 5): A Practitioner's Perspective on Building Machine Product i...
Day 2 (Lecture 5): A Practitioner's Perspective on Building Machine Product i...Day 2 (Lecture 5): A Practitioner's Perspective on Building Machine Product i...
Day 2 (Lecture 5): A Practitioner's Perspective on Building Machine Product i...
 
OSDC 2015: Kris Buytaert | From ConfigManagementSucks to ConfigManagementLove
OSDC 2015: Kris Buytaert | From ConfigManagementSucks to ConfigManagementLoveOSDC 2015: Kris Buytaert | From ConfigManagementSucks to ConfigManagementLove
OSDC 2015: Kris Buytaert | From ConfigManagementSucks to ConfigManagementLove
 
Exploring the Real Power of Functional Programming
Exploring the Real Power of Functional ProgrammingExploring the Real Power of Functional Programming
Exploring the Real Power of Functional Programming
 
Go fundamentals
Go fundamentalsGo fundamentals
Go fundamentals
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 

More from saber tabatabaee

clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxsaber tabatabaee
 
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجیهاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجیsaber tabatabaee
 
لاراول ارائه 26 سپتامبر 2021 اسکایپ
لاراول ارائه 26 سپتامبر 2021 اسکایپلاراول ارائه 26 سپتامبر 2021 اسکایپ
لاراول ارائه 26 سپتامبر 2021 اسکایپsaber tabatabaee
 
scrum master اسکرام مستر
scrum master اسکرام مسترscrum master اسکرام مستر
scrum master اسکرام مسترsaber tabatabaee
 
Scrum master - daily scrum master story
Scrum master - daily scrum master storyScrum master - daily scrum master story
Scrum master - daily scrum master storysaber tabatabaee
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionsaber tabatabaee
 
teaching data science students to write clean code
teaching data science students to write clean codeteaching data science students to write clean code
teaching data science students to write clean codesaber tabatabaee
 
R. herves. clean code (theme)2
R. herves. clean code (theme)2R. herves. clean code (theme)2
R. herves. clean code (theme)2saber tabatabaee
 
refactoring code by clean code rules
refactoring code by clean code rulesrefactoring code by clean code rules
refactoring code by clean code rulessaber tabatabaee
 
sharepoint 2007 presentation in crcis
sharepoint 2007 presentation in crcis sharepoint 2007 presentation in crcis
sharepoint 2007 presentation in crcis saber tabatabaee
 
Linux DVD 03 Learnkey linux+ setup
Linux DVD 03 Learnkey linux+ setupLinux DVD 03 Learnkey linux+ setup
Linux DVD 03 Learnkey linux+ setupsaber tabatabaee
 

More from saber tabatabaee (18)

clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
 
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجیهاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
هاستینگ و راه اندازی یک پروژه لاراول - آنالیز و امکان سنجی
 
لاراول ارائه 26 سپتامبر 2021 اسکایپ
لاراول ارائه 26 سپتامبر 2021 اسکایپلاراول ارائه 26 سپتامبر 2021 اسکایپ
لاراول ارائه 26 سپتامبر 2021 اسکایپ
 
scrum master اسکرام مستر
scrum master اسکرام مسترscrum master اسکرام مستر
scrum master اسکرام مستر
 
L5 swagger
L5 swaggerL5 swagger
L5 swagger
 
Crm or xrm
Crm or xrmCrm or xrm
Crm or xrm
 
Scrum master - daily scrum master story
Scrum master - daily scrum master storyScrum master - daily scrum master story
Scrum master - daily scrum master story
 
Online exam ismc
Online exam ismcOnline exam ismc
Online exam ismc
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English version
 
teaching data science students to write clean code
teaching data science students to write clean codeteaching data science students to write clean code
teaching data science students to write clean code
 
R. herves. clean code (theme)2
R. herves. clean code (theme)2R. herves. clean code (theme)2
R. herves. clean code (theme)2
 
Clean code chpt_1
Clean code chpt_1Clean code chpt_1
Clean code chpt_1
 
Code quality
Code qualityCode quality
Code quality
 
refactoring code by clean code rules
refactoring code by clean code rulesrefactoring code by clean code rules
refactoring code by clean code rules
 
clean code - uncle bob
clean code - uncle bobclean code - uncle bob
clean code - uncle bob
 
sharepoint 2007 presentation in crcis
sharepoint 2007 presentation in crcis sharepoint 2007 presentation in crcis
sharepoint 2007 presentation in crcis
 
Linux DVD 03 Learnkey linux+ setup
Linux DVD 03 Learnkey linux+ setupLinux DVD 03 Learnkey linux+ setup
Linux DVD 03 Learnkey linux+ setup
 
linux+ learnkey DVD 2
linux+ learnkey DVD 2 linux+ learnkey DVD 2
linux+ learnkey DVD 2
 

Recently uploaded

WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 

Recently uploaded (20)

WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

Writing clean scientific software Murphy cleancoding

  • 1. Nick Murphy Center for Astrophysics | Harvard & Smithsonian With thanks to: the PlasmaPy, SunPy, and Astropy communities; the Python in Heliophysics Community; Sterling Smith; Sumana Harihareswara; Leonard Richardson; and many others. Writing Clean Scientific Software
  • 2. About me ● Grew up in Michigan ○ Side effect: I really like suspension bridges ● Undergrad at the University of Michigan ○ Side effect: I know about rabbit dietary preferences ● Grad school in astronomy at the University of Wisconsin ○ Thesis: simulating magnetic reconnection ○ Side effect: I started reading sci-fi poetry ● Have been at Center for Astrophysics for a decade ○ Side effect: getting to drive ~1800 km with a very grumpy cat ● Helped begin the PlasmaPy project
  • 3. What is PlasmaPy? Mission To grow an open source software ecosystem for plasma research & education
  • 4. Anatomy of a software ecosystem ● PlasmaPy core package ○ Most frequently needed functionality ○ Under active development (clean coding is important) ● Affiliated packages ○ Will contain specialized functionality ● Educational resources ○ Introduce plasma concepts using PlasmaPy ● Community ○ You’re welcome to say hi on our Riot chat room! ○ Or join in our weekly community meetings!
  • 5. Where I’m coming from... ● This talk does not come from: ○ Years of experience writing clean code ● Rather, this talk comes from: ○ Years of experience writing messy code ○ And then living with the consequences... 😿 ○ Lessons from PlasmaPy ● Many of these suggestions come from: ○ Clean Code and Clean Architecture by R. Martin ○ Best Practices for Scientific Computing by G. Wilson et al. ○ Pseudo-random blog posts on the World Wide Web™ ○ Pseudo-random friends
  • 6. Common pain points with scientific software ● Often not openly available ● Difficult installation ● Inadequate documentation ● Lack of user-friendliness ● Cryptic error messages ● Missing tests ● Unreadable code
  • 7. Why do these pain points exist? ● Programming not covered in physics courses ● We tend to be self-taught programmers ● Worth often measured by number of publications ● Code is often written in a rush ● Time pressure prevents us from taking time to learn ● Software not valued as a research product
  • 8. Consequences of these pain points ● Beginning research is hard ● Collaboration is difficult ● Duplication of functionality ● Research is less reproducible ● Research can be frustrating
  • 9. How do we address these pain points? ● Make our software open source ● Use a high-level language ● Prioritize documentation ● Create an automated test suite ● Develop code as a community ● Write readable, reusable, & maintainable code
  • 10. My definition of clean code ● Readable and modifiable ● Communicates intent ● Well-tested ● Good documentation ● Succinct ● Can understand the big picture ● Makes research fun!
  • 12. Which is more readable? >>> omega_ce = 1.76e7*(B/u.G)*u.rad/u.s >>> electron_gyrofrequency = e * B / m_e
  • 13. How do we choose good variable names? ● Reveal intention and meaning ● Choose clarity over brevity ○ Longer names are better than unclear abbreviations ● Avoid ambiguity ○ Is electron_gyrofrequency an angular frequency? ○ Is volume in m3 or in barn-megaparsecs? ● Be consistent ○ Use one word for each concept ● Use searchable names
  • 14. Change numerical values to named constants ● In this expression: velocity = -9.81 * time ○ Where does -9.81 come from? ○ Are we sure it’s correct? ○ What if we go to a different planet? ● Clarify intent by using named constants instead: velocity = gravitational_acceleration * time
  • 15. Decompose large programs into functions ● Huge chunks of code are hard to: ○ Read ○ Test ○ Keep track of in our mind ● Breaking code into functions helps us: ○ Re-use code ○ Improve readability ○ Isolate bugs
  • 16. Don’t repeat yourself ● Copying and pasting code is fraught with peril ○ Bugs would need to be fixed for every copy ● Create functions instead of copying code ○ Simplifies fixing bugs ○ Can re-use code ● If we want to change one thing in the code, we should only need to change it in one place
  • 17. How do we write clean functions? ● Functions should: ○ Be short ○ Do one thing ○ Have no side effects ● Write explanatory note at top of function ● Avoid having too many required arguments ○ Use keywords or optional arguments ○ Define classes or data structures
  • 18. Comments are not inherently good! ● As code evolves, comments often: ○ Become out-of-date ○ Contain misleading information ● “A comment is a lie waiting to happen” 🙀
  • 19. Not so helpful comments ● Commented out code ○ Quickly becomes irrelevant ○ Use version control instead ● Definitions of variables ○ Encode definitions in variables names instead ● Redundant comments i = i + 1 # increment i ● Description of the implementation ○ Becomes obsolete quickly ○ Communicate the implementation in the code itself
  • 20. Helpful commenting practices ● Explain the intent but not the implementation ○ Refactor code instead of explaining how it works ● Amplify important points ● Explain why an approach was not used ● Provide context and references ● Update comments when updating code
  • 21. Well-written tests make code more flexible ● Without tests: ○ Changes might introduce hidden bugs ○ Less likely to change code for fear of breaking something ● With clean tests: ○ We know if a change broke something ○ We can track down bugs more quickly ● “Legacy code is code without tests.” — from Working Effectively with Legacy Code by M. Feathers
  • 22. A minimal software test def test_douglas_adams_number(): “““Test answer to life, the universe, & everything.””” assert 6 * 9 == 42,“Universe is broken.” ● Descriptive name ● Descriptive docstring ● A check that a condition is met ● Descriptive error message if condition
  • 23. Testing best practices ● Write assertions into code ○ Raise error if positive_number is negative ● Turn every bug into a new test ○ Tells us when that bug is fixed ○ Prevents bug from happening in future ● Error messages should ○ Describe what went wrong ○ Provide information needed to fix problem ● Run tests often!!!! ○ To find bugs as soon as we introduce them
  • 24. Test-driven development ● Most common practice: ○ Write a function ○ Write tests for that function ○ Fix bugs in the function ● Test-driven development ○ Write tests for a function ○ Write and edit the function until tests pass ● Advantages of writing tests first ○ Makes us think about what each function will do ○ Saves us time!
  • 25. How do we know what tests to write? ● We write tests to: ○ Provide confidence that code gives correct results ○ Help us find and track down bugs ● Test some typical cases ● Test special cases ○ If a function acts weird near 0, test at 0 ● Test near and at the boundaries ○ If a function requires a value ≥ 1, test at 1 and 1.00001 ● Test that code fails correctly ○ If a function requires a value ≥ 1, test at 0.999
  • 26. High-level vs. low-level code ● High-level code: ○ Describes the big picture ○ “Abstracts away” implementation details ● Low-level code: ○ Describes implementation details ○ Contains concrete instructions for a computer
  • 27. High-level vs. low-level cooking instructions ● High-level: describe goal of recipe ○ Bake a cake ● Low-level: a line in a recipe ○ Add 1 barn-Mpc of baking powder to flour
  • 28. Avoid mixing low-level & high-level code ● Mixing low-level & high-level code makes it harder to: ○ Understand what the program is doing ○ Change how code is implemented ● Separate high-level, big picture code from low-level implementation details
  • 29. Write code as a top-down narrative1 To perform a numerical simulation, we: 1. Read in the inputs 2. Construct the grid 3. Perform the time advances 4. Output the results 1 This is called the “Stepdown Rule” in Clean Code by R. Martin.
  • 30. Write code as a top-down narrative To perform a numerical simulation, we: 1. To read in the inputs, we: 1. Open the input file 2. Read in each individual parameter 3. Close the input file 2. Construct the grid 3. Perform the time advances 4. Output the results ● Each of these lines can be a function
  • 31. Write code as a top-down narrative To perform a numerical simulation, we: 1. To read in the inputs, we: 1. Open the input file 2. To read in each individual parameter, we: 1. Read in a line of text 2. Parse the text 3. Store the variable 3. Close the input file 2. Construct the grid 3. Perform the time advances 4. Output the results
  • 32. When is it worth taking time to write clean code? ● Writing clean code requires time and effort 😿 ○ Sometimes it’s worth it 😸 ○ Sometimes it’s not 🙀 ● Code to be used once needn’t be (very) clean 😹 ● Taking time to write clean code is worth it when: ○ You’ll re-use the code 😺 ○ The code will be shared with others 🐈 🐈 🐈 🐈 🐈 🐈 ● Avoid perfectionism 😼 ○ Best to mostly (but not completely) follow this advice
  • 33. Announcing APS DPP open source mini-conference ● Mini-conference on: Growing an open source software ecosystem for plasma science ○ To be held at virtual APS DPP meeting in November ○ Abstracts due by June 29 at 5 pm EDT
  • 34. Final thoughts ● Code is communication! ● Helpful to practice reading code ● Important to take time to learn ● Break up complicated code into manageable chunks ● Writing clean code is an iterative process ● No single way to write clean code ● I’m happy to talk more later about PlasmaPy!
  • 36. “Program to an interface, not an implementation” ● Suppose our program uses atomic data ● We’re using the Chianti database, but want to use AtomDB ● If our high-level code repeatedly calls Chianti, then… ○ Switching to AtomDB will be a pain! ● If our high-level code calls functions that call Chianti ○ We need only make these interface functions call AtomDB instead ○ The high-level code can remain unchanged!
  • 37. Separate stable & unstable code with boundaries ● These interface functions represent a boundary ● The clean, stable code depends directly on the boundary, not the messy unstable code ● The boundary should be stable

Editor's Notes

  1. The PlasmaPy Project is an effort to grow an open source software ecosystem for plasma research and education. A software ecosystem is a collection of software projects that are developed and co-evolve in the same environment. A software ecosystem includes code, documentation, and tests. It includes educational elements, and it includes community.
  2. Should go after "write code as a top-down narrative".
  3. Should nominally go after "program to an interface, not an implementation".