Yet another building metaphor

1,269 views

Published on

Look at the value of a programming language such as JS and how it influences the way we express and organise our thoughts. At same time, express the value design over language.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,269
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Yet another building metaphor

  1. 1. Yet Another Building Metaphor factor10.com :: @aslamkhn :: aslamkhan.net
  2. 2. Aslam Khanmail at aslam.khan@factor10.com blog at http://aslamkhan.net tweet at @aslamkhn skype at aslmkhn
  3. 3. One of the first games that I wrote 10 I = INT(10 * RND(1)) 20 PRINT “GUESS MY NUMBER:” 30 INPUT J 40 IF INT(J) != I THEN GOTO 20 50 PRINT “YOU GOT IT!” 60 PRINT “PLAY AGAIN (Y/N)?” 70 INPUT Y 80 IF Y == ‘Y’ THEN GOTO 10 90 PRINT “THANKS FOR PLAYING”
  4. 4. It looks The JS version better with whitespacedo { 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);alert(“Thanks for playing”);
  5. 5. It reads better with whitespacedo { 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);alert(“Thanks for playing”);
  6. 6. What else can we do about it?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);alert(“Thanks for playing”);
  7. 7. It reads better with nicer namesdo { var mystery_number = Math.floor(Math.random()*11); do { var guessed_number = prompt(“Guess my number”); } while (guessed_number != mystery_number); alert(“You got it!); var play_again = confirm(“Play again?”);} while (play_again);alert(“Thanks for playing”);
  8. 8. Can we do better than renaming?do { var mystery_number = Math.floor(Math.random()*11); do { var guessed_number = prompt(“Guess my number”); } while (guessed_number != mystery_number); alert(“You got it!); var play_again = confirm(“Play again?”);} while (play_again);alert(“Thanks for playing”);
  9. 9. Make the intention clearerdo { keep_guessing_until_matched( mystery_number() );} while (play_again());say_thanks();
  10. 10. It’s still not good enoughdo { keep_guessing_until_matched( mystery_number() );} while (play_again());say_thanks(); Why?
  11. 11. Because it is still this design10 I = INT(10 * RND(1))20 PRINT “GUESS MY NUMBER:”30 INPUT J40 IF INT(J) != I THEN GOTO 20 And it is not a50 PRINT “YOU GOT IT!” very good60 PRINT “PLAY AGAIN (Y/N)?” design either70 INPUT Y80 IF Y == ‘Y’ THEN GOTO 1090 PRINT “THANKS FOR PLAYING”
  12. 12. How do I add a new feature?10 I = INT(10 * RND(1))20 PRINT “GUESS MY NUMBER:”30 INPUT J40 IF INT(J) != I THEN GOTO 20 And it is not a50 PRINT “YOU GOT IT!” very good60 PRINT “PLAY AGAIN (Y/N)?” design either70 INPUT Y80 IF Y == ‘Y’ THEN GOTO 1090 PRINT “THANKS FOR PLAYING”
  13. 13. How do I test it?10 I = INT(10 * RND(1))20 PRINT “GUESS MY NUMBER:”30 INPUT J40 IF INT(J) != I THEN GOTO 20 And it is not a50 PRINT “YOU GOT IT!” very good60 PRINT “PLAY AGAIN (Y/N)?” design either70 INPUT Y80 IF Y == ‘Y’ THEN GOTO 1090 PRINT “THANKS FOR PLAYING”
  14. 14. Let’s just focus on the game itselfTestCase("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()); }});
  15. 15. The game as Higher Order Functions var play = new_game(); var win = play(guess); The rest is user interface stuff
  16. 16. age Ma tter?Does Langu
  17. 17. Every powerful language has this Primitive expressions Means of combination Means of abstraction (this parallels the basics of human understanding too, btw)
  18. 18. We use these languages toexpress and organise our thoughts do { keep_guessing( mystery_number() ); } while (play_again()); say_thanks(); OR var play = new_game(); var win = play(guess);
  19. 19. We use these languages toexpress and organise our thoughts do { Build monolithic keep_guessing( mystery_number() ); } while (play_again()); static structures say_thanks(); OR var play = new_game(); var win = play(guess); Build composable dynamic structures
  20. 20. JS let’s you build both.What do you want to build? factor10.com :: @aslamkhn :: aslamkhan.net

×