Think Like a Programmer
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Think Like a Programmer

  • 216 views
Uploaded on

Talk presented to High School students at the Utah DigiForge Student Conference: "Think Like A Programmer". (Attributes: Laziness, Impatience, Hubris. And how algorithms, abstraction, and......

Talk presented to High School students at the Utah DigiForge Student Conference: "Think Like A Programmer". (Attributes: Laziness, Impatience, Hubris. And how algorithms, abstraction, and generalization will help you to achieve those attributes.)

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
216
On Slideshare
216
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
4
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Think Like A Programmer #UtahDigiCon#UtahDigiCon #thinklikeaprogrammer#thinklikeaprogrammer
  • 2. “Cover Slides” that didn't make the final draft...
  • 3. Think Like a ProgrammerThink Like a Programmer
  • 4. Think Like a ProgrammerThink Like a Programmer
  • 5. Think Like a ProgrammerThink Like a Programmer
  • 6. Think Like a ProgrammerThink Like a Programmer
  • 7. Think Like a ProgrammerThink Like a Programmer
  • 8. Think Like a ProgrammerThink Like a Programmer
  • 9. Twitter ● #UtahDigiCon ● #thinklikeaprogrammer
  • 10. The Twitter Disadvantage? chomp( my @hashtags = <DATA> ); my @by_length = sort { length $b <=> length $a || $a cmp $b } @hashtags; print "$_n" for @by_length; print length, "n" for @by_length[0,1]; __DATA__ #agilegames #startbusiness #dba #gamedesignroad #getrich #gamedesign #factsoflife #mathmyths #makeagame #careercode #pathfinding #girlsgonetech #networking #networkdevices #anyonecancode #thinklikeaprogrammer #netduino #tweetme
  • 11. The Twitter Disadvantage! #thinklikeaprogrammer #gamedesignroad #networkdevices #anyonecancode #girlsgonetech #startbusiness #factsoflife #pathfinding #agilegames #careercode #gamedesign #networking #makeagame #mathmyths #netduino #getrich #tweetme #dba 21 15
  • 12. The Twitter Disadvantage! #thinklikeaprogrammer (140 - 21 = 119) Now add in #UtahDigiCon (140 - 21 – 14 - 2 = 103) You have 103 keystrokes to say something nice... (or not).
  • 13. Twitter ● #UtahDigiCon ● #thinklikeaprogrammer
  • 14. Who am I? ● Dave Oswald
  • 15. Who am I? ● Dave Oswald – A Propetual Hobbiest.
  • 16. Who am I? ● Dave Oswald – A Propetual Hobbiest. ● Studied Economics and Computer Science at U of U. – Also CS in High School, SLCC, LAVC, and self-guided.
  • 17. Who am I? ● Dave Oswald – A Propetual Hobbiest. ● Studied Economics and Computer Science at U of U. – Also CS in High School, SLCC, LAVC, and self-guided. ● Independent software developer and consultant. – Focus on Perl, C++, and server-side development.
  • 18. Who am I? ● Dave Oswald – A Propetual Hobbiest. ● Studied Economics and Computer Science at U of U. – Also CS in High School, SLCC, LAVC, and self-guided. ● Independent software developer and consultant. – Focus on Perl, C++, and server-side development. ● Solving problems is my hobby. Surprisingly, people pay me to do it.
  • 19. Who am I? ● Dave Oswald – A Propetual Hobbiest. ● Studied Economics and Computer Science at U of U. – Also CS in High School, SLCC, LAVC, and self-guided. ● Independent software developer and consultant. – Focus on Perl, C++, and server-side development. ● Solving problems is my hobby. Surprisingly, people pay me to do it. ● daoswald@gmail.com ● Salt Lake Perl Mongers – http://saltlake.pm.org
  • 20. Our Goal ● Learn programming in one hour.
  • 21. Our Goal ● Learn programming in one hour.
  • 22. Our Goal ● Learn programming in one hour. ● Sit around and tweet / text / play Temple Run for one hour.
  • 23. Our Goal ● Learn programming in one hour. ● Sit around and tweet / text / play Temple Run for one hour.
  • 24. Our Goal ● Learn programming in one hour. ● Sit around and tweet / text / play Temple Run for one hour. ● Get out of class for four hours.
  • 25. Our Goal ● Learn programming in one hour. ● Sit around and tweet / text / play Temple Run for one hour. ● Get out of class for four hours.XXXXXXXXXXXXXXXXXXXXX
  • 26. Our Goal ● Learn programming in one hour. ● Sit around and tweet / text / play Temple Run for one hour. ● Get out of class for four hours.XXXXXXXXXXXXXXXXXXXXX ● Learn simple thought processes useful to programmers.
  • 27. Our Goal ● Learn programming in one hour. ● Sit around and tweet / text / play Temple Run for one hour. ● Get out of class for four hours.XXXXXXXXXXXXXXXXXXXXX ● Learn simple thought processesLearn simple thought processes useful to programmers.useful to programmers.
  • 28. Attributes
  • 29. Let's try this...
  • 30. The Three Virtues... ● “The Triumph of the Virtues” – Andrea Mantegna, 1497 ● Justice! ● Temperance! ● Fortitude!
  • 31. The Three Virtues... ● “The Triumph of the Virtues” – Andrea Mantegna, 1497 ● Justice! ● Temperance! ● Fortitude!
  • 32. The Three Virtues...of Programmers
  • 33. The Three Virtues...of Programmers ● Laziness
  • 34. The Three Virtues...of Programmers ● Laziness – The quality that makes you go to great effort to reduce overall energy expenditure.
  • 35. The Three Virtues...of Programmers ● Laziness – Working hard to avoid even more hard work in the future.
  • 36. Impatience!
  • 37. The Three Virtues...of Programmers ● Laziness ● Impatience
  • 38. The Three Virtues...of Programmers ● Laziness ● Impatience – The anger you feel when the computer is being lazy, which happens when another programmer is not lazy.
  • 39. The Three Virtues...of Programmers ● Laziness ● Impatience ● Hubris
  • 40. The Three Virtues...of Programmers ● Laziness ● Impatience ● Hubris – The pride that makes you write and maintain programs that you can be proud of, and that your peers will admire.
  • 41. The Three Virtues...of Programmers ● Laziness – ...makes you go to great effort to reduce your overall energy expenditure. – …makes you write robust, modular, well- documented programs so you can reuse [the effort]. ● Impatience – ...anger you feel when the computer is being lazy, which happens when another programmer is not lazy. – ...makes you write programs that use minimal code so they’re fast, efficient, and anticipate what needs to be done. ● Hubris – ...pride that makes you write and maintain programs that you and your peers will admire. – ...uncontrolled or undeserved, it can also get you in trouble.
  • 42. So... Who am I? ● Dave Oswald – Hobbiest. ● Studied Economics and Computer Science at U of U. – Also CS in High School, SLCC, LAVC, Independent. ● Independent developer and consultant. – Focus on Perl, C++, and server-side development. ● Solving problems is my hobby. Surprisingly, people pay me to do it. ● daoswald@gmail.com ● Salt Lake Perl Mongers – http://saltlake.pm.org
  • 43. I am... ● Dave Oswald – Hobbiest. ● Studied Economics and Computer Science at U of U. – Also CS in High School, SLCC, LAVC, Independent. ● Independent developer and consultant. – Focus on Perl, C++, and server-side development. ● Solving problems is my hobby. Surprisingly, people pay me to do it. ● daoswald@gmail.com ● Salt Lake Perl Mongers – http://saltlake.pm.org ● Aspiring to be: – Lazy – Impatient – Hubristic
  • 44. Mental Tools of the Trade
  • 45. Abstraction ● An emphasis on the idea, qualities and properties rather than the particulars (a suppression of detail).
  • 46. Abstraction ● An emphasis on the idea, qualities and properties rather than the particulars (a suppression of detail). ● Goat – Small Livestock. – Horns. – Horizontal Pupils. – Eat almost anything. – Curious. – Used in Production of Milk, Cheese, Meat.
  • 47. My abstraction: Dave Oswald ● Hobbiest. ● Studied Economics and Computer Science at U of U. – Also CS in High School, SLCC, LAVC, Independent. ● Independent developer and consultant. – Focus on Perl, C++, and server-side development. ● Solving problems is my hobby. Surprisingly, people pay me to do it. ● daoswald@gmail.com ● Aspiring to be: – Lazy – Impatient – Hubristic
  • 48. “All problems in computer science can be solved by another level of indirection [or abstraction]...” – David Wheeler
  • 49. Generalization ● A broadening of application to encompass a larger domain of objects of the same or different type.
  • 50. Generalization ● A broadening of application [or tool] to encompass a larger domain of objects of the same or different type. ● Dremel Tool – Spins a bit receptical really fast. – Has a hand grip.
  • 51. Generalization ● A broadening of application to encompass a larger domain of objects of the same or different type. ● Dremel Tool – Spins a bit receptical really fast. – Has a hand grip.
  • 52. Not a generalized tool
  • 53. A generalized tool
  • 54. Substitution ● To put something in the place of another. ● Example: Factorial. n! = n * ( n – 1 ) * … * 2 * 1.
  • 55. Substitution ● To put something in the place of another. ● Example: Factorial. n! = n * ( n – 1 ) * … * 2 * 1.
  • 56. Substitution ● To put something in the place of another. ● Example: Factorial. ● 2! + 3! ● ( ( 2 * ( 2 – 1 ) ) + ( 3 * ( 3 – 1 ) * (3 – 2 ) )
  • 57. Encapsulation ● To completely enclose.
  • 58. Encapsulation ● To completely enclose.
  • 59. Algorithms
  • 60. Algorithms ● Step by step procedures for solving calculations or problems.
  • 61. Algorithms ● Get ready for school. – If alarm hasn't rung, continue to sleep. – Alarm rings ● Wake up. ● Turn off alarm. – Pee. – Drink some water. – Undress. – Shower ● Lather, rinse, repeat. – Dry off. – Dress. – Comb hair. – Eat breakfast. – Brush teeth.
  • 62. Algorithms
  • 63. Abstraction, Generalization, and Substitution allow us to develop algorithms. And then encapsulate them, and forget about their internals.
  • 64. Number Guessing Game
  • 65. Rules ● I choose a random number between 1 and 100. ● You try to guess the number by asking, “Is it X?” ● I respond with either “Yes”, “Higher”, or “Lower”.
  • 66. How would you solve it with pencil and paper?How would you solve it with pencil and paper?
  • 67. Write down the steps
  • 68. Rules ● I choose a random number between 1 and 100. ● You try to guess the number by asking, “Is it X?” ● I respond with either “Yes”, “Higher”, or “Lower”. ● Step 1: – Ask is it X? ● Step 2: – ??????
  • 69. The Blind Guess Algorithm ● Steps – Call one person on the phone. Person guesses 23. ● Higher. ● Hang up. – Call another person. Ask, “Is it 19?” ● Higher. ● Hang up. ● Worst case: It could take an infinite amount of guesses. ● There's only a 63% chance that it will be guessed within 100 tries. ● There's a 1% chance that it will not be guessed even after 450 tries.
  • 70. The brute force algorithm. ● Steps – Is it 1? ● Higher. – Is it 2? ● Higher. – Is it 3? ● Higher. – Is it 4? ● Higher ● Worst case: Linear.
  • 71. Divide and Conquer ● Is it 50? – Lower ● Is it 25? – Higher ● Is it 38? – Lower. ● Each question bisects the remaining range. ● Each answer constrains the remaining range.
  • 72. Divide and Conquer ● Secret number is between 1 and 10: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] – Is it 5? ● Higher. ● The secret is between 6 and 10: [ 6, 7, 8, 9, 10 ] – Is it 8? ● Lower. ● The secret is between 6 and 7: [ 6, 7 ] – Is it 6? ● Higher. ● The secret number is between 7 and 7: [ 7 ] – Is it 7? ● Yes.
  • 73. How good is divide and conquer? ● Order of growth: – As 'n' (the size of the data set) grows toward infinity, queries needed to find an item grows at log2 (n). ● log2 (n), rounded up to nearest integer: – n = 100, guesses = 7 – n = 1,000, guesses = 10. – n = 1,000,000, guesses = 20. – n = 1,000,000,000, guesses = 30. – n = 1,000,000,000,000, guesses = 40. – n = 1 Googol (10e100, or ten duotrigintillion), guesses = 336.
  • 74. Putting it in perspective ● A computer can do 338 guesses 100 times for each time your heart beats.
  • 75. Putting it in perspective ● A computer can do 338 guesses 100 times for each time your heart beats. – A totally made-up statistic.
  • 76. Putting it in perspective ● A computer can do 338 guesses 100 times for each time your heart beats. – A totally made-up statistic. ● 1.33e80 atoms in the universe. – 266 guesses. – ( If only the atoms were all lined up in order. )
  • 77. Generalization ● “Divide and Conquer” number guessing can be generalized. – Is “oxter” is in the dictionary? – Search 171,476 words in the Oxford dictionary. ● 18 divide and conquer steps.
  • 78. Generalization ● “Divide and Conquer” number guessing can be generalized. – Is “oxter” is in the dictionary? – Search 171,476 words in the Oxford dictionary ● 18 divide and conquer steps. ● n – Outdated word meaning “armpit”. ● “Oxter” is in the dictionary.
  • 79. Generalization ● “Divide and Conquer” number guessing can be generalized. – Find if “oxter” is in the dictionary. – Find if we have a 19 year old on the roster. – Find the address from John Doe's directory listing. – ...and about a million other uses. ● “Binary Search”
  • 80. A Generalized Binary Search ● found = binary_search( [target], [big_list], [comparator] ) ● What does our black-box Binary Search need? – Something to find: a target. – Somewhere to find it: a big list. – Some means of comparing items: a comparator.
  • 81. What is a comparator? ● Another black box. – comparator( a, b ) ● Return -1 if a < b ● Return 1 if b < a ● Otherwise return 0. ● Dan < David – D == D – a == a – n < v ● 7 > 5 ● 2.71828 < 3.14151
  • 82. A Generalized Binary Search ● found = binary_search( [target], [big_list], [how_to_compare] ) ● What does our black-box Binary Search need? – Something to find: a target. – Somewhere to find it: a sorted big list. – Some means of comparing items: a comparator. ● An item comparer black-box. sub comparator {   my( $a, $b ) = ( shift, shift );   return $a <=> $b; # ( returns ­1, 0, or 1 ) } my $found    = bsearch( $target, @list, &comparator );
  • 83. A Generalized Binary Search Implementation sub binsearch (&$@) { my ( $code, $target, $aref ) = @_; my $min = 0; my $max = $#{$aref}; while ( $max > $min ) { my $mid = int( ( $min + $max ) / 2 ); no strict 'refs'; ## no critic(strict) local ( ${caller() . '::a'}, ${caller() . '::b'} ) = ( $target, $aref->[$mid] ); if ( $code->( $target, $aref->[$mid] ) > 0 ) { $min = $mid + 1; } else { $max = $mid; } } { no strict 'refs'; ## no critic(strict) local ( ${caller() . '::a'}, ${caller() . '::b'} ) = ( $target, $aref->[$min] ); return $min if $code->( $target, $aref->[$min] ) == 0; } return; }
  • 84. Guessing numbers programatically use List::BinarySearch 'binsearch'; use IO::Prompt::Hooked 'prompt'; print "Please think of a number between 1 and 10...nnn"; my $tries = 0; binsearch { ++$tries; my $response = prompt( message => "Is the number $b? (higher, lower, or yes):", validate => qr/higher|lower|yes/i, error => sub { "Bad input: $_[0] .Try again.n" }, ); return 1 if $response =~ /higher/i; return -1 if $response =~ /lower/i; print "Found $b in $tries guesses.n"; exit(0); } undef, @{[1 .. 10]};
  • 85. Generalization, Abstraction, Encapsulation ● We generalized the Binary Search algorithm.
  • 86. Generalization, Abstraction, Encapsulation ● We generalized the Binary Search algorithm. – It doesn't care what a target, or comparator look like inside.
  • 87. Generalization, Abstraction, Encapsulation ● We generalized the Binary Search algorithm. – It doesn't care what a target, or comparator look like inside. ● Our target and list items are abstractions on their underlying characteristics.
  • 88. Generalization, Abstraction, Encapsulation ● We generalized the Binary Search algorithm. – It doesn't care what a target, or comparator look like inside. ● Our target and list items are abstractions on their underlying characteristics. ● Our comparator specializes our Binary Search algorithm. – Black box (encapsulation of functionality).
  • 89. Generalization, Abstraction, Encapsulation ● We generalized the Binary Search algorithm. – It doesn't care what a target, or comparator look like inside. ● Our target and list items are abstractions on their underlying characteristics. ● Our comparator specializes our Binary Search algorithm. – Black box (encapsulation of functionality). ● We can use it anywhere. – Just provide a target, a list, and a means to compare. – Never re-invent it again. – Fewer bugs, through code re-use.
  • 90. We turned the number guessing game into a Dremel tool.
  • 91. A good programmer should seek opportunities to apply generalized solutions to specific problems.
  • 92. A good programmer should seek to make solutions general, so that they can be reused by providing a specialization to them.
  • 93. A good programmer needs to learn where and how to find these opportunities.
  • 94. Laziness, Impatience, Hubris
  • 95. The Virtues You Should Seek ● Be Lazy – Work hard now to make it possible. ● Be Impatient – The status quo is never good enough ● Be Hubristic – Write code you can be proud of.
  • 96. ...and...
  • 97. Love What You Do
  • 98. Think Like A Programmer ● #UtahDigiCon ● #thinklikeaprogrammer ● David Oswald ● daoswald@gmail.com ● Salt Lake Perl Mongers ● http://saltlake.pm.org
  • 99. Algorithms should place as few requirements on the data they work with as practical.