Tips for working with jquery

I’m a fan of jQuery because it promotes unobtrusive markup and functionality and has a wealth of plugins to take care of common web development/ajax functionality. Marc Grabanski offers some tips for working with it effectively. I’ll need to look at livequery in some apps I’ve put together. If you need to attach events to elements, and re-attach the same events when parts of the page are updated, it should make keeping track of it all a lot easier. My tip for working with jQuery? Use Firefox with Firebug to inspect the page and debug ajax requests.

5 Tips for Better jQuery Code: jQuery, Tutorial

I’ve been coding using jQuery since shortly after it came out, and well — I’ve been using it almost every work day. Here is a few tips that have saved me time.

Practical use of message queues

I stumbled on this post over at the flickr developers blog while chatting with Jo about web applications designed and built for “the cloud”. An article earlier this week piqued my curiosity about message queues, and got me thinking about how they can be used to move processing tasks out of the way. I’d known already that minimizing those tasks within, let’s say, a PHP script that builds a page can help your application scale or seem more responsive.

To do so, I’ve relied on cron jobs, but the main problem with that approach is either that your cron job runs when it has nothing to do, if you run it too frequently, or it has too much to do and you run the risk of one job starting while another is in progress and working with potentially the same data.

Code: Flickr Developer Blog » Flickr Engineers Do It Offline

For scale, Flickr separates these three lookups into three different places. When you upload that photo, we immediately tell you about it, and get out of your way so you can go off and marvel at it. We don’t make you wait while we tell your contacts about it, insert it into the search system, notify third-party partners, etc. Instead, we insert several jobs into our queueing system to do these steps “later”. In practice, every single one of these actions subsequently takes place within 15 seconds of your upload while you’re free to do something else.

Finding some late night coding time.

Nicholas has started sleeping through the night, as of last week, and while it hasn’t been every single night yet, its happened more often than not. I’m glad for Staci, since she’s not a night owl like me, staying up past 10 or 10:30 was harder on her.

This also means I’ve found some time to work on my project sites and blogs. I’ve recently discovered the utility of PHP5’s autoload function, specifically the spl_autoload_register method. So I’ve taken to refactor my codebase to use it Its nice not having to figure out where and when to include class definitions. I’m using the PEAR naming convention to handle autoloading and, as a bonus side effect, this has forced me to organize my class files and directories in a consistent manner.

It hasn’t been all under-the-hood, improvements that only I will see, but I can’t unveil anything new just yet. Sadly, this is the most interesting coding I’ve been doing lately. At work, we’re learning to use Drupal effectively, which has, for the better, reduced the back-end programming we get to do. But I’ve gotta keep my skillz up2date.

Management Anti-patterns

Sandy sent me a link listing anti-patterns as applied to management practices. For those not up on the jargon, an anit-pattern is a bad practice that is repeated frequently. I first heard this term in terms of programming patterns and anti-patterns, but I’ve since heard it used in non-programming contexts. I can honestly say I’ve run into the Hard Code anti-pattern, code-momentum, cargo cult programming, and bug maganets, to name a few. I’ve also commited Accidental complexity, magic numbers, monkey work, and parallel protectionism as I’ve learned to be a better programmer.

The management ones would be funnier if they weren’t so true. The ones that hit close to home for me are Hostile Testing, Napkin specification, Seagull Management, Leader not Manager, Scope Creep, and Design by Committee.

Anti-pattern – Wikipedia, the free encyclopedia

Often pejoratively named with clever oxymoronic neologisms, many anti-pattern ideas amount to little more than mistakes, rants, unsolvable problems, or bad practices to be avoided if possible. Sometimes called pitfalls or dark patterns, this informal use of the term has come to refer to classes of commonly reinvented bad solutions to problems. Thus, many candidate anti-patterns under debate would not be formally considered anti-patterns.

Are you working with Customers or Collaborators?

Zed Shaw provides a very useful insight into what makes a project work.  I’m posting this here a) so that I can refer to it again in the future and b) the hypothesis passes my "Yup-I’ve-been-there" test.

This method is based on a simple revelation about how clients and projects can be divided generally into Customer,Collaborator;Implementation,Invention. I call this “The C2I2 Hypothesis” since it’s not a theory yet, never will be a theory, is based mostly on my thoughts on the subject, and you should probably just use it as a way to think about the problem yourself.

Another reason I like the essay is his burtally honest statement that "This is again just my thoughts and experiences. I’ve got little if no evidence that my ideas are valid since there simply isn’t any". I could point to a number of projects where matching the two were successful or lead to failure.  Figuring out if you are working with Customers or Collaborators will give you direction in to how to ask questions, report progress, and deliver the final product.  At the least, it would be very valuable to ask these questions next time your sitting in a conference room with a Project Manager to kick off a project to make sure the project group is on the same page.

