Archive for January, 2008

The first rule of Fight Club is…

Tuesday, January 15th, 2008

I hear, all too often, about how movies are too violent. But frankly, I just don’t agree. Why? Because this week’s example of how Hollywood has made me a better person comes from the movie Fight Club.

I guess it’s hard for some people to imagine how anyone can get any philosophical value or learn any life lessons from a movie about grown men beating each other senseless in basements and parking garages. As it turns out, though, the fighting was just a detail in the story. The real story was about mediocre men discovering themselves while cutting through the crap of modern society.

Hitting rock bottom
The movie gets going when we see the main character lose everything he owns to a mysterious explosion in his apartment. With only the clothes on his back, he finds himself staying with his friend in a condemned house. Left without the mind-numbing qualities of television, playstation, and the Internet, these two decide to get out and feel life. That’s when they start their fight club.

But it doesn’t stop at the creation of the fight club. They build an entire empire, manned by people who, like them, have nothing. They gain power over businesses, politicians, and even the police, all because they stopped numbing their minds and instead set out about doing something. As the line from the film goes, “it’s only after you’ve lost everything that you’re free to do anything.”

The things you own end up owning you
In a consumer-driven society, we are taught that it is our civic duty to spend. And no matter what ails you, there is something you can buy that will temporarily relieve you from your self-loathing.

We become garbage collectors, and our homes become our prisons. Soon enough, we’re keeping a $20,000 car in the driveway, in the rain and snow and wind, because there’s no room for it in the garage where we keep a few hundred dollars worth of junk nobody uses any more. Over the last 10 years, I’ve watched one of the fastest growing businesses in America — mini storage — popping up everywhere. We have more stuff than we have room for, so we’re paying other people to keep it for us!

We’re buying newer, bigger shelves to hold more and more CDs and DVDs; new dusting gadgets and disposable toilet brushes, and more injection-molded racks to hang them on; one blanket for the bedroom and another for the living room… for each person in the house… and then we complain that it’s too much work to keep it all clean! We want to travel, but we’re afraid of leaving our houses unoccupied for too long.

And everyone I meet is anxious to tell me how they’re not as materialistic as everyone else. Everyone is frugal. Everyone is economic. Everyone hates clutter. Nobody is part of the problem. So why is it a problem? As they say in the movie, “sticking feathers up your butt does not make you a chicken.”

Day after day, becoming a little more like Tyler Durden
After watching Fight Club, I was immediately inspired to break out of the materialist pattern. I bought a black leather CD case, capable of holding 200 discs, loaded it with my DVD collection, and took two enormous bags of DVD cases out to the trash. I had a big garage sale and sold off all knick-knacks, the tools I didn’t need since changing careers, the guitars I didn’t play any more, and all the clothes I didn’t wear any more.

But I didn’t stop there. When a friend needed a tv, I gave her mine, and since that day, I have not owned a television. (Seriously, almost five years!) Without the tv to keep me complacent, I get out more, I read more, I travel more, I do more. And I snack less, too.

The same year, I gave another friend my microwave, and haven’t had one since. That’s one of the best things I’ve done. Now that I have to cook, I’ve learned to do it pretty well. I eat healthier food, and it tastes better… and usually it costs less! And the cooking time isn’t an issue… after all, it’s not like I’m missing my favorite show…

At this point in my life, everything I own fits neatly into one small room. I have some clothes, some books, a few cameras, and a computer. But more importantly, I have a life. Perhaps I haven’t lost everything, but for the most part, I really am free to do anything.

Are you kidding me?

Tuesday, January 15th, 2008

MacBook Air

Apple just never ceases to impress me. This is another home-run. Go watch the video and see how huge this tiny little laptop really is.

Quote of the day

Tuesday, January 15th, 2008

“Losing an illusion makes you wiser than finding a truth.” — Ludwig Börne

Quote of the day

Monday, January 14th, 2008

“Believe those who are seeking the truth. Doubt those who find it.” — Andre Gide

Quote of the day

Friday, January 11th, 2008

“We can’t solve problems by using the same kind of thinking we used when we created them.” — Albert Einstein

Little green wisdom

Thursday, January 10th, 2008

The most profound sages and gurus in my life have not been religious leaders, intellectuals, teachers, leaders, bosses, or even parents. In my lifetime, the real sages have been fictional people — moving images dancing on a projection screen inside the cinema. With that in mind, I’m starting a new series of posts, discussing how Hollywood made be a better person.

