usable in any place a human can be used

Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

20100507

stir trek

[caption id="attachment_844" align="alignnone" width="539" caption="They are watching Iron Man 2 this time, but kept the Stir Trek name"]Stir Trek 2010: Iron Man Edition[/caption]

This post is a little bit late but c'est la vie. I will be talking at Stir Trek today about all the new goodness baked into the latest release of jQuery, version 1.4. I'm taking a page from this very cool HTML5 Presentation and have decided to skip the normal PowerPoint and do the whole presentation in HTML5. It was actually quite easy to do and since I'm showing off jQuery I can have live demos in my slides, so no fumbling between PowerPoint and a browser. I used the HTML5 Presentation as a template so you will notice that the look very similar, I've kept their stylesheet and their slide javascript. The presentation is functional in most modern browsers but is definitely fine tuned for Google's Chrome browser.


My presentation tries to be a quick redux of the information found at the jQuery 1.4 Launch Site, specifically the great introductory series jQuery 1.4 Hawtness by the very talented Paul Irish. I took the javascript code examples from the jQuery 1.4 Hawtness videos and built functioning demos around them to better illustrate how they work to an audience.


Keeping in the spirit of my open-source free-love hippie nature, the presentation is completely free for anyone to use. You can find the full source of it on github, feel free to download it or fork it and make it better. Well I'm going to go run through the things I plan on saying for the next couple of hours, so pardon the brief post. It's been an interesting time in my life recently and once everything settles down I'll have some big news for everyone, so stay posted.


Edit: Realized that I can host whatever I want on my domain so instead of just linking to the source code on github I can actually host the presentation on this server. If you want to check it out go to http://ihumanable.com/jquery/presentation.html, remember it works best in Chrome but should work ok in other modern browsers, for IE users you will have to load up Chrome Frame.

20100125

api

[caption id="attachment_618" align="alignright" width="142" caption="oh creator of hot pockets, we praise thee!"]samsung microwave[/caption]

I remember being a young lad preparing myself for university I was given a gift from my mother, "C++ for dummies." The vote of confidence on my status as a "dummy" aside, I read the book with great interest. There was an analogy the author used to explain the idea of classes and what functions they should expose, I'm going to shamelessly steal it (paraphrasing as I don't have the book with me).


Imagine your son comes up to you and says he wants to make some nachos. You tell him that its fine by you, just go cook them in the microwave, and there is nothing controversial about this statement. Microwaves are actually boxes full of high energy radiation being produced by cavity magnetrons or waveguides, to the end user, the microwave is just a magic warming box. It exposes a very simple interface, some buttons that allow you to enter the amount of time you want to cook something.


This is the essence of an API (Application Programming Interface), wrapping up something complex and possibly dangerous in something safe and easy to interact with. When building code that you intend other people to use someday, it is the part that is most important part, and the part that is easiest to overlook. The problem is that we are often too close to something and too concerned with our use case. If you want to design code for others to use, it requires significant time and effort, and even then you probably still won't get it right.


Prosper is still undergoing active development, I'm currently agonizing over how I want to expose various execution modes. The solution, no matter what I pick, is trivial to implement, but the api is the most important part. A great api exposes a consistent concept, something that is easily grasped and allows the end user of the api to declare what they want to do without having to worry about how its going to get done. Since good programmers write good code and great programmers steal great code, I've modeled the api for prosper extensively off of jQuery. And why not, let's take a look at two different APIs, the browser dom api and jquery.


[javascript]
//Let's barber pole a list by coloring every other element blue
var list = document.getElementById('the_list');
var highlight = false;
for(var i = 0; i < list.children.length; i++) {
if(highlight) {
list.children[i].style['backgroundColor'] = '#FF0000';
}
highlight = !highlight;
}
[/javascript]

Fairly straightforward implementation, but it concerns itself heavily with the "how" of what its doing. Manually traversing to pick the elements it wants, eww.


[javascript]
//Same thing using jquery
$("ul#the_list li:odd").css("background-color", "#FF0000");
[/javascript]

jQuery is definitely magic, but this code is great because it let's you focus on the "what" of what you are doing. How does jQuery go about selecting the right elements and all that? I don't care, and the great thing is I don't have to care, and if in the next version of jQuery they find a way to do it faster, I win without having to do anything.


Writing a great api is difficult, you have to divorce yourself from the concrete problem you are solving and look at it in the abstract. Put yourself into the shoes of someone trying to figure out how the api works, and then ask the questions they are going to ask.



  • Why do I need to build up this context thing and pass it in?

  • How come there isn't a sensible default for these arguments?

  • What dumbass made this thing?


Answer those questions, and keep working at it, strive for elegance and consistency, because then it will be easy for people to learn and use. If your code is easy to learn and use, people are going to want to use it more, and they are going to want to tell their friends about it. Then you can get some lucrative ad campaigns with Nike because of the boffo library you write in FoxPro.


There is a more subtle lesson in all of this though. Any code you write is exposing an api to someone else. "But only I am ever going to use this code!" I hear the naysayers warming up their keyboards for the comments section. This may be true now, but the six-months-from-now-you is going to look back at the you-of-today and wonder what the hell he was thinking.


Get in the habit of making your code easy to use, and expose a nice api. This will endear you to your fellow programmers and help make maintenance easy. Strive to be that guy on the team that writes functions and classes people want to use. Make life easier for your fellow developers and even if they don't return the favor, maybe they will take you out for a beer. People notice over time that your code is the best to work with, they internalize it, and they start to think of you as a great programmer. That's a pretty great api to expose to the world.