Archive for the ‘Web Interface Development’ Category

Thinking beyond Web 2.0

Wednesday, June 18th, 2008

Over the past few years we have seen people do some remarkable things with Javascript. This is almost like the age of enlightenment for Front-End developers. In a short amount of time we have gone from a majority of hobbyists and hackers to a group with a fair amount of true engineers. In some cases we are even gaining respect from back-end coders. We have done well for ourselves. 

Now we are experiencing some growing pains as Libraries are created and each competes to make their code faster and better. We are left wondering what direction to head next. It seems that there are so manny possibilities going in so many directions that if we follow the wrong one we could end up loosing our way and get left behind by the time Web 2.0 gives way to Web 3.0.1 Gold Vista-Leopard. Honestly though, I don’t think that things work that way. None of the changes that brought us to “Web 2.0″ should have been a surprise. We had been tinkering with ways to make things more interactive for years. Also, coding styles may have changed. This was no surprise either, we knew that we were hacking Javascript all along. I think in some way we all knew the time would come where we had to start respecting it as a real language and start following some standards. So, here we are still waiting for the next train. I think it should be obvious if you use common sense. Everything that you are doing now that you have always done, but just seems wrong will be replaced by a way that actually works. 

Here are a few areas that I think would be smart areas to focus:

  • Unit Testing: We will find ourselves building complex applications which can have many points of failure. Unit testing each interface can help us to make sure that each component of our code meets guidelines before moveing to build the next component. When QA comes knocking on your door they will find fewer bugs and you will have an easier time fixing bugs that are found because you will be more familiar with your code. It is no wonder why your buddies that code languages like Ruby and Java are crazy about Test Driven Development. Here are some resources that are available to unit test Javascript:
  • Name Spacing: For any of us that have subscribed to Dustan Diaz’s blog or that use the YUI libaray, this is old news. Still a lot of us are not keeping our code in a namespace. This ensures that any future home that your code finds itself. May it be a browser running Javascript 8.0 or a new JS library, your code will not collide with anybody elses code. When all browsers decide to start to support a String.toJSON() method, it will not conflict with your ME.string.toJSON() method. for more info on this check out the daddy of all JS namespacing posts: http://www.dustindiaz.com/namespace-your-javascript/
  • Code Composition: We need to start thinking of our code as something that is going to be seen by other people. Code should be reusable and the more organized, commented, and spaced it is, the better. Cleanly composed code also is easier to troubleshoot. Sold yet? Good. Here are a few tips:
    • Use a minifier that strips our comments and spacing before moving code to production. This gives developers the freedom to comment and space as much as they need without effecting the weight of the page over the wire. 
    • Use symantic names for your methods. We all know that the only documentation that we want to write is our code. When somebody encounters an object that you have created to deal with a customer they will be way more likely to understand a method named “saveCustomerData()” then one named “ajaxCall()”. 
    • If you don’t need to re-use it, nest it. For example, if you are calling a method that asks for a function as a parameter to use a call back, nest the function inside of the method call. A lot of people think that it is cleaner to declare the function first and then put its reference into the method call, but this moves the code for the function out of context while reading the code. It also requires more work for the browser to first declare the function and then envoke it later. Here is a code example:

          yourObject.yourMethod(function(
           /* string */ argument 1,
           /* Object */ argument 2
          ){
           //Code here
          });

This all comes down to common sense. I am sure that there are a lot more things that could be added to this list and I would love to revise this later. This is the direction that I am heading for now.



 

The community JS file [Teams]

Monday, March 10th, 2008

I want to share a pretty cool pattern for collaborating on JavaScript files.A little backgroundIn the application I am working on, we have a need to keep all of our JS in one file that is committed to subversion and included in the application. This one file is updated multiple times by members of our team and as a result needs to be frequently merged. Sure we could use includes of some sort to simplify this process but that has overhead of it’s own. I am sure that a lot of teams have more then one hand in the Java Script cookie jar at a time.The pattern

<script>
//Promoted Code
(function(){
	//Stuff
}())
//John's Code
(function(){
	//Stuff
}())
//Paul's Code
(function(){
	//Stuff
}())
</script>

What does this format offer?

  1. Each developer (John and Paul), will have their own closure to keep their development code. This will give them the freedom to create variables without worry of collision with other developer’s code. This also will do garbage collection before running the next developer’s closure.
  2. A “Promoted Code” closure exists to hold code that is production ready. This code will be moved from the specific developer’s closure when it’s development is over.
  3. Merging is way easier when you know where your code is in the file.
  4. Development code is placed after production ready code. This will allow the developers to rewrite objects in the production ready code if needed.
  5. Most text editors will let you collapse code blocks in a closure. This will clean up your work area and allow you to better concentrate on your code.

This pattern may seem simple but those are sometimes the best kind. I have noticed a dramatic improvement in my ability to work on these files in collaboration with other developers.

Sub-Library [Libraries]

Friday, January 25th, 2008

Just about everybody is using a framework or library for something these days. It is the logical next step. When everybody that matters and their mother has weighed in and IT is done as ITs going to be, it is time to lock what ever IT is down. This allows us a foundation to start building a greater ITs, you know? In my newest role, I have formed a bias towards YUI for layouts and javascript though I am fond of a few other frameworks too. Working with these libraries hopefully will inspire developers to start thinking of their work as something that could be re-used. Ideally everybody will see each html document as an API that could be used in a limitless amount of future applications. Even if you are just building a simple page for a one-off project that may never see the light of day, it is still important to think your conventions through.

Javascript conventions

Well thought-out javascript does not stop at the framework level. Creating a huge blob of overly specialized functions that leverage a flexible framework seems wasteful to me. There is so much extra energy that can be harnessed from all of those development hours that you spent toggling the visibility of that light box effect (or whatever). Learn to step back from your specific implementation and write a reusable control. This control would be a simple object-oriented chunk of behavior with a clear set of descriptively named methods and properties. I like to spend a little time brainstorming other use-cases before I begin creating any control. Once your inventory grows a bit you can start promoting your controls to a larger library that can be used for multiple applications. I find that this model, at first seems redundant, but in reality reduces the over-all code on your properties. If you are a freelancer, I imagine that this sub-library can be something that you package and sell to your clients to add value to your product offering.

Markup conventions

Hopefully you are already separating your behavior and style from your markup. If not, read this. One thing that will help you to build reusable javascript controls is a good Markup convention. Every control that manipulates what the user sees will need to work with the markup layer in some way. A little time spent deciding on some adaptable code standards will save you tons of time. Most of this is done for you in the W3C spec. They have worked hard to come up with flexible ways to represent things like numbered lists and definition lists and you owe it to yourself to leverage them. Sometimes however, you will need markup for a more specific implementation that does not match a W3C spec. When that happens try to stay shallow and make sure it works constantly in all browsers. If possible it would be smart to document this convention so that your peers and successors can easily pick up where you left off. I like to think that most conventions can be positioned so that a javascript control will bind to a specific container on a page using a className, then gain access to child nodes of the container relying on your pre-defined markup convention as an API. I suggest using a class because it is possible that your control will be used more then once on one loaded document.

And a little about style

I like to think that this can all be done without applying styles such as font types, images, and colors. With a few exceptions this can all be done through CSS which by now should go without saying.This pattern is one that I have been perfecting over that last couple of years. I am a huge fan of using conventions where possible because, I hate doing the same thing twice. I am sure most of you can agree with that and a lot of you are already applying this in some way.