Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Teaching Data Science Students
to Write Clean Code
Todd Iverson, Winona State University
Slides/Code: https://bit.ly/2WgFI...
Episode 37
Doug Wants an A
Doug “Crazy Legs” Ervison “Mean” Dr. Iverson
Hi! I’m
Doug
Grrr
The Hero The Villain
I NEED
an A!
Doug?
An A?
HA!
His code
...
Doug will demonstrate
1. Good names
2. Small functions
3. Unit tests
4. Refactor code, specifically
1. Extract functions
2...
Opening Scene - The Assignment
https://www.kaggle.com/c/spooky-author-identification/data
Kaggle
is Kool!
Kaggle
assignmen...
Doug’s Original code
(…this assignment
require unit tests!…)
Iverson
loves Bag of
Words!
I am going to
get an A for
sure!
...
It looks like our hero is doomed
to an F!
Then just in the nick of time …
… Doug remembers unit tests!
You MUST have
unit tests.
What are unit tests?
• Captures/maintain intended behavior
• Helpful when changing code
• Should be automated
Doug writes some unit tests
Original behavior
New behavior
That was
easy!
And my code
passed!
Doug’s Original code
(…with names
like that …)
Remembered
the Unit Tests!
I am going to
get an A for
sure!
(…a C at
best…)
Luckily, Doug remembers to think about names
Names are
important!
They should
express
intent!
Good names…
• Reveal intent
• Use the proper parts of speech
• Have the proper length for their scope
• Avoids disinformat...
Good names…
• Reveal intent
• Use the proper parts of speech
• Have the proper length for their scope
• Avoids disinformat...
These names
are bad.
What’ ews
again??
Doug inspects
his names
ews hold the words
for Edgar Allen Poe
Maybe I
should just use
poe_words
Doug finds some better names
The other
names are
better too!
poe_words is
better than ews
A+ for
sure!
(…see the
bug?…)
(…D at
best…)
It looks like our hero is doomed
with D!
Then just in the nick of time …
… Doug remembers to test!
Good thing I
ran unit test!
Better test!
Oops!
What else
would Iverson
complain
about?
Doug imagines Iverson’s feedback
Clean
functions do
one thing
You need more
functions
and Doug even remembers refactoring!
… and extract
a function!
Find a block
that does
something …
Common Refactoring Technique
Extract Function
The DRY principle
• Don’t repeat yourself!
• Find similar code
• Make it exactly the same
• Extract a function!
Extract Functions
Replace hyphens
Remove punctuation
Test after each change!
Extract this
function!
What does
each part do...
Extract Another Function
Clean and Split
Any other
functions!
Extract this
too!
Test after each change!
Better
test
Doug is on a roll now!
There are a LOT
of nesting …
And this blocks
look similar
… a sign of a
function doing
too much
Common Refactoring Technique
Split Loop
1 Loop that
does 2 things
2 Loop that
do 1 thing
Doug makes the blocks identical …
These are
independent!
And replace
words with a
query
I can separate them!
… carefully splits the loop …
First split the
loop
… and extracts a function
Now extract
a function
and replace
blocks with a call
Tests pass,
But isn’t this
inefficient?
What did Iverson say about efficiency?
97% of your code
doesn’t impact overall
speed
Optimize the other
3% … after profili...
The real problem is that programmers have spent far too much
time worrying about efficiency in the wrong places and at the...
Doug’s Final Product
Better names!
A+ work for
sure!
Short
functions!
Fast even with
split loops!
Doug’s code is demonstrably better
He clearly took Iverson’s
clean code lectures to heart
His solution consists of
many small functions
with good names
And he even refactored like a pro
So does Doug get the A?
Tune in next week to find out …
(Still too much
nesting…)
(MUHAHAHA…)
.. on the next exciting episode of
Doug Does Data Sc...
Let’s Review
What are unit tests?
• Captures/maintain intended behavior
• Helpful when changing code
• Should be automated
Good names…
• Reveal intent
• Use the proper parts of speech
• Have the proper length for their scope
• Avoids disinformat...
Good names…
• Reveal intent
• Use the proper parts of speech
• Have the proper length for their scope
• Avoids disinformat...
Refactoring
What is it?
• Reorganize your code
• Break it into different
parts
• Change composition
Why use it?
• Understa...
Common Refactoring Technique
Extract Function
Common Refactoring Technique
Split Loop
1 Loop that
does 2 things
2 Loop that
do 1 thing
The DRY principle
• Don’t repeat yourself!
• Find similar code
• Make it exactly the same
• Extract a function!
Advice for teaching clean code
• Require unit tests and good names.
• Don’t just teach it, live it!
• Allow students to se...
Clean Code Resources
• These slides: https://bit.ly/2WgFIb
• Clean Code, by Robert Martin
• www.cleancoders.com, videos by...
Nächste SlideShare
Wird geladen in …5
×
Nächste SlideShare
What to Upload to SlideShare
Weiter
Herunterladen, um offline zu lesen und im Vollbildmodus anzuzeigen.

0

Teilen

Herunterladen, um offline zu lesen

teaching data science students to write clean code

Herunterladen, um offline zu lesen

teaching data science students to write clean code

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen
  • Gehören Sie zu den Ersten, denen das gefällt!

