Archive for July, 2010


One line if statement

This is an oldie but goodie. As you may already know, an if statement (in any programming language) executes a statement based on the condition you provide. In addition, it also gives you the option for an alternate else statement if your condition fails. Here’s the basic syntax:

if (condition){
// condition is truthy
} else {
// condition is falsey
}

By default, the condition always assumes true. For example:

var testMe = false;
if(testMe){
alert("Yay!");
} else {
alert("Boo!");
}

Since testMe is set to false, the statement above alerts “Boo!”. Since the if statement assumes true, we did not have to write if(testMe === true), just putting the variable is enough since it’s a boolean (true/false) value.

Now that we got that out of the way, on to the one liner!

Programmers tend to follow some mantra’s when coding, one being K.I.S.S. (Keep It Simple Stupid). Saving bits of information can make or break your app (depending on the amount saved, or course).

If you’re writing a conditional if statement that it simple, why not write it on one line! Here’s the template for the syntax:

(condition) ? //do if condition is True : //do if condition is False;

Let’s take the previous code and put it in one line:

var testMe = false;
(testMe) ? alert("Yay!") : alert("Boo!");

When I first found out about the one line if statement, it was a bit confusing. I knew using it would kick ass and make my code cleaner, but I just couldn’t grasp the syntax. So I committed the following to memory:

() ? : ;

The question mark makes it easy for you to understand what your trying to do (execute a piece of code based on the condition).

Here’s the break down:

  • the question (condition) is in parenthesis
  • ? ends the condition
  • the code to the left  of the : is executed if the condition is true
  • if the condition is false the value to the right of the : is executed
  • ;, of course ends the statement.

Easy, right?!

Let’s go one step further…

Say that you have a variable who’s value must be determined by a condition. We can set the value of the variable using a one line if statement. Take the following example:

var myNum, myConditionalNum;
myNum = 3;
myConditionalNum = (myNum > 1) ? 5 : 0;
alert(myConditionalNum);

So what value get’s assigned to the myAmazingNum variable?…

Continue reading

Advertisements

In case you missed it, here’s the webcast:

jQuery Deconstructed

colleague of mine showed me this amazing site. It’s probably the best documentation that I’ve ever seen in my entire career. Check it: http://www.keyframesandcode.com/resources/javascript/jQuery/deconstructed/

I Like You: Part 1

I, perhaps like you, has heard of a little site called Facebook. In an effort to dominate the world provide Internet users a way to share pages with their friends, they have released what they call an Open Graph protocal. This public API allows developers a new and rich way to interact with the Facebook audience (400M+).

Many popular sites have already been implementing their most popular feature: the infamous Like button. You have probably already seen this at the top or bottom of many articles; it’s the little thumbs up icon that allows readers to post the current article to their wall, thus promoting the article to their friends. The easiest way to get it on your site is by using their generator: http://developers.facebook.com/docs/reference/plugins/like.

The cool part about this is that they also offer additional meta-tag so your page can show up on search results and the users profile. Adding these optional values will really beef up your presence on Facebook (that’s a good thing). If you plan on using the  like button, you should put the extra effort to add the optional meta-tags. Doing so won’t slow down your page, it will only help people find your article easier.

FQL
Their API also offers developers the ability to query their database via Facebook Query Language (FQL). It’s pretty badass.

An example of a query would be the following:

https://api.facebook.com/method/fql.query?query=SELECT share_count, like_count, comment_count, total_count, click_count, normalized_url FROM link_stat WHERE url="http://google.com/"

This returns an XML output of the query used. This can be extremely helpful when you want to grab specific information and do cool things with it.

Stay tuned for a future post where I’ll go over how you can render your query output to a page and make it look all pretty.

Learning jQuery: resources

There are millions of articles, blogs, and slides on the Internet that can show you how to learn jQuery. If you don’t feel like forking out real dollar signs for a book, check out the list below for FREE resources that can get you started now:

There are TONS of resources, these are just a few of my favs. Which is the best one? That’s up to you. For an even more extensive list, check out: http://www.learningjquery.com/2010/07/great-ways-to-learn-jquery

Today ONLY!!
http://www.oreillynet.com/pub/e/1649

Update:

Check out the video here: https://qpants.wordpress.com/2010/07/22/oreilly-webcast-5-jquery-mistakes-youre-probably-making-in-your-project/