Archive for April, 2010

Never Compromise Quality

Tuesday, April 27th, 2010

In programming people sometimes feel they have to sacrifice quality in order to get a product out the door on time. Never do this. You are hurting the customer, the company and yourself if you do.

This American Life had a very interesting program on the New United Motor Manufacturing Inc (NUMMI) plant in Freemont, California. NUMMI was a joint venture started in 1984 between Toyota and GM for both parties to learn from each other. GM would learn about Toyota’s quality control systems and Toyota would learn about building cars in America.

The amazing story is the turnaround of the plant. Prior to the joint venture, the plant was operated by GM and was one of their worst factories. The program talks about workers gambling, having sex, and drinking at work. They produced very low quality cars and didn’t really care. The plant was closed as a result. However, for this joint venture, GM agreed to re-open the plant and hired back many of the original workers. Thanks to Toyota’s quality systems, the plant turned around to become one of the best GM had.

One example of change was “stopping the line.” The GM workers grew up with the notion that you don’t stop the line. If you notice something is wrong, you just keep it going. Just get the cars out the door. With Toyota’s system, workers were encouraged to stop the line if they noticed something wrong and fix it on the spot. This was a big shift for them. The process became about quality over quantity. Within just three months, cars coming off the lot were being made with near perfect quality.

When you let an error slip, either with cars or with software, you are compounding the problem. It takes much more time and money to fix the problem later than when it is first discovered.

How much more money? One study estimated “it would take 50% more workers under the old system to produce the same car.”

Recently I had a situation like this. I was responsible for checking over and delivering database scripts to a client. I noticed one of the scripts had an input parameter and while I got a feeling I should double-check about it, I ignored that intuition and delivered them anyways. The client ended up running the scripts in their environment but ignored the input parameter which caused part of the scripts to fail. There was a lot of back and forth to get it fixed. I estimate it probably wasted about one man-days worth of everybody’s time. Because I didn’t take 5 minutes to check about something that could be wrong, I ended up costing hours worth of time.

It’s not mentioned explicitly but it is implied that the vices went away after the quality program was introduced. While workers were embarrassed to be working at the plant before, they now were proud to tell friends where they worked.

In ancient times, brick makers, engravers, and other artisans used a symbol to mark the things they created to show that they were the makers. The symbol that each one used was his “character.” The value of the work was in proportion to the skill with which the object was made. And only if the quality of the work was high was the character esteemed. In other words, the quality of the person and his work gave value to his credentials. If the work was good, so was the character. If it was bad, then the character was viewed as poor.
- “Becoming a Person of Influence” by John C. Maxwell & Jim Dornan

This is why the morale and esteem of the workers improved once quality became the focus. Your character is revealed in the work you do and vice versa. If you are producing shoddy work, your character is being revealed as such.

In programming, quality does not mean taking a stand over something trivial such as whether to use tabs or spaces for indentation. Quality means being consistent in your work. It means following design principles that will save time and effort down the road. It means doing things right the first time so you don’t need to correct them later.

Always do things in a quality manner. Never compromise. For if you do, you are compromising your character.

This American Life episode about NUMMI
Becoming a Person of Influence

How to easily create Models and Table Relationships in Zend Framework

Saturday, April 24th, 2010

When I first started using Zend Framework, I was so frustrated when trying to figure out how to get information from the database. I fought against the framework for the longest time. Instead of working with it, I would write the SQL and then simply query the database to get the data back as an array of objects.

Later on I learned at just how powerful Zend Framework can be when it comes to retrieving models. It is actually easier and more fun to do things in the “Zend” way.

This is the post I wish I had read before spending hours going about things the wrong way.

I’m using Zend Framework for a project which has videos, users (each user can have many videos) and tags (many tags to many videos). This example will show you how to setup your models correctly:

/application/models/DbTable/Videos.php

class Model_DbTable_Videos extends Zend_Db_Table_Abstract
{
	protected $_name = 'videos';	// database table name
	protected $_rowClass = 'Model_Row_Video';	// row class for extending
	protected $_dependentTables = array('Model_DbTable_VideoTag');	// videos depends on the many-to-many join table for tags

	protected $_referenceMap = array(
		'User' => array(
			'columns' => 'user_id',	// the column in the 'videos' table which is used for the join
			'refTableClass' => 'users',	// the users table name
			'refColumns' => 'id'	// the primary key of the users table
		)
	);
}

