Archive for the ‘advice’ Category

A Primer on Javascript Objects - JSON vs. Functions

Friday, April 25th, 2008

Modularity is one of the fundamental principles of good programming, and it’s something that is too-often overlooked in Javascript.  The problem is twofold - one part is that most people approach writing Javascript without a plan, and one part is that many people don’t know that there are better ways of doing things.

Advantages of Objects

The advantages of using objects are fairly numerous, but there are two that stand out. The first is that your set of functions is probably not the only one around that uses function names like “init.” The object notation makes this a non-issue, since each object defines a namespace. Another advantage is that you don’t have global variables floating around, for much the same reason.

The second advantage is a logical one; in some cases, it makes more sense to have objects doing the work, and it’s certainly easier for somebody reading your code to figure out what “randomQuoteGenerator.init()” does, as opposed to just “init()”. In the following example, you’ll see some of this.

A Contrived Demonstration

I’m going to demonstrate two different ways of creating Javascript “objects.”  One is based on JSON, the JavaScript Object Notation, and the other based on Javascript’s first-class functions.  As a demonstration, we’ll write an object representing a div that shows random quotes.  The object we will be creating will be defined as follows:

- A class variable called _target, which is a reference to a target <div> on the page
- A method called “update,” which updates the div with a random quote
- A method called “showAll,” which updates the div to show all the quotes.
- A method called “init,” which initializes the class variable to the target div.

A demonstration can be found here.

Functions as Objects

The functional method goes as such:

var object2def = function(){
	 this._target = null;
	 this.init = function(id){
		 this._target = document.getElementById(id);
	}

	 this.update = function(){
		index = Math.floor(Math.random()*quotes.length);
		 this._target.innerHTML = quotes[index];
	}

	 this.showAll = function(){
		quotestr = “”;
		for(i=0;i<quotes.length;i++){
			quotestr += “<p>”+quotes[i]+”</p>”;
		}
		 this._target.innerHTML = quotestr;
	}
}

var object2 = new object2def;

If you were comparing this to the regular, procedural method, the first thing you would notice is the addition of “this.” Variables declared with “this” are only accessible as properties of their parent entity - in this case, the object2def class/function/variable (in Javascript, it’s all 3). The rest of it is pretty straightforward: read from a predefined list of quotes, and output what was specified above.

Notice that at the very end I make a new instance of the object. This isn’t strictly necessary, it’s just done to demonstrate the ability to create new objects. You could put 20 quote generators on the page and create randomQuoteMachines 1 through 20, and have them each control a different id. Also note that I could dispense with the init function and put the id variable call in the outer function, thereby creating a constructor of sorts. Then I could just say “var object2 = new object2def(’bar’)” to make an object that updates the div “bar”.

JSON Method

The JSON method goes like this:

var object1 = {
	_target : null,
	init : function(id){
		this._target = document.getElementById(id);
	},

	update : function(){
		// An integer between 0 and the length of quotes - 1
		index = Math.floor(Math.random()*quotes.length);
		this._target.innerHTML = quotes[index];
	},

	showAll : function(){
		quotestr = “”;
		for(i=0; i < quotes.length; i++){
			quotestr += “<p>”+quotes[i]+”</p>”;
		}
		this._target.innerHTML = quotestr;
	}
}

This functions in exactly the same way as the other one. However, creating instances of it is an entirely different story, in that you can’t really; at least not without using something like Classy JSON.

A few gotchas to note about the JSON version are the commas after function declarations (except that last one), and the use of colons between name-value pairs. However, I think you’ll agree that, overall, the JSON version is somewhat cleaner, if less flexible.

If I’m creating something I know will only be used in one context, I tend to lean towards JSON, because I do like the cleanliness. However, if I’m creating something which I want to function more like an object and less like just a namespace, I’ll switch to the other method. It’s up to you which one you use, but I do recommend using something like this, as opposed to having function init(){} floating around gumming up the works.

Marketing Snippet: Memorable Trumps Good

Monday, March 10th, 2008

I would like to introduce you to a man.  This man is a feature — nay, a legend — in his hometown, which happens to be my hometown. His influence upon this city can be felt the moment you step off the ferry and gaze upon his majestic visage.

His name is Gordy Dodd, and if you live in Victoria you have almost certainly noticed his new bus ad campaign, featuring 007 Dodd, Superman Dodd, and a few others. Dodd, and his company Dodd’s furniture, have produced a number of highly entertaining videos, although unfortunately only a select few are available online. These videos have burrowed their way into the hearts and minds of Victorians everywhere, and whether they like him or hate him, you can be damn sure that almost any Victorian you ask can remember him. But what makes Gordy Dodd so memorable?

Uniqueness

Dodd's Furniture
Gordy Dodd is not a beautiful man. In fact, he has a very distinct homeliness about him (I’m sure he knows it, but I’ll sure feel bad if he has to hear it from me). Plus, even with all the years he’s been on TV, he still has an incredibly strong accent. Even better, his ads go over and above regular low-rent-local-business-loudmouth territory and into a strange world of bad costumes, worse acting, and the sense that Gordy is going to crack up any second until you realize that, no, that’s just his face.

Gordy has been the Hulk, Harry Potter (”Even a muggle knows our savings are magic”… but add a strong indian accent), Elvis, and many more. He’s made full-scale bollywoodesque ads featuring what probably constitutes a solid majority of his female relatives, dancing in saris. Rest assured that the link above contains a fraction of a fraction of Dodds’ full repetoire.

His delivery is priceless - there is no worse actor in the world than Gordy Dodd, and he the fact that he tries so hard makes you love him all the same. But what he lacks in acting, he seems to make up for in marketing genius and sheer gumption.

