think like a programmer

Download Think Like a Programmer

If you can't read please download the document

Upload: daoswald

Post on 16-Apr-2017

787 views

Category:

Technology


3 download

TRANSCRIPT

Think Like A Programmer

#UtahDigiCon#thinklikeaprogrammer

Cover Slides that didn't make the final draft...

Think Like a Programmer

Think Like a Programmer

Think Like a Programmer

Think Like a Programmer

Think Like a Programmer

Think Like a Programmer

Twitter

#UtahDigiCon

#thinklikeaprogrammer

The Twitter Disadvantage?

chomp( my @hashtags = );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

The Twitter Disadvantage!

#thinklikeaprogrammer#gamedesignroad#networkdevices#anyonecancode#girlsgonetech#startbusiness#factsoflife#pathfinding#agilegames#careercode#gamedesign#networking#makeagame#mathmyths#netduino#getrich#tweetme#dba2115

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).

Twitter

#UtahDigiCon

#thinklikeaprogrammer

Who am I?

Dave Oswald

Who am I?

Dave Oswald A Propetual Hobbiest.

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.

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.

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.

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.

[email protected]

Salt Lake Perl Mongershttp://saltlake.pm.org

Our Goal

Learn programming in one hour.

Our Goal

Learn programming in one hour.

Our Goal

Learn programming in one hour.

Sit around and tweet / text / play Temple Run for one hour.

Our Goal

Learn programming in one hour.

Sit around and tweet / text / play Temple Run for one hour.

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.

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.

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.

Learn simple thought processes useful to programmers.

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.

Learn simple thought processes useful to programmers.

Attributes

Let's try this...

The Three Virtues...

The Triumph of the VirtuesAndrea Mantegna, 1497

Justice!

Temperance!

Fortitude!

The Three Virtues...

The Triumph of the VirtuesAndrea Mantegna, 1497

Justice!

Temperance!

Fortitude!

The Three Virtues...of Programmers

The Three Virtues...of Programmers

Laziness

The Three Virtues...of Programmers

LazinessThe quality that makes you go to great effort to reduce overall energy expenditure.

The Three Virtues...of Programmers

LazinessWorking hard toavoid even morehard work in thefuture.

Impatience!

The Three Virtues...of Programmers

Laziness

Impatience

The Three Virtues...of Programmers

Laziness

ImpatienceThe anger you feel when the computer is being lazy, which happens when another programmer is not lazy.

The Three Virtues...of Programmers

Laziness

Impatience

Hubris

The Three Virtues...of Programmers

Laziness

Impatience

HubrisThe pride that makes you write and maintain programs that you can be proud of, and that your peers will admire.

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 theyre 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.

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.

[email protected]

Salt Lake Perl Mongershttp://saltlake.pm.org

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.

[email protected]

Salt Lake Perl Mongershttp://saltlake.pm.org

Aspiring to be:Lazy

Impatient

Hubristic

Mental Tools of the Trade

Abstraction

An emphasis on the idea, qualities and properties rather than the particulars (a suppression of detail).

Abstraction

An emphasis on the idea, qualities and properties rather than the particulars (a suppression of detail).

GoatSmall Livestock.

Horns.

Horizontal Pupils.

Eat almost anything.

Curious.

Used in Production of Milk, Cheese, Meat.

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.

[email protected]

Aspiring to be:Lazy

Impatient

Hubristic

All problems in computer science can be solved by another level of indirection [or abstraction]... David Wheeler

Generalization

A broadening of application to encompass a larger domain of objects of the same or different type.

Generalization

A broadening of application [or tool] to encompass a larger domain of objects of the same or different type.

Dremel ToolSpins a bit receptical really fast.

Has a hand grip.

Generalization

A broadening of application to encompass a larger domain of objects of the same or different type.

Dremel ToolSpins a bit receptical really fast.

Has a hand grip.

Not a generalized tool

A generalized tool

Substitution

To put something in the place of another.

Example: Factorial.n! = n * ( n 1 ) * * 2 * 1.

Substitution

To put something in the place of another.

Example: Factorial.n! = n * ( n 1 ) * * 2 * 1.

Substitution

To put something in the place of another.

Example: Factorial.

2! + 3!

( ( 2 * ( 2 1 ) ) + ( 3 * ( 3 1 ) * (3 2 ) )

Encapsulation

To completely enclose.

Encapsulation

To completely enclose.

Algorithms

Algorithms

Step by step procedures for solving calculations or problems.

Algorithms

Get ready for school.If alarm hasn't rung, continue to sleep.

Alarm ringsWake up.

Turn off alarm.

Pee.

Drink some water.

Undress.

ShowerLather, rinse, repeat.

Dry off.

Dress.

Comb hair.

Eat breakfast.

Brush teeth.

Algorithms

Abstraction, Generalization, and Substitution allow us to develop algorithms.

And then encapsulate them, and forget about their internals.

Number Guessing Game

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.

How would you solve it with pencil and paper?

Write down the steps

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:??????

The Blind Guess Algorithm

StepsCall 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.

The brute force algorithm.

StepsIs it 1?Higher.

Is it 2?Higher.

Is it 3?Higher.

Is it 4?Higher

Worst case: Linear.

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.

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.

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.

Putting it in perspective

A computer can do 338 guesses 100 times for each time your heart beats.

Putting it in perspective

A computer can do 338 guesses 100 times for each time your heart beats.A totally made-up statistic.

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 atomswere all lined upin order. )

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.

Generalization

Divide and Conquer number guessing can be generalized.Is oxter is in the dictionary?

Search 171,476 words in the Oxford dictionary18 divide and conquer steps.

n Outdated word meaning armpit.

Oxter is in the dictionary.

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

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.

What is a comparator?

Another black box.comparator( a, b )Return -1 if a < b

Return 1 if b < a

Otherwise return 0.

Dan < DavidD == D

a == a

n < v

7 > 5

2.71828 < 3.14151

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 );

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;}

Guessing numbers programatically

use List::BinarySearch 'binsearch';use IO::Prompt::Hooked 'prompt';

print "Please think of a number between 1 and 10...\n\n\n";

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]};

Generalization, Abstraction, Encapsulation

We generalized the Binary Search algorithm.

Generalization, Abstraction, Encapsulation

We generalized the Binary Search algorithm.It doesn't care what a target, or comparator look like inside.

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.

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).

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.

We turned the number guessing game into a Dremel tool.

A good programmer should seek opportunities to apply generalized solutions to specific problems.

A good programmer should seek to make solutions general, so that they can be reused by providing a specialization to them.

A good programmer needs to learn where and how to find these opportunities.

Laziness, Impatience, Hubris

The Virtues You Should Seek

Be LazyWork hard now to make it possible.

Be ImpatientThe status quo is never good enough

Be HubristicWrite code you can be proud of.

...and...

Love What You Do

Think Like A Programmer

#UtahDigiCon

#thinklikeaprogrammer

David Oswald

[email protected]

Salt Lake Perl Mongers

http://saltlake.pm.org

Algorithms should place as few requirements on the data they work with as practical.