Lessons from a coding veteran - Web Directions @Media

3,487 views
3,402 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,487
On SlideShare
0
From Embeds
0
Number of Embeds
720
Actions
Shares
0
Downloads
25
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lessons from a coding veteran - Web Directions @Media

  1. 1. LESSONS FROM A CODING VETERAN
  2. 2. TOM HUGHES-CROUCHER @sh1mmer
  3. 3. Stuff to be “proud of ”BSc 1st Class (Hons) Computer Scalable Server-Side Code with JavaScriptScienceHelped write two W3C standardsWritten code for: NASA Tesco Channel 4 Node Up and Running Three Telecom Tom Hughes-Croucher Yahoo!
  4. 4. 3 RULES TO LIVE BY
  5. 5. RULE 1.COMPLEXITY IS THE ENEMY
  6. 6. WHAT IS COMPLEXITY?
  7. 7. COMPLEXITY IS...
  8. 8. ...TOO MANY FEATURES
  9. 9. ...UNINTELLIGIBLE CODE
  10. 10. ...CODE THAT DOES THINGS MORE THAN ONE WAY
  11. 11. ...TOOMUCHCODETHATJUSTGOESONANDONANDRUNSTOGETHER
  12. 12. COMPLEXITY IS...
  13. 13. ...ANYTHING THATREDUCES UNDERSTANDING
  14. 14. WHY IS UNDERSTANDING SO IMPORTANT?
  15. 15. “BUILD TWITTER”
  16. 16. What is Twitter?140 character micro-blogging serviceAJAX web siteAPIScalability (sort of)etc
  17. 17. “BUILD TWITTER” IS NOT UNDERSTANDABLE
  18. 18. COMPUTERS REQUIREPERFECT INSTRUCTIONS
  19. 19. PERFECTION REQUIRES UNDERSTANDING
  20. 20. RULE 1A.HAVE A CLEAR GOAL
  21. 21. TANGIBLE OBJECTIVE
  22. 22. IT IS YOUR JOB AS DEVELOPERTO ACCEPT THE REQUIREMENTS
  23. 23. CLEAR OUTCOME
  24. 24. SUCCESS = ??
  25. 25. A WAY TO MEASURE SUCCESS
  26. 26. TDD
  27. 27. PUBLIC API
  28. 28. AGILE ALLOWS YOU TO FAIL FAST
  29. 29. FAILING FAST ALLOWSREDUCE COST OF DEFINITION
  30. 30. RULE 1B.WRITE CODE YOU* CAN UNDERSTAND * and your team
  31. 31. function(a,b,c){return{pub:function(d,e){for(c in a)if(c.split("-")[0]==d)a[c](e)!==!1||delete a[c]},sub:function(d,e){a[d+--b]=e}}}({},0)
  32. 32. function(  a, // the subscription object  b, // the current event number  c // (placeholder)){  return { // return an object, with    pub: function( // a "pub" function that takes      d, // a channel name and      e // a message value,    ){ // and      for( // for each        c // subscriber        in a // in the subscription object,      ) if ( // if        c // its name, with the event number discarded          .split // by splitting it          ("-") // with a hyphen and          [0] // taking only the first part,          == d // is equal to the published channel      ) a[c](e) // call the subscriber. and if        !== !1 || // the value returned is false        delete a[c] // unsubscribe by removing the subscriber, and    },        sub: function( // a "sub" function that takes      d, // a channel name and      e // a subscriber function    ){ // and,      a[ // on the subscriptions object,        d + // sets the name of the channel appended with        --b // a hyphen and the decremented event number, to      ] = e // the function passed.    }  }}( // auto-run with  {}, // the subscription object  0 // and event id.)
  33. 33. LIKE “REAL” CODE
  34. 34. TERSE != BETTER
  35. 35. LANGUAGES ARE EXPRESSIVE
  36. 36. //this is JavaScriptvar isTrueif (true) { isTrue = true}
  37. 37. //still JavaScriptvar isTrue = true ? true : undefined
  38. 38. ANOTHER EXAMPLE
  39. 39. var c = Math.floor(b)
  40. 40. var c = ~~b
  41. 41. YOUR BRAIN HAS MORE TO REMEMBER
  42. 42. FRENCH VS. ENGLISH
  43. 43. THERE ARE MANY REALPATTERNS TO REMEMBER
  44. 44. DON’T ADD SYNONYMOUS PATTERNS
  45. 45. RULE 1C.PICK SOME CONVENTIONS
  46. 46. BE OPINIONATEDAS LONG AS YOU STICK TO IT
  47. 47. PLACE { AFTER )
  48. 48. function buildObjectLiteral(){   return // this returns!   {      "someProperty": "hello"   };}
  49. 49. STUPID ERROR
  50. 50. STUPID CODE
  51. 51. IF YOU PICK A GOOD STYLE YOU WON’T WRITE STUPID CODE
  52. 52. SEMI-COLON FIRST JAVASCRIPT
  53. 53. // return whatever was printedfunction showFields (data, version, fields) { var o = {} ;[data,version].forEach(function (s) { Object.keys(s).forEach(function (k) { o[k] = s[k] }) }) return search(o, fields.split("."), version._id, fields)}
  54. 54. YOU CAN PICK ALL KINDS OF STYLES
  55. 55. MOST IMPORTANT IS CONSISTENCY
  56. 56. RULE 1D.USE ABSTRACTION WISELY
  57. 57. SIMPLE VS COMPLEX
  58. 58. Browser Server
  59. 59. ABSTRACTION
  60. 60. GETBrowser Server 200 OK
  61. 61. TCP (Slow start)Browser Server
  62. 62. ABSTRACTION AIDES UNDERSTANDING
  63. 63. ABSTRACTION MAKES THINGS SIMPLE
  64. 64. WHEN DOING COMPLEX THINGS ABSTRACTION MAKE THINGS MORE COMPLEX
  65. 65. //node.js style JavaScriptquery = mysql.query(SELECT * FROM data);query.on(‘result’, function(result) { for(var i=0;i<length;i++) { var row = result.rows[i]; //do something with the row }});
  66. 66. //threaded style JavaScriptresult = mysql.query(SELECT * FROM data);for(var i=0;i<length;i++) { var row = result.rows[i]; //do something with the row}
  67. 67. var x = "I am a string"~1ns Running 1 instruction2ns Data from l1 cpu cache5ns Data from l2 cpu cache80ns Data from ram
  68. 68. queryServer mySQL result
  69. 69. result = mysql.query(SELECT * FROM data);~100ms Time to run a query in database50ms Time to roundtrip query over the network
  70. 70. LOCAL VARIABLE
  71. 71. REMOTE OPERATION
  72. 72. 100 Blue Whales Cat
  73. 73. TOO MUCH ABSTRACTION
  74. 74. USE ABSTRACTION WELL
  75. 75. MODULARISATION
  76. 76. BLACK BOXES ARE EASY TO UNDERSTAND
  77. 77. DRIVING A CAR ISN’TABOUT COMBUSTION
  78. 78. WELL DEFINED APISPROVIDE ABSTRACTION
  79. 79. RULE 2.DONT OPTIMISE TOO SOON
  80. 80. OPTIMISATION ISN’T JUST ABOUT PERFORMANCE
  81. 81. RULE 2A.1ST DRAFT, 2ND DRAFT
  82. 82. YOU AREN’T THEKWISATZ HADERACH
  83. 83. YOU HAVE TO WRITE CODETO LEARN WHAT YOU NEED
  84. 84. THE FIRST VERSION ALWAYS SUCKS
  85. 85. BACK TO AGILEDESIGNED TO “FAIL”
  86. 86. BUILD IN TIME TO REWRITE YOUR CODE
  87. 87. ESTIMATE+ 1/3 + 1/4
  88. 88. RULE 2B. WRITING COMPLEX CODE ISOK WHEN YOU UNDERSTAND IT
  89. 89. RULE 2B. WRITING COMPLEX CODE ISOK WHEN YOU UNDERSTAND IT
  90. 90. RULE 2B.REWRITING CODE TO BE MORE COMPLEX IS OK WHEN YOU UNDERSTAND IT
  91. 91. SPEED
  92. 92. Math.floor(3.141) //3~~3.141 //3 but faster
  93. 93. typeof foo === number && !isNaN(foo) && foo !== Infinity ? foo > 0 ? Math.floor(foo) : Math.ceil(foo) : 0;// ~~var in JavaScript explained by James Padosley// side note (this is why I, personally, dislike ? )
  94. 94. ~~VAR IS LIKE MATH.FLOOR FOR +INTEGER
  95. 95. RELIABILITY
  96. 96. RULE 2C.DOCUMENT THE HECK OUT OF OPTIMISATIONS
  97. 97. // Using ~~ to floor and generate a random #1-6(~~(Math.random()*6))+1
  98. 98. RULE 2D.OPTIMISE WITH TOOLS IF POSSIBLE
  99. 99. JSLINT
  100. 100. YUI/DOJO COMPRESSOR CLOSURE COMPILER
  101. 101. CLANG
  102. 102. RULE 3.ALL RULES ARE MADE TO BE BROKEN
  103. 103. WE ALL WORK FOR BUSINESSES
  104. 104. REALITY BITES
  105. 105. DEAL WITH IT
  106. 106. RULE 3A.IT’S OK TO WRITE SHITTY CODE FOR A DEADLINE
  107. 107. YOU HAD A DEADLINE
  108. 108. YOU DRANK TOO MUCH COFFEE
  109. 109. STAYED UP ALL NIGHT
  110. 110. YOU UPSET YOUR[GIRL/BOY]FRIEND
  111. 111. THE CODE YOU WROTE SUCKED
  112. 112. SUUUUUUUUUUCCCKKKEED!
  113. 113. BUT IT WORKED
  114. 114. SO. COOL.
  115. 115. RULE 3B.BREAKING RULES INVOLVES CLEANUP
  116. 116. SOMEONE* WILL HAVE TO MAINTAIN THAT CODE * maybe you
  117. 117. SHORTLY AFTER YOU WROTE BAD CODE
  118. 118. YOU WILL...
  119. 119.
  120. 120. ...WHAT DOES THIS DO?
  121. 121. CLEAN UP CODE ASAP
  122. 122. SPRINT 1 : CODE THAT SUCKSSPRINT 2 : CODE THAT DOESN’T
  123. 123. RULE 3C.RULE BREAKING ISN’T SUSTAINABLE
  124. 124. IT’S CALLEDTECHNICAL DEBT
  125. 125. EVERY OPEN SOURCEPROJECT YOU FORK
  126. 126. EVERY FUNCTION THAT ISN’T DOCUMENTED
  127. 127. EVERY LINE THAT WAS ONLY WRITTEN ONCE
  128. 128. TECHNICAL DEBT IS LIKE OTHER DEBT
  129. 129. THE MORE OF IT YOU HAVE THE HARDER IT IS TO FIX
  130. 130. RULE 3D.IF YOU KEEP CHEATING GTFO
  131. 131. ENVIRONMENTS THAT DON’T CARE ABOUT GOOD CODE
  132. 132. DON’T CARE ABOUT YOU!
  133. 133. STRESS IS NOT GOOD FOR LIFE
  134. 134. CODING WITH RULE 1 AND RULE 2 IS CODING FOR THE FUTURE
  135. 135. CODING WITH RULE 3 IS CODING FOR NOW
  136. 136. SHORT-TERM THINKING DOESN’T YIELD LONG-TERM RESULTS
  137. 137. Summary1. Complexity is the enemy 2. Dont optimise too soon1a. Have a clear goal 2a. 1st draft, 2nd draft1b. Write code you can 2b. rewriting code to be moreunderstand complex is ok when you understand it1c. Pick some conventions 2c. Document the heck out of1d. Use abstraction wisely optimisations 2d. optimise with tools if possible
  138. 138. QUESTIONS?@sh1mmer

×