/application/models/DbTable/User.php

class Model_DbTable_Users extends Zend_Db_Table_Abstract
{
	protected $_name = 'users';
	protected $_rowClass = 'Model_Row_User';
	protected $_dependentTables = array('Model_DbTable_Videos');
}

/application/models/DbTable/Tags.php

class Model_DbTable_Tags extends Zend_Db_Table_Abstract
{
    protected $_name = 'tags';
    protected $_rowsetClass = 'Model_Rowset_Tags';
}

/application/models/DbTable/VideoTag.php (the join table for many-to-many relationship)

class Model_DbTable_VideoTag extends Zend_Db_Table_Abstract
{
    protected $_name = 'video_tag';
    protected $_referenceMap = array(
		'Video' => array(
			'columns' => 'video_id',
			'refTableClass' => 'Model_DbTable_Videos',
			'refColumns' => 'id'
   		),
   		'Tag' => array(
   			'columns' => 'tag_id',
   			'refTableClass' => 'Model_DbTable_Tags',
   			'refColumns' => 'id'
   		)
	);
}

The protected fields allow you to tell the framework the setup of the tables. You can also extend the Row on Rowset by simply setting the $_rowClass or $_rowsetClass field.

In Model_DbTable_Videos, you see I have specified the $_rowClass field. This means any row returned will be an object of Model_Row_Video. In the class Model_Row_Video, I have added extra methods for easily retrieving the user of the video and tags belonging to that video:

/application/models/Row/Video.php

class Model_Row_Video extends Zend_Db_Table_Row_Abstract
{
	private $tags = null;
	private $user = null;

	/**
	 * @return Model_Row_User
	 */
	public function getUser()
	{
		if (!$this->user) {
			$this->user = $this->findParentRow('Model_DbTable_Users');
		}

		return $this->user;
	}

	/**
	 * @return Model_Rowset_Tags
	 */
	public function getTags()
	{
		if (!$this->tags) {
			$this->tags = $this->findManyToManyRowset(
				'Model_DbTable_Tags',	// match table
				'Model_DbTable_VideoTag');	// join table
		}

		return $this->tags;
	}
}

Because I have all the relationships setup, I simply need to call findParentRow() or findManyToManyRowset() to get the Row or Rowset of a related record.

Here is an example for the Rowset:

/application/models/Rowset/Tags.php

class Model_Rowset_Tags extends Zend_Db_Table_Rowset_Abstract
{
	/**
	 * @return array the tags in an array
	 */
	public function getAsArray()
	{
		$tags = array();

		while ($this->valid()) {
			$tag = $this->current();
			$tags[] = $tag->name;  // the actual tag name
			$this->next();
		}

		$this->rewind();

		return $tags;
	}
}

So to get the tags of the video with ID 23 all I need to do now is simply:

$tblVideo = new Model_DbTable_Videos();
$video = $tblVideo->find(23)->current();  // returns Model_Row_Video
$tagsArr = $video->getTags()->getAsArray();

Working with Zend Framework becomes very easy and pleasurable once you follow their setup.

Refer to the Zend_Db section of the manual for more information about working with the database.

You Don’t Want Money

Thursday, April 15th, 2010

The other day, a young kid on the bus struck up a conversation with me. During the conversation, he said he wanted to make a lot of money because he doesn’t get enough respect from his peers. He thought if he could become rich enough, he could earn that respect.

Thinking about this conversation later, I realised no one really wants money. Money is meaningless. When people say they want money, they really mean they want something intangible which money can help them get. This could be respect, as in the case of this young man. For a lot of people, it probably comes down to happiness, security, love, power, respect or freedom.

Personally for me, I like freedom. I like being able to work on what I want and having autonomous control over my time and activities.

For the past few years, I’ve had dreams of retiring early so I can spend time doing what I want. I follow the blog Early Retirement Extreme of a man who retired in his early 30s by saving 80% of his salary and making smart investments which now cover his humble living expenses. This was the path I wanted to follow up until my revelation after this conversation.

What I really want is freedom. Money is just a tool that can be used to buy my time and freedom. However, if I want to save enough to retire extremely early, I would basically need to postpone life. Postponing life for a few years is a sacrifice to get to the goal. But since money is just a tool to achieve freedom, I should just cut out the middle-man to achieve my goal faster.