Yoda. You seek Yoda.
The first fictional character to influence me came into my life when I was only five years old. It was a lovable green muppet who spoke in cryptic zen koans with poor grammar. Yoda was famous for saying “Do, or do not. There is no try.”

It was a fictitious, mystical religion invented to sell movies, but to a five-year-old kid, The Force was the most awesome idea ever conceived. And in spite of being rather ugly and scary-looking for a muppet, all kids knew that he was the master, and if we understood what Yoda said, maybe we could be Jedi knights, too. Ah, the power of a kid’s imagination. What fun.

But one little phrase from Yoda has stuck with me my entire life: “Do, or do not. There is no try.”

They may be words from a fake religion invented for a fantasy movie, but they’re so true! When you say you’ll try, you are already accepting the possibility of failure. But more than that, Yoda isn’t just telling his student not to leave room for failure… he’s telling him to take control of his life. What he’s really saying is: either choose to do it, or choose to not do it; saying you’ll try is only copping out.

The words we say reveal a great deal about the way we think. They also affect the way we think. One of the hidden powers of the mind is our ability to convince ourselves of things by using words. The subconscious doesn’t know the difference between reality and imagination.

Napoleon Hill
In his book Think and Grow Rich, Napoleon Hill describes the qualities of successful people, and he should know… he spent a lot of time around some of the most influential people of the early 20th century, including Andrew Carnegie, Thomas Edison, and Henry Ford. He spent two decades learning from rich and powerful people what made them successful.

Hill dedicates an entire chapter to the importance of decision making. He describes two kinds of people — leaders and followers — and suggests that 98 percent of people go through life as followers, taking what life gives them, while the rare two percent of people who make decisions and empower themselves go on to be successful at everything they do.

When Andrew Carnegie approached him and asked him to write the book, he thought about it for 30 seconds before saying yes. Carnegie told him that had he spent 30 more seconds, the offer would have been rescinded because a man who can’t make decisions and take action will never be successful.

Feel the Fear and Do It Anyway
In her book Feel The Fear And Do It Anyway, Susan Jeffers writes about the pain-to-power vocabulary, which encourages the reader to take responsibility for their life rather than leaving it to fate.

She says to replace the words I can’t with I won’t. Instead of saying I hope, say I know. The idea was important enough to her that she made a half-page chart to illustrate the importance of changing ten common cop-outs into empowering statements. But all she’s really saying is Do, or do not. There is no try.

Elegant date validation in JavaScript

Wednesday, January 9th, 2008

Why is date validation such a difficult concept for Javascript programmers? People are always trying way too hard to accomplish simple tasks. Let’s look at some of the wrong ways to do it.

This is from the top result on Google for “javascript valid date function”:

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

Holy cow! That’s a large amount of code to include into a project, just for date validation. And worse, if you click through the links on the Google search, you’ll find countless examples of huge, over-complicated validation functions, occasionally mixed with smaller functions that are simpler, but inaccurate. Why is this so hard?

Let’s look at another one. This is actual code that was used by one of the companies I’ve worked for:

function validateDate(dateValue) {
	var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
	if ((dateValue.match(RegExPattern)) && dateValue.length >= 8 && dateValue.length <= 10) {
		return true;
	} else {
		return false;
	}
}

This regular expression seems to accurately validate dates, even for leap year, but it’s absolutely unmanageable. If any aspect of this validation is ever found to be lacking in some way, I pity the person who will have to fix it.

Why don’t people just use Javascript’s built-in Date object to validate their dates? JavaScript’s Date object is built to do some rather neat stuff. If you set the month or day to a value that is out of range, the Date object subtracts the difference and updates the entire date. For instance, if you set the month to April (which has 30 days), and then set the date to the 31st, the Date object will automatically change the month to May and the date to the 1st.

Thus, validating a date is rather easy. Here’s what I do:

function validDate(date){
	//split the date using ANY separator
	var re=/(\d+)\D+(\d+)\D+(\d+)/;
	var a=date.match(re);
	if(a){
		//check the year format
		if(a[3].length==2) a[3]= (a[3]>69?'19':'20') + a[3];
		if(a[3].length<4) return false;

		//use integers and avoid octal parse errors for numbers starting with '0'
		a[1]=parseInt(a[1],10);
		a[2]=parseInt(a[2],10);
		a[3]=parseInt(a[3],10);

		//use the Javascript Date object to validate the date
		var dt=new Date();
		dt.setFullYear(a[3]);
		dt.setMonth(a[1]-1);
		dt.setDate(a[2]);
		if((a[1]==dt.getMonth()+1) && a[2]==dt.getDate() && a[3]==dt.getFullYear())
			return (String(a[1]).length>1?a[1]:'0'+a[1])+'/'+(String(a[2]).length>1?a[2]:'0'+a[2])+'/'+a[3];
		else return null;
	}
	else return null;
}

There are a number of advantages to doing it this way. The first advantage is flexibility of formatting. This function accepts dates with ANY delimiter — you don’t have to check independently for dots, dashes, slashes, etc. It also gracefully handles 1-digit or 2-digit numbers for month and day, and 2-digit or 4-digit years.

The second advantage is that the hard work is done by JavaScript’s Date object, which is compiled and fast. All we’re doing here is setting the year, month, and day, and then checking to make sure it stayed where we set it. If it did, all the values were in their acceptable ranges.

And the third advantage to this function is that it returns a consistently formatted date for use throughout your code, or null if the input value is invalid.

So, if you want to validate and format a date in a text field, you can just add one line to the onblur handler:

<input type="text" name="datefield" value="" onblur="if (!(this.value=validDate(this.value))) alert('Please enter a valid date.');" />

Of course, I recommend using a more elegant event handler to do the validation, but if you’re lazy, it really is that simple.

So there you have it, a much better way to validate dates. Please note that this function assumes the date is in m/d/y format. Naturally, I have a way of handling different formats, as well, but I chose not to address that here since the functions I found for comparison don’t bother to address it.

Some results from the “no” experiment

Wednesday, January 9th, 2008

Yesterday, I performed a little experiment, answering every question I was asked with “no” to see just how much of my intended meaning was conveyed through tone and body language. The results were interesting.

Case 1
I bought a pack of cigarettes at the 7-Eleven on Michigan Ave and was packing them as I walked out of the store. A panhandler approaching me asked, “hey man, let me git one dem.”

The audacity! I haven’t even opened the pack, and he’s asking for one. I looked him squarely in the eyes and calmly but firmly said, “no.”

He seemed almost hurt by my directness, and mumbled about me as he walked away.

Case 2
I’m outside my office building at lunch having a cigarette and a young lady come out. I’ve seen her out there before, but we’ve never spoken. She seems to have forgotten her lighter, so I’m already reaching for mine as she approaches.

When she asks if she can use my lighter, I say (with a very subtle grin), “no.” My words say no, but my body language is saying yes.

So she comes even closer, standing close enough that my body heat could be keeping her warm, and with the cutest puppy dog eyes she looks up at me and says, “pleeeeaaasssseeee….?”

Hmmmm…..

Case 3
I was paying for my lunch, and the cashier asked if I wanted a receipt. In a mostly serious tone, trying to keep a straight face, I said, “Absolutely not. Do not, under any circumstances, give me that receipt.” I even embellished with the axe-chop hand motion used by many politicians.

The response obviously did not fit the situation, and the cashier hemmed and hawed for a moment trying to decide if my response was more sarcastic or serious. In the end, she let out a giggle and said have a nice day.

If she had chosen to give me the receipt anyway, the game would have been on…

[Edit: Perhaps if I had stood at the register for a moment longer, she might have read into it that I was waiting for the receipt. I’ll have to try this in the future.]

Conclusions
I have no doubt that the words being spoken are significantly less important than the less-obvious communication happening with tone and body language. What I find most interesting, though, is the reactions I get from sending mixed messages — giving a spoken response that doesn’t match my body language.

I’m definitely going to have to experiment more with this. I’m particularly interested in noting any differences in how mixed messages are interpreted by males vs. females, and any effects age might have on the result!

Quote of the day

Tuesday, January 8th, 2008

“Give me a place to stand and a lever long enough and I will move the world” — Archimedes, 220 BC

“No” day like today

Tuesday, January 8th, 2008

Today is “no” day. This is my experiment: no matter what I am asked today, my answer will be “no”. I want to see just how much the words matter, and how much of the true meaning is conveyed through tone and body language. It’s already proving to be quite fun!