And you can bet that he’s never paid more he makes from selling a couch to make them.

Repetition

Gordy Dodd is everyhere. His mug has been gracing local TV screens for years and years. He appears on buses, in newspapers, on magazines. All this further separates Dodd from the crappy carpet salesmen with similar budgets - not only will they not dress up in spandex on TV, they’ll only try once or twice and then give up because almost everyone ignores them. Nowadays, when Gordy releases a new ad, you can be sure that it will come up as water-cooler talk.

Be Memorable

So what can we learn from Dodd’s Furniture? Here, I’ll illustrate it with another example.

QUICK! Picture in your head, in detail, an Olympic games logo.

I’ll wait, although I bet you see where this is going already.

Was it London 2012 (click to see the logo)? I bet for a lot of you, it was. Even though statistically you probably hate it, you can be sure that the 2012 logo is going to be hard to forget. And don’t you say that’s just because of the controversy - if it wasn’t for the logo’s decided “uniqueness”, it never would have gotten that sort of press in the first place (no such thing as bad publicity).

Being memorable is not the same as being good. And unless you’re trying to showcase your design/directorial/creative talents, being memorable is just as good and probably better.  If you can hit that water-cooler-talk sweet spot among your clientèle, success is sure to be yours.

Typography: Leading For Dummies

Thursday, February 21st, 2008

Or, The Magic Number 1.68

One issue in web design coming to the forefront recently is that of typography. Typography is defined by the Oxford English Dictionary as “The action or process of printing; esp. the setting and arrangement of types and printing from them; typographical execution; hence, the arrangement and appearance of printed matter.” We’ll just pretend that computers count as “printed matter”.

Today’s article on typography applies to spacing. To web designers, spacing can mean a lot of things, and proper typographic spacing can mean a lot of work. Well-typed documents often require the designer to obsess over details like vertical rhythm, line heights, header margins, grids, etc. It’s so much work that I’ve saved this post for a good long time until I could get around to typing this blog properly, so that I wouldn’t get complaints about it.

However, I’ve come up with what I consider a good standard for spacing, and as you may have predicted, the ratio 1.68 is central to it.

The Method

The first aspect of this scheme is to set the body font size, which I typically set to 83.5%. This allows ems to be treated as point sizes, such that 1.2em is equal to 12pt text. This is a matter of preference of course, but unless you do it this way your magic number may be different, so bear with me for now.

The total vertical space occupied by an element is the sum of its top and bottom margins and its line-height, as well as its top and bottom padding if applicable. 1.68 represents a good line-height for 12pt (1em) sized text, and a good top-and-bottom margin as well. So the way to start is to do this:

p{
font-size: 1em;
line-height: 1.68;
margin: 1.68em 0px;
}

This ends us up with 10pt paragraph font, and a vertical spacing of 3*1.68, whatever that is. Also note that the margins will collapse on top and bottom.

The next step in the process is to make sure that every block element in your body copy should have 1.68, or some multiple of it, intrinsic to its vertical spacing. This includes h1 through h6, paragraphs, list items, block quotes, and any other sectioned text that you have in your documents. This can be achieved by picking the font size you want, and then setting the line-height to that over 1.68. If you want 1.2em text, make your line-height 1.4. The top- and bottom- margins should be the same, in ems, as this.

Headers are a special case. They can have uneven top and bottom margins, as long as the sum of the top and bottom margins is a multiple of 1.68. The line-height for larger headers can double if they’re getting cut off, such as the headlines in my blog as of this writing.

After all that’s said and done, you will have a page with perfect (adequate) vertical rhythm. What does that look like? It looks like this template I made. Look at the sidebars, and notice that the text lines up with the body. If I had used a smaller font with appropriately larger spacing, such as 0.9 : 1.8666, it would still line up, and be a neat effect to boot. Try it!

I should mention I learned all of this from http://webtypography.net , whose advice is nothing short of exhaustive. If you found this even a little interesting, be sure to check them out.

Why isn’t my Negative text-indent hiding text?

Wednesday, February 20th, 2008

I’m making this post so that some day some person will google that phrase, and come here, and I can tell them that it might be because some parent element has a text-align: right set on it, which seems to render negative text-indents not at all.  Set the thing to text-align: left and everything will be ok.

Dress for Success

Wednesday, February 20th, 2008

That’s what my mom always told me. I never believed it — through high-school and well into college I wore almost exclusively second-hand clothes, save the first week or two on a job when I was trying to make a good impression. But I sometimes felt out of place, especially when I had to put on my not-so-well-worn suit and a tie and make like it didn’t feel strange. However, two months ago I placed an order at Indochino.com for a tailored suit, after having read about them in UVic’s student paper (The Martlet) several months prior. The suit came in two weeks, and as soon as I tried it on, I knew I had made a good decision.

Nothing looks quite so good as a properly fitted, stylish suit. You can’t slouch in a suit; you have to stand straight up, chest out. And I can tell you, when you know you look good, it really helps your confidence. I can’t tell you why, but if you’ve tried it you’ll agree with me.

Anyhow, a few weeks after that Indochino announce a sale, giving three free shirts with a suit or outerwear purchase. Having found that my usual vintage outerwear did my newfound business apparel no justice, I opted for a James Dean-esque peacoat, which proved to be another excellent purchase. I wear the shirts whenever they’re clean, and I always feel like I’m ready to take on the world, even when taking on the world involves not leaving the house.

In closing, I have two points. One, Indochino is worth checking out. I’ve had several contacts with the guys who run it, and they seem very down to earth and service-oriented, and the suits they sell look pretty sharp too.

Two, if you dress well, you feel good, and if you feel good, you live, work, and play better.  Of course, sometimes dressing well means my comfortable second-hand stuff, and that’s alright too.