teaching data science students to write clean code

  1. 1. Teaching Data Science Students to Write Clean Code Todd Iverson, Winona State University Slides/Code: https://bit.ly/2WgFIbI
  2. 2. Episode 37 Doug Wants an A
  3. 3. Doug “Crazy Legs” Ervison “Mean” Dr. Iverson Hi! I’m Doug Grrr The Hero The Villain I NEED an A! Doug? An A? HA! His code stinks!
  4. 4. Doug will demonstrate 1. Good names 2. Small functions 3. Unit tests 4. Refactor code, specifically 1. Extract functions 2. Split loops
  5. 5. Opening Scene - The Assignment https://www.kaggle.com/c/spooky-author-identification/data Kaggle is Kool! Kaggle assignment! But Dr. Iverson is SO mean!
  6. 6. Doug’s Original code (…this assignment require unit tests!…) Iverson loves Bag of Words! I am going to get an A for sure! (…F!…)
  7. 7. It looks like our hero is doomed to an F!
  8. 8. Then just in the nick of time …
  9. 9. … Doug remembers unit tests! You MUST have unit tests.
  10. 10. What are unit tests? • Captures/maintain intended behavior • Helpful when changing code • Should be automated
  11. 11. Doug writes some unit tests Original behavior New behavior That was easy! And my code passed!
  12. 12. Doug’s Original code (…with names like that …) Remembered the Unit Tests! I am going to get an A for sure! (…a C at best…)
  13. 13. Luckily, Doug remembers to think about names Names are important! They should express intent!
  14. 14. Good names… • Reveal intent • Use the proper parts of speech • Have the proper length for their scope • Avoids disinformation and encodings Data: What is it? Function: What does it do?
  15. 15. Good names… • Reveal intent • Use the proper parts of speech • Have the proper length for their scope • Avoids disinformation and encodings Variable: Noun Function: Verb Boolean: Predicate
  16. 16. These names are bad. What’ ews again?? Doug inspects his names
  17. 17. ews hold the words for Edgar Allen Poe Maybe I should just use poe_words Doug finds some better names
  18. 18. The other names are better too! poe_words is better than ews A+ for sure! (…see the bug?…) (…D at best…)
  19. 19. It looks like our hero is doomed with D!
  20. 20. Then just in the nick of time …
  21. 21. … Doug remembers to test! Good thing I ran unit test! Better test! Oops!
  22. 22. What else would Iverson complain about?
  23. 23. Doug imagines Iverson’s feedback Clean functions do one thing You need more functions
  24. 24. and Doug even remembers refactoring! … and extract a function! Find a block that does something …
  25. 25. Common Refactoring Technique Extract Function
  26. 26. The DRY principle • Don’t repeat yourself! • Find similar code • Make it exactly the same • Extract a function!
  27. 27. Extract Functions Replace hyphens Remove punctuation Test after each change! Extract this function! What does each part do? Better test
  28. 28. Extract Another Function Clean and Split Any other functions! Extract this too! Test after each change! Better test
  29. 29. Doug is on a roll now! There are a LOT of nesting … And this blocks look similar … a sign of a function doing too much
  30. 30. Common Refactoring Technique Split Loop 1 Loop that does 2 things 2 Loop that do 1 thing
  31. 31. Doug makes the blocks identical … These are independent! And replace words with a query I can separate them!
  32. 32. … carefully splits the loop … First split the loop
  33. 33. … and extracts a function Now extract a function and replace blocks with a call Tests pass, But isn’t this inefficient?
  34. 34. What did Iverson say about efficiency? 97% of your code doesn’t impact overall speed Optimize the other 3% … after profiling Blah blah … Donald Knuth … Blah blah
  35. 35. The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming. - Donald Knuth Iverson is always talking about Knuth What a fanboy! (…he’s not wrong …)
  36. 36. Doug’s Final Product Better names! A+ work for sure! Short functions! Fast even with split loops!
  37. 37. Doug’s code is demonstrably better
  38. 38. He clearly took Iverson’s clean code lectures to heart
  39. 39. His solution consists of many small functions with good names
  40. 40. And he even refactored like a pro
  41. 41. So does Doug get the A?
  42. 42. Tune in next week to find out … (Still too much nesting…) (MUHAHAHA…) .. on the next exciting episode of Doug Does Data Science
  43. 43. Let’s Review
  44. 44. What are unit tests? • Captures/maintain intended behavior • Helpful when changing code • Should be automated
  45. 45. Good names… • Reveal intent • Use the proper parts of speech • Have the proper length for their scope • Avoids disinformation and encodings Data: What is it? Function: What does it do?
  46. 46. Good names… • Reveal intent • Use the proper parts of speech • Have the proper length for their scope • Avoids disinformation and encodings Variable: Noun Function: Verb Boolean: Predicate
  47. 47. Refactoring What is it? • Reorganize your code • Break it into different parts • Change composition Why use it? • Understand the code • Clean the code • Allow new features
  48. 48. Common Refactoring Technique Extract Function
  49. 49. Common Refactoring Technique Split Loop 1 Loop that does 2 things 2 Loop that do 1 thing
  50. 50. The DRY principle • Don’t repeat yourself! • Find similar code • Make it exactly the same • Extract a function!
  51. 51. Advice for teaching clean code • Require unit tests and good names. • Don’t just teach it, live it! • Allow students to see you clean your messy code. • Teach/reinforce important concepts. • DRY • Refactoring • Efficiency concerns and profiling • Projects that require 100’s of lines of code.
  52. 52. Clean Code Resources • These slides: https://bit.ly/2WgFIb • Clean Code, by Robert Martin • www.cleancoders.com, videos by Robert Martin and friends • Refactoring Code, by Martin Fowler

teaching data science students to write clean code

Aufrufe

Aufrufe insgesamt

51

Auf Slideshare

0

Aus Einbettungen

0

Anzahl der Einbettungen

0

Befehle

Downloads

1

Geteilt

0

Kommentare

0

Likes

0

×