After getting laid off in 2009, I ramped up my free-lance work. It didn’t take long for me to find enough work to pay for my lifestyle. I managed to survive just as most people manage to do. It’s human nature to find a way. That feeling of knowing that even getting laid off does not really affect me is priceless.

If you are working long hours or on something you don’t like for money, ask yourself what you are really working for? Remember, you don’t really want the money. What do you want to use that money for? Is it happiness? Freedom? Love? Once you have the answer, understand that you can cut out the middleman and go right for it. Don’t postpone life.

Early Retirement Extreme

The Dirty Secret of Work

Sunday, April 11th, 2010

This is my 4th Toastmasters speech. Please evaluate this Toastmasters speech at Soapbox Guru.

Good evening Mr. Toastmaster, fellow Toastmasters and welcome guests.

How many people here work full-time – about 40 hours a week or so?

Unfortunately, I have some bad news for you. You’re working too much.

What if I told you it was possible to be happier, to be more satisfied with your job and get more done while working fewer hours? Now I know what you’re thinking, you’re thinking whoa, wait a minute Matt, work less and get more done? This doesn’t seem possible. It must define some kind of law of physics or something. But it is true, if you’re working 40 hours, I believe you’re working too much.

Now to understand why, we need to go back and first look at the history of this 40-hour work week. People, I think, are working much longer than they’ve worked for most of history. Prior to the Industrial Revolution, people mainly worked on farms and work was limited by natural factors such as the seasons and daylight. No one’s going to go out in the middle of December and start planting seeds because that’s just bogus. Then the Industrial Revolution came and people flocked to the factories. Factory workers encouraged people to work as much as they could – work them to the bone, work them to death. So people worked 80, 100 hours a week. Their lives, basically were work. And since then, we’ve backed off from that to now where 40 hours is considered a full-time work week. And the 40-hour work week came about mainly because of one man. And you know him because some of you probably drive his cars. That man is Henry Ford.

Henry Ford did over 12 years of productivity experiments about how he can get his workers to work the most effectively so he can get the most profit. He realised that by working people as much as possible, people get tired, people can’t concentrate, they make mistakes causing bad cars, they get sick easier and then it affects everyone else thereby reducing productivity. So he discovered that 40 hours seems about right for what people should be working. So in 1926, he let all his employees know that from now on we are only going to be working 5 days a week, 8 hours a day. No overtime. No more working Saturdays. Let’s just work this. So that’s where we are today.

Now there’s one problem with this. How many people here work in a factory? Exactly. So the 40 hour work week is great for factory work but does it translate into work that most people do today which is, say, knowledge work or problem-solving work. And no, I don’t believe it does. I think the dirtiest secret of work is that people go to work for 40 hours but it is impossible to actually be productive at work for 40 hours. I would say that people do about 20 hours of productive work a week at work. Why do I say this? It’s because what I about do.

I know there’s a risk in me saying this but I’ll say it anyways.

So why do I know this? Because when I’m working I use a timer to track my productive work – the time I’m doing focused work on programming. I run it and I found that doing 20 hours of work a week seems about right. If I try to push myself and do more, I notice that I become more depressed, I am not looking towards going to work anymore, I get sick easier. But if I back it off a little, then I enjoy my work better, I feel I can concentrate more and I believe that I get more done.

The problem is that for most people they view work as something like this. The more hours you put in, the more productive you are. That every hour extra is another hour you can achieve more things. This is completely wrong.

What it actually looks like is like this. When you start off, you are pretty productive and then you reach a point. And as Henry Ford saw, this is about 40 hours for factory workers, a little less for problem-solving or creative knowledge workers and you actually start to lose your productivity. This has been done in studies in the game development industry for software. There’s been studies done because they’re known for pushing their workers for working long hours to get the games developed to a deadline. And so they find that, sure, you can push yourself for a while, say if you want to work 60 hours or 80 hours for a couple weeks, it works ok. That’s where people start to think they can keep pushing themselves. But as it works out that you will actually start to lose productivity in the long-run.

Work hasn’t changed much over the last 100 years since Henry Ford decided to go to a 40-hour work week but there is hope. Some companies are working towards what’s called a Results Only Work Environment, where they don’t care where you work from, they don’t care how many hours you work as long as you get the work done. And I hope this is what we’ll see going forward as it will help make people happier, more satisfied with their jobs and you will get more done in less time.