20
Yet Another Building Metaphor factor10.com :: @aslamkhn :: aslamkhan.net

Yet another building metaphor

Embed Size (px)

Citation preview

Page 1: Yet another building metaphor

Yet Another Building Metaphorfactor10.com :: @aslamkhn :: aslamkhan.net

Page 2: Yet another building metaphor

Aslam Khan

mail at [email protected]

blog at http://aslamkhan.net

tweet at @aslamkhn

skype at aslmkhn

Page 3: Yet another building metaphor

10 I = INT(10 * RND(1))20 PRINT “GUESS MY NUMBER:”30 INPUT J40 IF INT(J) != I THEN GOTO 2050 PRINT “YOU GOT IT!”60 PRINT “PLAY AGAIN (Y/N)?”70 INPUT Y80 IF Y == ‘Y’ THEN GOTO 1090 PRINT “THANKS FOR PLAYING”

One of the first games that I wrote

Page 4: Yet another building metaphor

alert(“Thanks for playing”);

It looks better with whitespace

do { var i = Math.floor(Math.random()*11);

do { var j = prompt(“Guess my number”); } while (i != j);

alert(“You got it!);y = confirm(“Play again?”);

} while (y);

The JS version

Page 5: Yet another building metaphor

alert(“Thanks for playing”);

do { var i = Math.floor(Math.random()*11);

do { var j = prompt(“Guess my number”); } while (i != j);

alert(“You got it!);

y = confirm(“Play again?”);} while (y);

It reads better with whitespace

Page 6: Yet another building metaphor

What else can we do about it?

alert(“Thanks for playing”);

do { var i = Math.floor(Math.random()*11);

do { var j = prompt(“Guess my number”); } while (i != j);

alert(“You got it!);

y = confirm(“Play again?”);} while (y);

Page 7: Yet another building metaphor

do { do { } while alert(“You got it!);

alert(“Thanks for playing”);

var mystery_number = Math.floor(Math.random()*11);

var guessed_number = prompt(“Guess my number”);(guessed_number != mystery_number);

var play_again = confirm(“Play again?”);} while (play_again);

It reads better with nicer names

Page 8: Yet another building metaphor

Can we do better than renaming?

do { do { } while alert(“You got it!);

alert(“Thanks for playing”);

var mystery_number = Math.floor(Math.random()*11);

var guessed_number = prompt(“Guess my number”);(guessed_number != mystery_number);

var play_again = confirm(“Play again?”);} while (play_again);

Page 9: Yet another building metaphor

Make the intention clearer

keep_guessing_until_matched( mystery_number() );(play_again());

say_thanks();

do { } while

Page 10: Yet another building metaphor

do {

keep_guessing_until_matched( mystery_number() ); } while (play_again());

say_thanks();

It’s still not good enough

Why?

Page 11: Yet another building metaphor

Because it is still this design

10 I = INT(10 * RND(1))20 PRINT “GUESS MY NUMBER:”30 INPUT J40 IF INT(J) != I THEN GOTO 2050 PRINT “YOU GOT IT!”60 PRINT “PLAY AGAIN (Y/N)?”70 INPUT Y80 IF Y == ‘Y’ THEN GOTO 1090 PRINT “THANKS FOR PLAYING”

And it is not a very good

design either

Page 12: Yet another building metaphor

10 I = INT(10 * RND(1))20 PRINT “GUESS MY NUMBER:”30 INPUT J40 IF INT(J) != I THEN GOTO 2050 PRINT “YOU GOT IT!”60 PRINT “PLAY AGAIN (Y/N)?”70 INPUT Y80 IF Y == ‘Y’ THEN GOTO 1090 PRINT “THANKS FOR PLAYING”

And it is not a very good

design either

How do I add a new feature?

Page 13: Yet another building metaphor

10 I = INT(10 * RND(1))20 PRINT “GUESS MY NUMBER:”30 INPUT J40 IF INT(J) != I THEN GOTO 2050 PRINT “YOU GOT IT!”60 PRINT “PLAY AGAIN (Y/N)?”70 INPUT Y80 IF Y == ‘Y’ THEN GOTO 1090 PRINT “THANKS FOR PLAYING”

And it is not a very good

design either

How do I test it?

Page 14: Yet another building metaphor

Let’s just focus on the game itself

TestCase("Game Tests", {"test new game should let us play": function() { var play = new_game(); assertFunction(play); },

"test play should let us check for a win": function() { var play = new_game(); var win = play(5); assertFunction(win); },

"test winning play": function() { var play = new_game(); var guess = play['mystery_number']; var win = play(guess); assertTrue(win());}

});

Page 15: Yet another building metaphor

The game as Higher Order Functions

var play = new_game();var win = play(guess);

The rest is user interface stuff

Page 16: Yet another building metaphor

Does Language Matter?

Page 17: Yet another building metaphor

Every powerful language has this

Primitive expressions

Means of combination

Means of abstraction

(this parallels the basics of human understanding too, btw)

Page 18: Yet another building metaphor

We use these languages to express and organise our thoughts

do {keep_guessing( mystery_number() );

} while (play_again());say_thanks();

OR

var play = new_game();var win = play(guess);

Page 19: Yet another building metaphor

We use these languages to express and organise our thoughts

do {keep_guessing( mystery_number() );

} while (play_again());say_thanks();

OR

var play = new_game();var win = play(guess);

Build monolithic static structures

Build composable dynamic structures

Page 20: Yet another building metaphor

JS let’s you build both.What do you want to build?

factor10.com :: @aslamkhn :: aslamkhan.net