Your SlideShare is downloading. ×
  • Like
Maintainable Javascript carsonified
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Maintainable Javascript carsonified

  • 4,990 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
4,990
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
43
Comments
1
Likes
13

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. Maintainable JavaScript Chris&an Heilmann Carsonfied online conference, September 2010
  • 2. JavaScript is awesome!
  • 3. Nowadays writing JavaScript is wonderfully easy.
  • 4. Libraries patch the support holes in browsers.
  • 5. Pure JavaScript environments allow us to really play with the language.
  • 6. JavaScript is dead easy to learn and the first steps are already very rewarding.
  • 7. This is also the problem of JavaScript.
  • 8. The web is not a closed environment where we can dictate the technologies people use.
  • 9. Some of the things I will talk about today will seem outdated or overly cautious.
  • 10. The reason is that I want to build a web that works.
  • 11. For far too long we have built a mess that barely functions and is hard to change.
  • 12. So here are a few ideas and concepts you should follow if you write JavaScript.
  • 13. ★ Using, not abusing libraries ★ Separation of concerns ★ Building for extensibility ★ Documenting your work ★ Planning for performance ★ Avoiding double maintenance ★ Live code vs. development code
  • 14. Libraries fix browsers and make the complex simple.
  • 15. They make web development predictable.
  • 16. Building without libraries means constant catch-up with the browser market.
  • 17. Do not mix and match libraries though.
  • 18. Stick with one and use it to its strengths.
  • 19. If the library totally re-invents JS as we know it, this is fine.
  • 20. But: writing in an abstraction syntax is not writing JavaScript.
  • 21. Quick two-liners do not replace architecting and planning.
  • 22. Professional libraries come as a pick and mix solution.
  • 23. Include what you need and when you need it - not the kitchen sink approach.
  • 24. One big danger of libraries is to tie the markup and your scripts far too close to each other.
  • 25. Small looking loops can actually be very slow.
  • 26. Long CSS selectors are dangerous.
  • 27. Pick your plugins by how well they are documented, how well supported and how easy they are to extend.
  • 28. Not by how flashy they are.
  • 29. ★ Using, not abusing libraries ★ Separation of concerns ★ Building for extensibility ★ Documenting your work ★ Planning for performance ★ Avoiding double maintenance ★ Live code vs. development code
  • 30. This is old news.
  • 31. Take each technology on the web and use it for what it was meant to.
  • 32. HTML that works without JavaScript should not be aded using JavaScript.
  • 33. HTML that only makes sense when JavaScript is available should be added with JavaScript.
  • 34. Instructions that describe functionality that is dependent on JS should also be added by it.
  • 35. Text, classes and ID names are very much prone to change.
  • 36. So don’t spread them all over your scripts but keep them in one place for maintenance.
  • 37. Public configuration objects are a great idea.
  • 38. Most underused element: <button>
  • 39. Buttons by definition are there to trigger script functionality - so use them instead of links.
  • 40. Links should point to a real resource - a url, a server endpoint or an anchor.
  • 41. Empty links, void(0) # and other hacks don’t make any sense.
  • 42. Styling should be done in CSS, not in your JavaScript.
  • 43. Calculated positions are of course the exception to that rule.
  • 44. Adding and removing classes makes sure designers have a handle and you don’t have to worry.
  • 45. By adding a class to a parent element you can swiftly hide a lot of elements you’d otherwise have to loop over.
  • 46. Adding a “js” class to the body means CSS designers can define two views easily.
  • 47. Make maintainers not have to know JS or change yours!
  • 48. ★ Using, not abusing libraries ★ Separation of concerns ★ Building for extensibility ★ Documenting your work ★ Planning for performance ★ Avoiding double maintenance ★ Live code vs. development code
  • 49. Using libraries means using a lot of anonymous functions.
  • 50. Most of the time at the end of a massive chain of methods or for every click().
  • 51. Naming and calling these methods makes more sense as you create a single space to maintain.
  • 52. Never think your script is done and you thought of all use cases.
  • 53. This is why we have dozens of lightbox plugins in jQuery all doing almost the same thing.
  • 54. Instead of building a “one size fits all” solution, split it up into small solutions that do one thing and allow for mixing and matching.
  • 55. Think about firing off custom events at interesting moments.
  • 56. This allows people who want to extend your solution to do so without having to touch the main code.
  • 57. Don’t limit anything to a fixed size or amount. Instead make it a variable in the configuration object.
  • 58. ★ Using, not abusing libraries ★ Separation of concerns ★ Building for extensibility ★ Documenting your work ★ Planning for performance ★ Avoiding double maintenance ★ Live code vs. development code
  • 59. Documentation is what we rely on when we get lost.
  • 60. People think documentation replaces good code examples.
  • 61. The issue is that as developers, we almost never start with the documentation.
  • 62. Instead we dive into the code and mess about with it.
  • 63. We read the documentation when we get stuck.
  • 64. Which is why code comments and descriptive variable and function names make your code easy to maintain.
  • 65. Comment the special case, not the obvious.
  • 66. Keep function and variable names short and to the point.
  • 67. Your documentation should explain applying and extending the code, not the code itself.
  • 68. Don’t worry about the size of comments - a good process deals with that.
  • 69. GitHub and Google Code is the new View-Source, so add value, not only solutions.
  • 70. ★ Using, not abusing libraries ★ Separation of concerns ★ Building for extensibility ★ Documenting your work ★ Planning for performance ★ Avoiding double maintenance ★ Live code vs. development code
  • 71. You can find a lot of great performance tricks on the web when it comes to JS.
  • 72. Read those with the use case in mind.
  • 73. Tricks necessary to make Google Mail on mobiles run smooth are edge cases.
  • 74. Performance is a specialist topic and can be handled a lot in build processes.
  • 75. If a performance change means re- educating a whole team of developers it is probably not worth it.
  • 76. Scripts can convert and change code written in a predictable fashion.
  • 77. Hacks and shortcuts need to be fixed by humans.
  • 78. The performance of JS itself is not really the issue you should be concentrating on.
  • 79. Slowness of the DOM and reflow of the interface is where users get hurt.
  • 80. So use DOM access sparingly.
  • 81. Assemble HTML as as string and use innerHTML once instead of adding to it.
  • 82. Use Event Delegation instead of hundreds of event handlers.
  • 83. Store information in JS objects instead of HTML attributes.
  • 84. Make your code easy to understand and clean and add a performance review and refactoring step at the end.
  • 85. ★ Using, not abusing libraries ★ Separation of concerns ★ Building for extensibility ★ Documenting your work ★ Planning for performance ★ Avoiding double maintenance ★ Live code vs. development code
  • 86. Double maintenance of code is a bad idea.
  • 87. Validation rules might get out of sync.
  • 88. This is always the killer argument of enemies of progressive enhancement.
  • 89. “I spend a lot of time doing form validation in JavaScript - why should I repeat the same on the server side? “
  • 90. Because there is no security in JavaScript!
  • 91. If all you do is validate in JS, attackers will have a field day with your server.
  • 92. Besides, there is no need to repeat validation rules.
  • 93. That way you validate on the server and you can use the same rules for your JS...
  • 94. Form validation scripts are annoying.
  • 95. You need to access the right parts, read and write from the DOM, change styles and and and...
  • 96. You can leave it all to the server and still save your users a full page reload.
  • 97. Request type switching is the answer.
  • 98. JavaScript libraries add a special footprint to the server request when calling content via Ajax.
  • 99. (so should your own Ajax solutions, btw)
  • 100. You can use this to send content to Ajax requests and other content to normal requests.
  • 101. You can use this to render a form completely server side and just send a string back for each request. http://github.com/codepo8/validationdemo
  • 102. Filter inputs for nasties and include the rules.
  • 103. If the form has not been submitted, include the form code.
  • 104. this is like /pattern/.test($(name).value) Otherwise loop through the rules and check the data that was sent against them.
  • 105. If there was an error - show the form again. Otherwise say thanks.
  • 106. The form itself is a simple HTML form doing all the dynamic rendering with PHP...
  • 107. Check for the error array and if there is an error, show it. Otherwise show a * to indicate required field.
  • 108. Check if the form was sent via Ajax - if not, render the form element.
  • 109. If there was an error, say so. At the field show the error or the * SPAN.
  • 110. And all you then need to do in JavaScript is to override the form submission.
  • 111. And instead replace the innerHTML of the form on every submit.
  • 112. An example using YUI3...
  • 113. On submission of the form load the validate.php file with the config and don’t send off the form. Load the IO and Node module. Define the configuration for the Ajax call.
  • 114. If the Ajax call was a success, replace the innerHTML of the form with the HTML returned from validate.php
  • 115. In addition to that, focus on the first element with an error message - this helps with assistive technology.
  • 116. If the Ajax call failed, send the form.
  • 117. Subscribe to the Ajax events.
  • 118. Writing lots of HTML with JavaScript is painful.
  • 119. So if you can avoid it, avoid it :)
  • 120. You can take this concept further.
  • 121. Make the backend render HTML to use with include() in PHP and load the same with Ajax.
  • 122. http://isithackday.com/hacks/flickrcollector/ http://github.com/codepo8/flickrcollector
  • 123. Using Node.js you can move that later on to a pure JavaScript solution.
  • 124. http://www.yuiblog.com/blog/2010/04/09/node-js- yui-3-dom-manipulation-oh-my/
  • 125. ★ Using, not abusing libraries ★ Separation of concerns ★ Building for extensibility ★ Documenting your work ★ Planning for performance ★ Avoiding double maintenance ★ Live code vs. development code
  • 126. Live code is not development code.
  • 127. If you really get to a high traffic level with your scripts you should have a build process.
  • 128. A build process should do a few things for you.
  • 129. ★ Remove comments ★ Remove whitespace ★ Bundle lots of files into one ★ Pack the code ★ Version, add the author and date. ★ Validate + Lint
  • 130. http://jslint.com/ http://blog.macromates.com/2007/javascript-tools/ http://closure-compiler.appspot.com/home http://code.google.com/closure/compiler/docs/ gettingstarted_api.html http://developer.yahoo.com/yui/compressor/ http://yuiblog.com/blog/2008/02/11/helping-the-yui- compressor Tools http://refresh-sf.com/yui/ http://www.ejeliot.com/blog/73
  • 131. Some examples of great practices in the wild.
  • 132. Give browsers what they can do and use what they do better!
  • 133. Easing the use of web fonts for better typography.
  • 134. http://code.google.com/webfonts/preview#font-family=Lobster
  • 135. Simply adding a link doesn’t give you feedback though...
  • 136. Using JS to load the fonts on the other hand does.
  • 137. Classes added to the root element by the Google WebFont loader .wf-inactive .wf-active .wf-tangerine-n4-inactive .wf-tangerine-n7-active .wf-droidsans-n4-inactive [...] n4 - normal i4 - italic n7 - bold i7 - bold italic http://code.google.com/apis/webfonts/docs/webfont_loader.html
  • 138. <style type="text/css"> .wf-inactive p { font-family: serif; font-size:12px; } .wf-active p { font-family: 'Tangerine', serif; font-size:20px; } .wf-inactive h1 { font-family: serif; font-size: 16px } .wf-active h1 { font-family: 'Cantarell', serif; font-size: 35px } </style> http://code.google.com/apis/webfonts/docs/webfont_loader.html
  • 139. WebFontConfig = { google: { families: [ 'Tangerine', 'Cantarell' ] }, typekit: { id: 'myKitId' }, loading: function() { }, fontloading: function(family, info) {}, fontactive: function(family, info) {}, fontinactive: function(family, info) {}, active: function() {}, inactive: function() {} };
  • 140. And that’s that. A lot about writing maintainable JavaScript is to avoid writing it yourself :)
  • 141. Christian Heilmann http://wait-till-i.com Thanks! http://developer-evangelism.com http://twitter.com/codepo8