A comprehensive look at PHP5 DateTime

Don’t expect to find it here, but Laughine Meme posted the most comprehensive one I could find.  You would think that with the introduction in 5.2 of the DateTime and DateTimeZone objects, that date math would be a lot easier in PHP.  Unfortunately, the implementation isn’t as advanced as I’d expected, maybe a case of high expectations.  Dealing with dates,especially once timezones are involed can be very unintuitive, and this doesn’t seem to make it noticeably easier.

Beyond adding deltas (”+7 days”), the other common date math is comparing two datetimes, to find out which is more recent, and getting the difference between them. DateTime supports no methods for comparing two datetimes. The simplest solution for doing comparison is to compare epoch seconds.

Buried in the comments, is this most wise bit of advice:

For most everyday issues, use gmdate functions for storage and putenv(’TZ=wherever’) to make the local presentation correct and you’re done. Converting a stored gmdate (say MySQL date/time format) to localtime for a given date string is a matter of appending ‘+0000′ onto the SQL string, passing it through strtotime, and then use date() on the result. The +0000 forces strtotime into dealing with it as GMT instead of trying to act smart and adding your local offset. Should be easier, but that ain’t bad.

Deals with the devil

After two summers during high school where I had the pleasure of working with Visual Basic and Access, you couldn’t pay me enough to program with an MS designed language again.  And, even though I have a basic understanding of Java, very few interesting web sites use Java as their foundation.  But apparently, the marketplace has figured out the price of your soul.  This post gives me an excuse to mention that I work for one of 60 Great places to Work in the Washington DC metro area, and we’re looking to hire more developers.

IT employment specialist Robert Half Technology’s 2008 Salary Guide found application and senior web developers versed in Java and Microsoft’s languages can add another 10 to 12 per cent on top of the salary range for developers. Those skilled in LAMP and AJAX can add around five per cent.

Java and .NET to drive double-digit wage growth. 

jQuery 1.1 – Slimmer and faster

Version 1.1 of my my preferred javascript library, jQuery, was released today.  They have a great motto – “write less, do more” and as far as I can tell, they live up to it.  The release notes claim speed improvements of 10-20x for selectors over those in 1.0.4.  The release includes some interesting new features – such as passing functions for an attribute or a css property.  The flexibility of that is mind-boggling, but it could also lead to some hard to maintain code in the wrong hands.

Why do I like jQuery over say prototype/Scriptaculous?  At a very basic level, it probably comes down to personal preference, jQuery was easier for me to grok.  But what first got me hooked was its straightforward use of selectors to work on DOM elements.

jQuery selectors are a combination of CSS 1-3, XPath, plus some custom code to glue it together. Essentially, the best parts from both of these query languages were taken, combined, and used to create the final jQuery expression language. If you already know CSS (which most web developers do) then you’re going to be fine.

OOP in Javascript examples

Andres at Web 2.0 Technologies has a very informative post about how to use object oriented techniques to implement a robust event broadcasting and listening in javascript.  Follow his advise an dmake your code more reusable.

 What do you want to invoke when the checkbox object changes state? If you’re programming it
in a non-reusable way, you can hard-code the action directly into the onclick event:
if (state==checked) do X; if (state==grayed_out) do y; etc. If you’re programming it
as a reusable component, you need a more flexible solution.

Regular Expressions are your friends.

Two and a half years ago, when I first dove into web development, I wasn’t very familiar with regular expressions. They certainly didn’t look regular to me, looked more like someone’s keyboard exploded. Now, having figured out a thing or two about them, I came across the following lines:

		$pos = strpos ( $this-&gt;user-&gt;privs, 'del');<br />		if ($pos===FALSE)<br />		{ // edit not found in string privs<br />			return FALSE;<br />		} else {<br />		// figure out where the string ends by looking for a comma, return that substring<br />			$next_comma = strpos( $this-&gt;user-&gt;privs, &quot;,&quot; , $pos);<br />			if ($next_comma===FALSE) $next_comma=strlen($this-&gt;user-&gt;privs);<br />			$len = $next_comma-$pos;<br />			$epriv = substr ($this-&gt;user-&gt;privs, $pos, $len);<br />			return $epriv;<br />

Which I was able to boil down into the following:

    if ( preg_match(&quot;/del_[a-z]+/&quot;, $privString, $match) )<br />    {<br />        return $match[0];<br />    } else {<br />        return false;<br />    }<br />