thingsinjars

  • 4 Aug 2011

    You must be able to read before you get a library card

    I like JavaScript. JS. ECMAScript. Ol' Jay Scrizzle as nobody calls it.

    I also like jQuery. jQ. jQuizzie. jamiroQuery. Whatever.

    Ignoring the stoopid nicknames I just made up, did you notice how I referred to JavaScript and jQuery separately? I didn't say "I like JavaScript, there are some really great lightbox plugins for it" just the same as I didn't say "I wish there was a way to do indexOf in jQuery".

    I'm regularly amazed at how many new (and some not-so-new) web developers either think they know JavaScript because they know jQuery or wish there was a way to do something in jQuery that they read about in an article about JavaScript. jQuery is a library written to make coding in JavaScript easier. It's made in JavaScript so you can say "jQuery is JavaScript" but only in the same way that "Simon is male". To confuse jQuery as all of JavaScript is the same as saying "Simon is all men" (don't worry, there's still only one of me).

    For most web site or web app development, I do recommend using a library. Personally, I've used jQuery and Prototype extensively and decided I prefer jQuery. Libraries are designed to make coding faster and more intuitive and they can be a great productivity aid. You can get a lot more done quicker. There is a downside, however.

    Downside

    If you're doing what the library was intended to help with, great. Slide this panel up, pop open a modal window, scroll to the bottom of the page and highlight that header. Brilliant. The difficulties come when you're either trying to do something the library wasn't intended to do or something nobody's thought of before or you're just plain doing something wrong. If you are fluent in your library of choice but don't know the JavaScript underpinnings, your usual debugging tools can only help you so far. There will come a point where there's an impenetrable black-box where data goes in and something unexpected comes out. Okay, it's probably still data but it's unexpected data.

    Don't let this point in the process be the discouragement. This is where the fun bit is.

    Learning to read

    Library authors are very clever groups of people. Often large groups. Reading through the unminified source of a library can be an awesomely educational experience as it's usually the culmination of many years best practice. If you want a nice introduction to some of the cool things in jQuery, for instance, check out these videos from Paul Irish:

    • http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/
    • http://paulirish.com/2011/11-more-things-i-learned-from-the-jquery-source/

    I've dug around in jQuery many, many times to try and figure out why something does or doesn't do what it should or shouldn't. The most detailed investigation was probably Investigating IE's innerHTML during which nothing was solved but I found out some cool stuff.

    Learning to write

    The best way to get your head around libraries is to write your own. Yes, there are literally millions of them (not literally) out there already but you don't need to aim for world dominance, that's not the point of writing your own. Start simply, map the dollar symbol to document.getElementById. Done. You've written a tiny library.

    function $(id){ 
        return document.getElementById(id);
    }

    Now you can add some more stuff. Maybe you could check to see if the thing passed to the $ is already an element or if it's a string. That way, you could be a bit more carefree about how you pass things around.

    function $(id){ 
      if(id.nodeType) {
        return id;
      } else {
        return document.getElementById(id);
      }
    }

    Add in a couple of AJAX methods, some array manipulation and before you know it, you've got a full-blown web development toolkit.

    If you're wanting a boilerplate to start your library off, I recommend Adam Sontag's Boilerplate Boilerplate.

    Here's your Library Card

    By now, you've rooted around in the jQuery undergrowth, dug through some of Moo's AJAX and pulled apart Prototype's string manipulation. You've written your own mini library, gotten a bit frustrated and wished you had a community of several thousand contributors to make it more robust. Now you're ready to start getting irked every time someone on Forrst asks if there's a jQuery plugin for charAt. Enjoy.

    This article is modified from a chapter in a book Andrew and I were writing a couple of years ago about web development practical advice. Seeing as we both got too busy to finish it, I'm publishing bits here and there. If you'd like to see these in book form, let me know.

    Geek, Opinion, Javascript, Guides

  • 29 Jul 2011

    Vendor-prefix questions

    There's something that's always niggled me about vendor-specific prefixes on CSS.

    Vendor prefix background

    Just in case you don't know about vendor prefixes, there's a good summary on A List Apart.

    Best practice dictates that you should always include non-prefixed properties last. This is so that when the property does become standard and the browser implements the non vendor-prefix version, it will use the standard rule as it comes later in the stylesheet than the prefixed one. The thing that has been bugging me is the assumption that the agreed standard version produces the same or better results than the prefixed one.

    A convulted and completely made-up example

    Imagine you have a paragraph:

    <p>Made-up text.</p>

    And you want it to have a white border with rounded corners. I'm fully aware you now don't need the vendor prefixes here but bear with me.

    p {
      border:1px solid white;
      -webkit-border-radius:5px;
      -moz-border-radius:5px;
      -ms-border-radius:5px;
      -o-border-radius:5px;
      border-radius:5px;
    }

    In this scenario, the non-prefixed border-radius hasn't been implemented by any browser but you're following best practice so you include the values matching the current specification. Okay, now imagine that for Webkit's implementation of -webkit-border-radius, they decided that the radius value was actually to be divided by two. No problem, you can include the required value for Webkit. Again, not a real example but stick with me.

    p {
      border:1px solid white;
      -webkit-border-radius:10px;
      -moz-border-radius:5px;
      -ms-border-radius:5px;
      -o-border-radius:5px;
      border-radius:5px;
    }

    You launch the site and send it out into the world.

    Six months later, the standard is set, it turns out developers agree on Webkit's implementation. It becomes standard to double your radius value. A month after that, browsers start recognising the non-prefix version of the rule and rendering with the new standard. At this point, webkit, moz, ms and o are all rendering wrong because they are ignoring their vendor-specific implementation and using the non-prefixed standard. Even though webkit currently has the right value, it's being overwritten. If the rules had been included the other way round, they'd still be rendering the same as they were.

    p {
      border:1px solid white;
      border-radius:5px;
      -webkit-border-radius:10px;
      -moz-border-radius:5px;
      -ms-border-radius:5px;
      -o-border-radius:5px;
    }

    Eventually,support for the prefixed version will be dropped and the browsers will only use the standard but that will be a little way down the line and gives you more time to correct your code or your client's code or wherever the code happens to be. Basically, prefix-last order buys the developer more time to correct any potential issues. I know that by using the vendor-prefixed rules, the developer is implicitly accepting the fact that they are using experimental properties which could change but in this scenario, it is not the prefixed properties that change but the non-prefixed one.

    The reason I chose border-radius here is just because it was easier that typing up an example that might actually happen in real-life involving gradients or masks.

    Open to suggestions

    I'm open to any explanation as to why this scenario might never happen or comments on how I've misunderstood something.

    Geek, CSS

  • 27 Jul 2011

    Feed the RSS

    For no real reason and definitely not to prove once again that RSS isn't dying, I just thought I'd write about a couple of the nice bits about the RSS feeds on this site so that readers could get the maximum amount of awesome per unit of blog.

    There's a handy trick you can do with the feeds if you don't want posts from every category. You can subscribe to an individual category by using the link http://thingsinjars.com/rss/categoryname.

    But that's not all.

    If, for instance, you like the JS and CSS stuff but can't stand the rest, just list the categories you want with a plus and you'll get those feeds combined and nothing else. Like this: http://thingsinjars.com/rss/js+css. Nifty, eh?

    Also, the RSS link changes depending on which category you're in.

    Geek

  • 25 Jul 2011

    Psychopathic megalomaniac vs Obsequious sycophant?

    - or -

    Tabs vs Spaces?

    Which are you? Is there any middle ground?

    In any modern, code-friendly text editor, you can set tab size. This means that one tab can appear to be as wide as a single space character or - more commonly - 2 or 4 spaces. It's even possible in some to set any arbitrary size so there's nothing stopping you setting tabs to be 30 spaces and

    format() {
                                  your code() {
                                                                like;
                                  }
                                  this;
    }

    However you do it, the point is that using tabs allows the reader personal preference.

    Indenting your code using spaces, however, is completely different. A space is always a space. There's actually a simple conversion chart:

    • 1 = One
    • 2 = Two
    • 3 = Three
    • and so on.

    The fundamental difference here is that the person reading the code has no choice about how it's laid out. Tabs means you get to read the code how you want to, spaces means you read the code how I want you to. It's a subtle difference but an important one.

    Space indenting is therefore perfectly suited to psychopathic megalomaniacs who insist their way is right while tab indenting is for obsequious sycophants who are putting the needs of others above themselves. Sure, there may be lots of grades in-between but why let moderation get in the way of a barely coherent point?

    Curiously, neither of these extremes feels a great need to comment their code. One sees no need as their code is for themselves and they already understand it, the other assumes that if they can understand it, its purpose is so obvious as to be trivial. Both are wrong here.

    There's unfortunately no real middle ground here. Teams must agree amongst themselves what side of the fence they fall down on. It is entirely possible to set many text editors to automatically convert tabs to spaces or spaces to tabs on file save but if you're doing that, you'd better hope your favourite diff algorithm ignores white space otherwise version control goes out the window.

    This article is modified from a chapter in a book Andrew and I were writing a couple of years ago about web development practical advice. Seeing as we both got too busy to finish it, I'm publishing bits here and there. If you'd like to see these in book form, let me know.

    Opinion, Development, Geek, Guides

  • newer posts
  • older posts

Categories

Toys, Guides, Opinion, Geek, Non-geek, Development, Design, CSS, JS, Open-source Ideas, Cartoons, Photos

Shop

Colourful clothes for colourful kids

I'm currently reading

Projects

  • Awsm Street – Kid's clothing
  • Stickture
  • Explanating
  • Open Source Snacks
  • My life in sans-serif
  • My life in monospace
Simon Madine (thingsinjars)

@thingsinjars.com

Hi, I’m Simon Madine and I make music, write books and code.

I’m the Engineering Lead for komment.

© 2025 Simon Madine